diff --git a/Gemfile b/Gemfile index a7a40d797..3c4aa8c94 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,7 @@ group :development do gem 'rmail', '~> 1.1' gem 'sinatra', '~> 1.4' gem 'redis', '~> 3.2' + gem 'activesupport', '>= 3.2' end platforms :jruby do diff --git a/bin/generate-api b/bin/generate-api index c16b6f957..f9a8111e6 100755 --- a/bin/generate-api +++ b/bin/generate-api @@ -1,15 +1,17 @@ #!/usr/bin/env ruby -# TODO - Repeated params - require 'thor' require 'open-uri' require 'google/apis/discovery_v1' -require 'google/apis/generator' require 'logger' module Google class ApiGenerator < Thor + + def self.exit_on_failure? + true + end + include Thor::Actions Google::Apis::ClientOptions.default.application_name = "generate-api" @@ -23,6 +25,9 @@ module Google names_out: :string method_option :preferred_only, default: true def gen(dir) + ensure_active_support + require 'google/apis/generator' + self.destination_root = dir Google::Apis.logger.level = Logger::DEBUG if options[:verbose] generate_from_url(options[:url]) if options[:url] @@ -86,6 +91,16 @@ module Google def generator @generator ||= Google::Apis::Generator.new(api_names: options[:names]) end + + def ensure_active_support + begin + require 'active_support/inflector' + rescue LoadError => e + error 'ActiveSupport is required, please run:' + error 'gem install activesupport' + exit 1 + end + end end end diff --git a/lib/google/apis/core/api_command.rb b/lib/google/apis/core/api_command.rb index 2733ae13c..519c0c97e 100644 --- a/lib/google/apis/core/api_command.rb +++ b/lib/google/apis/core/api_command.rb @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require 'active_support/inflector' require 'addressable/uri' require 'addressable/template' require 'google/apis/core/http_command' @@ -127,7 +126,9 @@ module Google # Updated header value def normalize_fields_param(fields) # TODO: Generate map of parameter names during code gen. Small possibility that camelization fails - fields.gsub(/:/, '').gsub(/\w+/) { |str| ActiveSupport::Inflector.camelize(str, false) } + fields.gsub(/:/, '').gsub(/\w+/) do |str| + str.gsub(/(?:^|_)([a-z])/){ Regexp.last_match.begin(0) == 0 ? $1 : $1.upcase } + end end end end diff --git a/spec/google/apis/core/api_command_spec.rb b/spec/google/apis/core/api_command_spec.rb index bcbc896e8..4920c496c 100644 --- a/spec/google/apis/core/api_command_spec.rb +++ b/spec/google/apis/core/api_command_spec.rb @@ -103,7 +103,7 @@ RSpec.describe Google::Apis::Core::HttpCommand do context('with a field parameter') do let(:command) do command = Google::Apis::Core::ApiCommand.new(:get, 'https://www.googleapis.com/zoo/animals') - command.query['fields'] = ':items(:id, :long_name)' + command.query['fields'] = ':items(:id, :long_name, :a_really_long_name), shouldBeLeftAlone ' command end @@ -115,7 +115,7 @@ RSpec.describe Google::Apis::Core::HttpCommand do it 'should normalize fields params' do command.execute(client) expect(a_request(:get, 'https://www.googleapis.com/zoo/animals') - .with(query: { 'fields' => 'items(id, longName)' })) .to have_been_made + .with(query: { 'fields' => 'items(id, longName, aReallyLongName), shouldBeLeftAlone ' })) .to have_been_made end end