diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..2a5c8de --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,45 @@ +AllCops: + Exclude: + - bin/**/* + - script/**/* + - vendor/**/* + - cookbooks/**/* + +ClassLength: + Enabled: false +CyclomaticComplexity: + Enabled: false +Documentation: + Enabled: false +Encoding: + Enabled: false +LineLength: + Enabled: false +MethodLength: + Enabled: false +Metrics/AbcSize: + Enabled: false +Metrics/ModuleLength: + Enabled: false +PerceivedComplexity: + Enabled: false +Style/SpaceBeforeFirstArg: + Enabled: true +Style/ClassAndModuleChildren: + Enabled: false +Style/EmptyLinesAroundBlockBody: + Enabled: true +Style/FileName: + Enabled: true +Style/RescueModifier: + Enabled: true +Style/StringLiterals: + Enabled: true +Metrics/BlockLength: + Enabled: false +Style/NumericLiterals: + Enabled: false +Style/ExtraSpacing: + Enabled: true + AllowForAlignment: false + ForceEqualSignAlignment: false diff --git a/Rakefile b/Rakefile index cf4652f..7312e67 100644 --- a/Rakefile +++ b/Rakefile @@ -4,5 +4,28 @@ Rake::TestTask.new do |t| t.libs << 'test' end +desc 'run rubocop' +task :rubocop do + sh 'rubocop -c .rubocop.yml --display-only-fail-level-offenses -D' +end + +desc 'run rubocop w/autocorrect' +task :rubocorrect do + sh 'rubocop -c .rubocop.yml -a' +end + +desc 'run minitest' +task :minitest do + Rake::Task[:test].invoke +end + desc 'Run tests' -task default: :test +task default: 'test:quick' + +namespace :test do + desc 'Run all the quick tests' + task :quick do + Rake::Task['rubocop'].invoke + Rake::Task['minitest'].invoke + end +end diff --git a/lib/ruby_http_client.rb b/lib/ruby_http_client.rb index 433a560..4d0aafd 100644 --- a/lib/ruby_http_client.rb +++ b/lib/ruby_http_client.rb @@ -38,7 +38,7 @@ class Client # - +proxy_options+ -> A hash of proxy settings. # (e.g. { host: '127.0.0.1', port: 8080 }) # - def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {}, proxy_options: {}) + def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {}, proxy_options: {}) # rubocop:disable Metrics/ParameterLists @host = host @request_headers = request_headers || {} @version = version @@ -182,7 +182,7 @@ def make_request(http, request) # - Request object def build_http(host, port) params = [host, port] - params = params + @proxy_options.values_at(:host, :port, :user, :pass) unless @proxy_options.empty? + params += @proxy_options.values_at(:host, :port, :user, :pass) unless @proxy_options.empty? add_ssl(Net::HTTP.new(*params)) end @@ -227,6 +227,8 @@ def _(name = nil) # * *Returns* : # - Client object or Response object # + # rubocop:disable Style/MethodMissingSuper + # rubocop:disable Style/MissingRespondToMissing def method_missing(name, *args, &_block) # Capture the version if name.to_s == 'version' @@ -235,8 +237,11 @@ def method_missing(name, *args, &_block) end # We have reached the end of the method chain, make the API call return build_request(name, args) if @methods.include?(name.to_s) + # Add a segment to the URL _(name) end + # rubocop:enable Style/MethodMissingSuper + # rubocop:enable Style/MissingRespondToMissing end end diff --git a/ruby_http_client.gemspec b/ruby_http_client.gemspec index 6d87bd2..e32940a 100644 --- a/ruby_http_client.gemspec +++ b/ruby_http_client.gemspec @@ -1,21 +1,22 @@ - -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) Gem::Specification.new do |spec| - spec.name = 'ruby_http_client' - spec.version = '3.3.0' - spec.authors = ['Elmer Thomas'] - spec.email = 'dx@sendgrid.com' - spec.summary = 'A simple REST client' + spec.name = 'ruby_http_client' + spec.version = '3.3.0' + spec.authors = ['Elmer Thomas'] + spec.email = 'dx@sendgrid.com' + spec.summary = 'A simple REST client' spec.description = 'Quickly and easily access any REST or REST-like API.' - spec.homepage = 'http://github.com/sendgrid/ruby-http-client' - spec.license = 'MIT' - spec.files = `git ls-files -z`.split("\x0") - spec.executables = spec.files.grep(/^bin/) { |f| File.basename(f) } - spec.test_files = spec.files.grep(/^(test|spec|features)/) + spec.homepage = 'http://github.com/sendgrid/ruby-http-client' + spec.license = 'MIT' + spec.files = `git ls-files -z`.split("\x0") + spec.executables = spec.files.grep(/^bin/) { |f| File.basename(f) } + spec.test_files = spec.files.grep(/^(test|spec|features)/) spec.require_paths = ['lib'] - spec.add_development_dependency 'rake', '~> 0' - spec.add_development_dependency 'simplecov', '~> 0' + spec.add_development_dependency 'minitest' + spec.add_development_dependency 'rake' + spec.add_development_dependency 'rubocop' + spec.add_development_dependency 'simplecov' end diff --git a/test/test_helper.rb b/test/test_helper.rb index 67b05d1..9d0bdec 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,4 +2,3 @@ require 'simplecov' SimpleCov.start end - diff --git a/test/test_ruby_http_client.rb b/test/test_ruby_http_client.rb index 928e792..69517c2 100644 --- a/test/test_ruby_http_client.rb +++ b/test/test_ruby_http_client.rb @@ -32,14 +32,14 @@ def setup ') @host = 'http://localhost:4010' @version = 'v3' - @http_options = {open_timeout: 60, read_timeout: 60} + @http_options = { open_timeout: 60, read_timeout: 60 } @client = MockRequest.new(host: @host, request_headers: @headers, version: @version) @client_with_options = MockRequest.new(host: @host, - request_headers: @headers, - version: @version, - http_options: @http_options) + request_headers: @headers, + version: @version, + http_options: @http_options) end def test_init @@ -67,7 +67,7 @@ def test_add_version def test_build_query_params url = '' - query_params = { 'limit' => 100, 'offset' => 0, 'categories' => ['category1', 'category2'] } + query_params = { 'limit' => 100, 'offset' => 0, 'categories' => %w[category1 category2] } url = @client.build_query_params(url, query_params) assert_equal('?limit=100&offset=0&categories=category1&categories=category2', url) end @@ -97,8 +97,8 @@ def test_build_request args = nil response = @client.build_request(name, args) assert_equal(200, response.status_code) - assert_equal({'message' => 'success'}, response.body) - assert_equal({'headers' => 'test'}, response.headers) + assert_equal({ 'message' => 'success' }, response.body) + assert_equal({ 'headers' => 'test' }, response.headers) end def test_build_request_post_empty_content_type @@ -109,7 +109,7 @@ def test_build_request_post_empty_content_type request_headers: headers, version: 'v3' ) - args = [{'request_body' => {"hogekey" => "hogevalue"}}] + args = [{ 'request_body' => { 'hogekey' => 'hogevalue' } }] client.build_request('post', args) assert_equal('application/json', client.request['Content-Type']) assert_equal('{"hogekey":"hogevalue"}', client.request.body) @@ -149,10 +149,10 @@ def test_build_request_post_multipart } client = MockRequest.new( host: 'https://localhost', - request_headers: headers, + request_headers: headers ) name = 'post' - args = [{'request_body' => 'hogebody'}] + args = [{ 'request_body' => 'hogebody' }] client.build_request(name, args) assert_equal('multipart/form-data; boundary=xYzZY', client.request['Content-Type']) assert_equal('hogebody', client.request.body) @@ -174,8 +174,8 @@ def test__ def test_method_missing response = @client.get assert_equal(200, response.status_code) - assert_equal({'message' => 'success'}, response.body) - assert_equal({'headers' => 'test'}, response.headers) + assert_equal({ 'message' => 'success' }, response.body) + assert_equal({ 'headers' => 'test' }, response.headers) end def test_http_options @@ -273,14 +273,10 @@ def test_troubleshooting_exists assert(File.file?('./TROUBLESHOOTING.md')) end - # def test_usage_exists - # assert(File.file?('./USAGE.md')) - # end + def test_use_cases_exists + assert(File.file?('use_cases/README.md')) + end - # def test_use_cases_exists - # assert(File.file?('./USE_CASES.md')) - # end - def test_license_date_is_updated license_end_year = IO.read('LICENSE.txt').match(/Copyright \(c\) 2016-(\d{4}) SendGrid/)[1].to_i current_year = Time.new.year