From d6319d67c8347675e85da26cf7267eefc6e61024 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 18:26:59 -0400 Subject: [PATCH 01/67] Explicitly specify ruby version --- .ruby-version | 2 ++ Gemfile | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..025e764 --- /dev/null +++ b/.ruby-version @@ -0,0 +1,2 @@ +2.5.1 + diff --git a/Gemfile b/Gemfile index c398068..b5c8215 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,7 @@ source "https://rubygems.org" +ruby '2.5.1' + gemspec gem 'rest-client', '~> 1.6.8' From 00407c642d051fe5d6ada2e60550d9f74bfc4840 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 18:27:17 -0400 Subject: [PATCH 02/67] Include rubocop for style analysis --- .rubocop.yml | 4 ++++ onfleet-ruby.gemspec | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..614e6c0 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,4 @@ +# Keep diffs clean +Layout/TrailingBlankLines: + EnforcedStyle: final_blank_line + diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index 5e4e916..77c0681 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -12,7 +12,7 @@ Gem::Specification.new do |s| s.add_dependency('rest-client', '~> 1.4') s.add_development_dependency("rspec",'~> 3.3.0', '>= 3.0.0') - + s.add_development_dependency('rubocop','~> 0.55') s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") From 0682175a68a5c624006c6c803feec5953579e91b Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 18:35:40 -0400 Subject: [PATCH 03/67] Place dot operator consistently on multiline calls rubocop: Layout/DotPosition --- lib/onfleet-ruby/util.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 9ca8062..4a74950 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -10,11 +10,11 @@ def self.to_underscore key if key.kind_of?(Symbol) key = key.to_s end - key.gsub(/::/, '/'). - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). - gsub(/([a-z\d])([A-Z])/,'\1_\2'). - tr("-", "_"). - downcase + key.gsub(/::/, '/') + .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') + .gsub(/([a-z\d])([A-Z])/,'\1_\2') + .tr("-", "_") + .downcase end def self.to_camel_case_lower str From cd1055b6fea5f8b33ac909e06f28071a82c652a8 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 18:37:19 -0400 Subject: [PATCH 04/67] Remove extraneous whitespace rubocop: - Layout/EmptyLines - Layout/EmptyLinesAroundAccessModifier - Layout/EmptyLinesAroundBlockBody - Layout/EmptyLinesAroundClassBody - Layout/EmptyLinesAroundMethodBody - Layout/EmptyLinesAroundModuleBody - Layout/ExtraSpacing --- Gemfile | 1 - lib/onfleet-ruby.rb | 2 +- lib/onfleet-ruby/actions/save.rb | 2 +- lib/onfleet-ruby/onfleet_object.rb | 2 -- lib/onfleet-ruby/organization.rb | 6 ++---- lib/onfleet-ruby/task.rb | 1 - lib/onfleet-ruby/util.rb | 1 - lib/onfleet-ruby/webhook.rb | 1 - spec/test_data.rb | 2 -- 9 files changed, 4 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index b5c8215..d502b79 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,3 @@ ruby '2.5.1' gemspec gem 'rest-client', '~> 1.6.8' - diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index bbbcd3d..f54c5c9 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -35,7 +35,6 @@ require 'onfleet-ruby/worker' require 'onfleet-ruby/webhook' - module Onfleet @base_url = "https://onfleet.com/api/v2" @@ -64,6 +63,7 @@ def self.request api_url, method, params={} end private + def self.request_headers { Authorization: "Basic #{self.encoded_api_key}", diff --git a/lib/onfleet-ruby/actions/save.rb b/lib/onfleet-ruby/actions/save.rb index 8a5e39f..108c575 100644 --- a/lib/onfleet-ruby/actions/save.rb +++ b/lib/onfleet-ruby/actions/save.rb @@ -6,7 +6,7 @@ def save request_type = :put api_url = "#{self.api_url}/#{self.id}" else - request_type = :post + request_type = :post api_url = self.api_url end response = Onfleet.request(api_url, request_type, self.attributes) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 37900ef..b4105b9 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -87,7 +87,6 @@ def set_attributes params else add_attrs({"#{key_underscore}" => value}) end - end end @@ -97,6 +96,5 @@ def add_attrs attrs instance_variable_set "@#{var}", value end end - end end diff --git a/lib/onfleet-ruby/organization.rb b/lib/onfleet-ruby/organization.rb index 95d5ad6..d9b8ffb 100644 --- a/lib/onfleet-ruby/organization.rb +++ b/lib/onfleet-ruby/organization.rb @@ -1,19 +1,17 @@ module Onfleet class Organization < OnfleetObject - class << self def get url = "/organization" - response = Onfleet.request(url, :get) + response = Onfleet.request(url, :get) Util.constantize("#{self}").new(response) end def get_delegatee_details id url = "/organizations/#{id}" - response = Onfleet.request(url, :get) + response = Onfleet.request(url, :get) Util.constantize("#{self}").new(response) end end - end end diff --git a/lib/onfleet-ruby/task.rb b/lib/onfleet-ruby/task.rb index c3deeac..39acdab 100644 --- a/lib/onfleet-ruby/task.rb +++ b/lib/onfleet-ruby/task.rb @@ -19,6 +19,5 @@ def complete Onfleet.request(url, :post, params) true end - end end diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 4a74950..bbeee3a 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -32,5 +32,4 @@ def self.object_classes } end end - end diff --git a/lib/onfleet-ruby/webhook.rb b/lib/onfleet-ruby/webhook.rb index b6e234e..9dc66e5 100644 --- a/lib/onfleet-ruby/webhook.rb +++ b/lib/onfleet-ruby/webhook.rb @@ -5,7 +5,6 @@ class Webhook < OnfleetObject include Onfleet::Actions::Save include Onfleet::Actions::Delete - def self.api_url '/webhooks' end diff --git a/spec/test_data.rb b/spec/test_data.rb index df73c60..f779d72 100644 --- a/spec/test_data.rb +++ b/spec/test_data.rb @@ -1,11 +1,9 @@ module Onfleet module TestData def recipient - end def destination - end def task From 919f9fc5004e6edd974b5604286be736118784fa Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 18:52:41 -0400 Subject: [PATCH 05/67] Indent consistently rubocop: - Layout/IndentationConsistency - Layout/IndentationWidth - Layout/MultilineMethodCallIndentation --- lib/onfleet-ruby.rb | 60 +++++++++++++++--------------- lib/onfleet-ruby/onfleet_object.rb | 60 +++++++++++++++--------------- lib/onfleet-ruby/util.rb | 3 +- 3 files changed, 62 insertions(+), 61 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index f54c5c9..3dbe368 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -64,39 +64,39 @@ def self.request api_url, method, params={} private - def self.request_headers - { - Authorization: "Basic #{self.encoded_api_key}", - content_type: :json, - accept: :json - } - end + def self.request_headers + { + Authorization: "Basic #{self.encoded_api_key}", + content_type: :json, + accept: :json + } + end - def self.encoded_api_key - @encoded_api_key ||= Base64.urlsafe_encode64(@api_key) - end + def self.encoded_api_key + @encoded_api_key ||= Base64.urlsafe_encode64(@api_key) + end - def self.handle_api_error code, body - case code - when 400, 404 - raise InvalidRequestError.new(body["message"]) - when 401 - raise AuthenticationError.new(body["message"]) - else - raise OnfleetError.new(body["message"]) - end + def self.handle_api_error code, body + case code + when 400, 404 + raise InvalidRequestError.new(body["message"]) + when 401 + raise AuthenticationError.new(body["message"]) + else + raise OnfleetError.new(body["message"]) end + end - def self.handle_restclient_error e - case e - when RestClient::RequestTimeout - message = "Could not connect to Onfleet. Check your internet connection and try again." - when RestClient::ServerBrokeConnection - message = "The connetion with onfleet terminated before the request completed. Please try again." - else - message = "There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com" - end - - raise ConnectionError.new(message) + def self.handle_restclient_error e + case e + when RestClient::RequestTimeout + message = "Could not connect to Onfleet. Check your internet connection and try again." + when RestClient::ServerBrokeConnection + message = "The connetion with onfleet terminated before the request completed. Please try again." + else + message = "There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com" end + + raise ConnectionError.new(message) + end end diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index b4105b9..b8c570d 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -55,46 +55,46 @@ def api_url private - def parse_onfleet_obj obj - if obj.is_a?(OnfleetObject) - if obj.respond_to?('id') && obj.id && (obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task)) - obj.id - else - obj.attributes - end + def parse_onfleet_obj obj + if obj.is_a?(OnfleetObject) + if obj.respond_to?('id') && obj.id && (obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task)) + obj.id + else + obj.attributes end end + end - def set_attributes params - params.each do |key, value| - key_underscore = Util.to_underscore(key) + def set_attributes params + params.each do |key, value| + key_underscore = Util.to_underscore(key) - if klass = Util.object_classes[key.to_s] - case value - when Array - objs = [] - value.each do |v| - objs << klass.new(v) - end - value = objs - when Hash - value = klass.new(value) + if klass = Util.object_classes[key.to_s] + case value + when Array + objs = [] + value.each do |v| + objs << klass.new(v) end + value = objs + when Hash + value = klass.new(value) end + end - if respond_to?("#{key_underscore}=") - send(:"#{key_underscore}=", value) - else - add_attrs({"#{key_underscore}" => value}) - end + if respond_to?("#{key_underscore}=") + send(:"#{key_underscore}=", value) + else + add_attrs({"#{key_underscore}" => value}) end end + end - def add_attrs attrs - attrs.each do |var, value| - self.class.class_eval { attr_accessor var } - instance_variable_set "@#{var}", value - end + def add_attrs attrs + attrs.each do |var, value| + self.class.class_eval { attr_accessor var } + instance_variable_set "@#{var}", value end + end end end diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index bbeee3a..160b56d 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -10,7 +10,8 @@ def self.to_underscore key if key.kind_of?(Symbol) key = key.to_s end - key.gsub(/::/, '/') + key + .gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .tr("-", "_") From b293021f26630a602c090d601c9fe893a96b5cb0 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 18:58:53 -0400 Subject: [PATCH 06/67] Space parameters and operator consistently rubocop: - Layout/SpaceAfterComma - Layout/SpaceAroundEqualsInParameterDefault - Layout/SpaceAroundOperators - Layout/SpaceInsideBlockBraces - Layout/SpaceInsideHashLiteralBraces --- lib/onfleet-ruby.rb | 4 ++-- lib/onfleet-ruby/actions/create.rb | 2 +- lib/onfleet-ruby/actions/get.rb | 2 +- lib/onfleet-ruby/actions/list.rb | 2 +- lib/onfleet-ruby/onfleet_object.rb | 6 +++--- lib/onfleet-ruby/task.rb | 2 +- lib/onfleet-ruby/util.rb | 4 ++-- onfleet-ruby.gemspec | 4 ++-- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 3dbe368..2ac6af6 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -42,11 +42,11 @@ class << self attr_accessor :api_key, :base_url, :encoded_api_key end - def self.request api_url, method, params={} + def self.request api_url, method, params = {} raise AuthenticationError.new("Set your API Key using Onfleet.api_key = ") unless @api_key begin - response = RestClient::Request.execute(method: method, url: self.base_url+api_url, payload: params.to_json, headers: self.request_headers) + response = RestClient::Request.execute(method: method, url: self.base_url + api_url, payload: params.to_json, headers: self.request_headers) if response != '' JSON.parse(response) diff --git a/lib/onfleet-ruby/actions/create.rb b/lib/onfleet-ruby/actions/create.rb index 984d247..4b1d083 100644 --- a/lib/onfleet-ruby/actions/create.rb +++ b/lib/onfleet-ruby/actions/create.rb @@ -2,7 +2,7 @@ module Onfleet module Actions module Create module ClassMethods - def create params={} + def create params = {} self.new(params).save end end diff --git a/lib/onfleet-ruby/actions/get.rb b/lib/onfleet-ruby/actions/get.rb index 186cace..a59b5af 100644 --- a/lib/onfleet-ruby/actions/get.rb +++ b/lib/onfleet-ruby/actions/get.rb @@ -4,7 +4,7 @@ module Get module ClassMethods def get id api_url = "#{self.api_url}/#{id}" - response = Onfleet.request(api_url, :get) + response = Onfleet.request(api_url, :get) Util.constantize("#{self}").new(response) end end diff --git a/lib/onfleet-ruby/actions/list.rb b/lib/onfleet-ruby/actions/list.rb index 77316ae..05ff00b 100644 --- a/lib/onfleet-ruby/actions/list.rb +++ b/lib/onfleet-ruby/actions/list.rb @@ -2,7 +2,7 @@ module Onfleet module Actions module List module ClassMethods - def list query_params={} + def list query_params = {} api_url = "#{self.api_url}" if !query_params.empty? diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index b8c570d..7084c3b 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -6,7 +6,7 @@ def initialize params @params = params set_attributes(@params) elsif params.kind_of?(String) - @params = {id: params} + @params = { id: params } set_attributes(@params) else @params = {} @@ -21,7 +21,7 @@ def parse_response response def attributes attrs = Hash.new - instance_variables.select {|var| var != '@params'}.each do |var| + instance_variables.select { |var| var != '@params' }.each do |var| str = var.to_s.gsub /^@/, '' if respond_to?("#{str}=") instance_var = instance_variable_get(var) @@ -85,7 +85,7 @@ def set_attributes params if respond_to?("#{key_underscore}=") send(:"#{key_underscore}=", value) else - add_attrs({"#{key_underscore}" => value}) + add_attrs({ "#{key_underscore}" => value }) end end end diff --git a/lib/onfleet-ruby/task.rb b/lib/onfleet-ruby/task.rb index 39acdab..1f7e1e3 100644 --- a/lib/onfleet-ruby/task.rb +++ b/lib/onfleet-ruby/task.rb @@ -15,7 +15,7 @@ def self.api_url def complete # CURRENTLY DOESN'T WORK url = "#{self.url}/#{self.id}/complete" - params = {"completionDetails" => {"success" => true }} + params = { "completionDetails" => { "success" => true } } Onfleet.request(url, :post, params) true end diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 160b56d..99ce460 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -12,8 +12,8 @@ def self.to_underscore key end key .gsub(/::/, '/') - .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') - .gsub(/([a-z\d])([A-Z])/,'\1_\2') + .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') + .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr("-", "_") .downcase end diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index 77c0681..b1e7e42 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -11,8 +11,8 @@ Gem::Specification.new do |s| s.add_dependency('rest-client', '~> 1.4') - s.add_development_dependency("rspec",'~> 3.3.0', '>= 3.0.0') - s.add_development_dependency('rubocop','~> 0.55') + s.add_development_dependency("rspec", '~> 3.3.0', '>= 3.0.0') + s.add_development_dependency('rubocop', '~> 0.55') s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") From 93d2ddd290a04173c6489bdaf2aaf9460f5b3acd Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:03:54 -0400 Subject: [PATCH 07/67] Include trailing line consistently in all files rubocop: Layout/TrailingBlankLines --- lib/onfleet-ruby.rb | 1 + lib/onfleet-ruby/actions/create.rb | 1 + lib/onfleet-ruby/actions/delete.rb | 1 + lib/onfleet-ruby/actions/find.rb | 1 + lib/onfleet-ruby/actions/get.rb | 1 + lib/onfleet-ruby/actions/list.rb | 1 + lib/onfleet-ruby/actions/query_metadata.rb | 1 + lib/onfleet-ruby/actions/save.rb | 1 + lib/onfleet-ruby/actions/update.rb | 1 + lib/onfleet-ruby/address.rb | 1 + lib/onfleet-ruby/admin.rb | 1 + lib/onfleet-ruby/destination.rb | 1 + lib/onfleet-ruby/errors/authentication_error.rb | 1 + lib/onfleet-ruby/errors/connection_error.rb | 1 + lib/onfleet-ruby/errors/invalid_request_error.rb | 1 + lib/onfleet-ruby/errors/onfleet_error.rb | 1 + lib/onfleet-ruby/onfleet_object.rb | 1 + lib/onfleet-ruby/organization.rb | 1 + lib/onfleet-ruby/recipient.rb | 1 + lib/onfleet-ruby/task.rb | 1 + lib/onfleet-ruby/team.rb | 1 + lib/onfleet-ruby/util.rb | 1 + lib/onfleet-ruby/vehicle.rb | 1 + lib/onfleet-ruby/webhook.rb | 1 + lib/onfleet-ruby/worker.rb | 1 + onfleet-ruby.gemspec | 1 + spec/spec_helper.rb | 1 + spec/test_data.rb | 1 + 28 files changed, 28 insertions(+) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 2ac6af6..3eea389 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -100,3 +100,4 @@ def self.handle_restclient_error e raise ConnectionError.new(message) end end + diff --git a/lib/onfleet-ruby/actions/create.rb b/lib/onfleet-ruby/actions/create.rb index 4b1d083..f749e98 100644 --- a/lib/onfleet-ruby/actions/create.rb +++ b/lib/onfleet-ruby/actions/create.rb @@ -13,3 +13,4 @@ def self.included base end end end + diff --git a/lib/onfleet-ruby/actions/delete.rb b/lib/onfleet-ruby/actions/delete.rb index 648e563..d804fd8 100644 --- a/lib/onfleet-ruby/actions/delete.rb +++ b/lib/onfleet-ruby/actions/delete.rb @@ -15,3 +15,4 @@ def self.included base end end end + diff --git a/lib/onfleet-ruby/actions/find.rb b/lib/onfleet-ruby/actions/find.rb index 83cfe1b..58a0bd3 100644 --- a/lib/onfleet-ruby/actions/find.rb +++ b/lib/onfleet-ruby/actions/find.rb @@ -16,3 +16,4 @@ def self.included base end end end + diff --git a/lib/onfleet-ruby/actions/get.rb b/lib/onfleet-ruby/actions/get.rb index a59b5af..7feb1b7 100644 --- a/lib/onfleet-ruby/actions/get.rb +++ b/lib/onfleet-ruby/actions/get.rb @@ -15,3 +15,4 @@ def self.included base end end end + diff --git a/lib/onfleet-ruby/actions/list.rb b/lib/onfleet-ruby/actions/list.rb index 05ff00b..fd0ee2f 100644 --- a/lib/onfleet-ruby/actions/list.rb +++ b/lib/onfleet-ruby/actions/list.rb @@ -25,3 +25,4 @@ def self.included base end end end + diff --git a/lib/onfleet-ruby/actions/query_metadata.rb b/lib/onfleet-ruby/actions/query_metadata.rb index 7da9026..a4591cd 100644 --- a/lib/onfleet-ruby/actions/query_metadata.rb +++ b/lib/onfleet-ruby/actions/query_metadata.rb @@ -15,3 +15,4 @@ def self.included base end end end + diff --git a/lib/onfleet-ruby/actions/save.rb b/lib/onfleet-ruby/actions/save.rb index 108c575..62b46b1 100644 --- a/lib/onfleet-ruby/actions/save.rb +++ b/lib/onfleet-ruby/actions/save.rb @@ -15,3 +15,4 @@ def save end end end + diff --git a/lib/onfleet-ruby/actions/update.rb b/lib/onfleet-ruby/actions/update.rb index 72e0733..6f32cc9 100644 --- a/lib/onfleet-ruby/actions/update.rb +++ b/lib/onfleet-ruby/actions/update.rb @@ -14,3 +14,4 @@ def self.included base end end end + diff --git a/lib/onfleet-ruby/address.rb b/lib/onfleet-ruby/address.rb index 426b493..e1a0a65 100644 --- a/lib/onfleet-ruby/address.rb +++ b/lib/onfleet-ruby/address.rb @@ -2,3 +2,4 @@ module Onfleet class Address < OnfleetObject end end + diff --git a/lib/onfleet-ruby/admin.rb b/lib/onfleet-ruby/admin.rb index fb78166..6a23c0d 100644 --- a/lib/onfleet-ruby/admin.rb +++ b/lib/onfleet-ruby/admin.rb @@ -12,3 +12,4 @@ def self.api_url end end end + diff --git a/lib/onfleet-ruby/destination.rb b/lib/onfleet-ruby/destination.rb index 6f73e84..39cb511 100644 --- a/lib/onfleet-ruby/destination.rb +++ b/lib/onfleet-ruby/destination.rb @@ -10,3 +10,4 @@ def self.api_url end end end + diff --git a/lib/onfleet-ruby/errors/authentication_error.rb b/lib/onfleet-ruby/errors/authentication_error.rb index 9bc5dd4..8d78981 100644 --- a/lib/onfleet-ruby/errors/authentication_error.rb +++ b/lib/onfleet-ruby/errors/authentication_error.rb @@ -1,3 +1,4 @@ module Onfleet class AuthenticationError < OnfleetError; end end + diff --git a/lib/onfleet-ruby/errors/connection_error.rb b/lib/onfleet-ruby/errors/connection_error.rb index 42ba19e..790178d 100644 --- a/lib/onfleet-ruby/errors/connection_error.rb +++ b/lib/onfleet-ruby/errors/connection_error.rb @@ -1,3 +1,4 @@ module Onfleet class ConnectionError < OnfleetError; end end + diff --git a/lib/onfleet-ruby/errors/invalid_request_error.rb b/lib/onfleet-ruby/errors/invalid_request_error.rb index 4bdaa01..1e6260d 100644 --- a/lib/onfleet-ruby/errors/invalid_request_error.rb +++ b/lib/onfleet-ruby/errors/invalid_request_error.rb @@ -1,3 +1,4 @@ module Onfleet class InvalidRequestError < OnfleetError; end end + diff --git a/lib/onfleet-ruby/errors/onfleet_error.rb b/lib/onfleet-ruby/errors/onfleet_error.rb index 7c59b7f..66ee07e 100644 --- a/lib/onfleet-ruby/errors/onfleet_error.rb +++ b/lib/onfleet-ruby/errors/onfleet_error.rb @@ -1,3 +1,4 @@ module Onfleet class OnfleetError < StandardError; end end + diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 7084c3b..24640cb 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -98,3 +98,4 @@ def add_attrs attrs end end end + diff --git a/lib/onfleet-ruby/organization.rb b/lib/onfleet-ruby/organization.rb index d9b8ffb..e6d4d8c 100644 --- a/lib/onfleet-ruby/organization.rb +++ b/lib/onfleet-ruby/organization.rb @@ -15,3 +15,4 @@ def get_delegatee_details id end end end + diff --git a/lib/onfleet-ruby/recipient.rb b/lib/onfleet-ruby/recipient.rb index cc9ce68..142faef 100644 --- a/lib/onfleet-ruby/recipient.rb +++ b/lib/onfleet-ruby/recipient.rb @@ -12,3 +12,4 @@ def self.api_url end end end + diff --git a/lib/onfleet-ruby/task.rb b/lib/onfleet-ruby/task.rb index 1f7e1e3..a2cdc7f 100644 --- a/lib/onfleet-ruby/task.rb +++ b/lib/onfleet-ruby/task.rb @@ -21,3 +21,4 @@ def complete end end end + diff --git a/lib/onfleet-ruby/team.rb b/lib/onfleet-ruby/team.rb index a4485d3..d105ff6 100644 --- a/lib/onfleet-ruby/team.rb +++ b/lib/onfleet-ruby/team.rb @@ -8,3 +8,4 @@ def self.api_url end end end + diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 99ce460..2062fba 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -34,3 +34,4 @@ def self.object_classes end end end + diff --git a/lib/onfleet-ruby/vehicle.rb b/lib/onfleet-ruby/vehicle.rb index 0ecd324..4baaaf6 100644 --- a/lib/onfleet-ruby/vehicle.rb +++ b/lib/onfleet-ruby/vehicle.rb @@ -2,3 +2,4 @@ module Onfleet class Vehicle < OnfleetObject end end + diff --git a/lib/onfleet-ruby/webhook.rb b/lib/onfleet-ruby/webhook.rb index 9dc66e5..fd45dcf 100644 --- a/lib/onfleet-ruby/webhook.rb +++ b/lib/onfleet-ruby/webhook.rb @@ -10,3 +10,4 @@ def self.api_url end end end + diff --git a/lib/onfleet-ruby/worker.rb b/lib/onfleet-ruby/worker.rb index 7fa419c..d6b633c 100644 --- a/lib/onfleet-ruby/worker.rb +++ b/lib/onfleet-ruby/worker.rb @@ -13,3 +13,4 @@ def self.api_url end end end + diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index b1e7e42..1150585 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -18,3 +18,4 @@ Gem::Specification.new do |s| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.require_paths = ['lib'] end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8c9991f..e155c9e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,3 @@ require 'onfleet-ruby' require File.expand_path('../test_data', __FILE__) + diff --git a/spec/test_data.rb b/spec/test_data.rb index f779d72..cf946ba 100644 --- a/spec/test_data.rb +++ b/spec/test_data.rb @@ -10,3 +10,4 @@ def task end end end + From 348e28c05aaf4523e24930d06cd2c4a137bbe890 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:08:33 -0400 Subject: [PATCH 08/67] Fix ambiguous regexp literal rubocop: Lint/AmbiguousRegexpLiteral --- lib/onfleet-ruby/onfleet_object.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 24640cb..fd19e1e 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -22,7 +22,7 @@ def parse_response response def attributes attrs = Hash.new instance_variables.select { |var| var != '@params' }.each do |var| - str = var.to_s.gsub /^@/, '' + str = var.to_s.gsub(/^@/, '') if respond_to?("#{str}=") instance_var = instance_variable_get(var) if klass = Util.object_classes[str] From ad197e1b66406c9226cb628874fc3517562905b4 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:12:03 -0400 Subject: [PATCH 09/67] Clarify assignment in conditional expressions rubocop: Lint/AssignmentInConditions --- lib/onfleet-ruby.rb | 2 +- lib/onfleet-ruby/onfleet_object.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 3eea389..a3f182c 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -52,7 +52,7 @@ def self.request api_url, method, params = {} JSON.parse(response) end rescue RestClient::ExceptionWithResponse => e - if response_code = e.http_code and response_body = e.http_body + if (response_code = e.http_code) and (response_body = e.http_body) handle_api_error(response_code, JSON.parse(response_body)) else handle_restclient_error(e) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index fd19e1e..a1a4795 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -25,7 +25,7 @@ def attributes str = var.to_s.gsub(/^@/, '') if respond_to?("#{str}=") instance_var = instance_variable_get(var) - if klass = Util.object_classes[str] + if (klass = Util.object_classes[str]) if instance_var.is_a?(OnfleetObject) attrs[Util.to_camel_case_lower(str).to_sym] = parse_onfleet_obj(instance_var) elsif instance_var.is_a?(Array) @@ -69,7 +69,7 @@ def set_attributes params params.each do |key, value| key_underscore = Util.to_underscore(key) - if klass = Util.object_classes[key.to_s] + if (klass = Util.object_classes[key.to_s]) case value when Array objs = [] From e502a7610f6a0074a9e06b58292a5c7d6d9b4792 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:13:45 -0400 Subject: [PATCH 10/67] Remove duplicate accessor method definition rubocop: Lint/DuplicateMethods --- lib/onfleet-ruby.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index a3f182c..08f4218 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -39,7 +39,7 @@ module Onfleet @base_url = "https://onfleet.com/api/v2" class << self - attr_accessor :api_key, :base_url, :encoded_api_key + attr_accessor :api_key, :base_url end def self.request api_url, method, params = {} From cd48ba890e0a1a57b79a279f22b9c40a8ecc4325 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:19:16 -0400 Subject: [PATCH 11/67] Make private methods actually private rubocop: Lint/IneffectiveAccessModifier --- lib/onfleet-ruby.rb | 94 ++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 08f4218..6038a05 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -40,64 +40,64 @@ module Onfleet class << self attr_accessor :api_key, :base_url - end - - def self.request api_url, method, params = {} - raise AuthenticationError.new("Set your API Key using Onfleet.api_key = ") unless @api_key - - begin - response = RestClient::Request.execute(method: method, url: self.base_url + api_url, payload: params.to_json, headers: self.request_headers) - if response != '' - JSON.parse(response) - end - rescue RestClient::ExceptionWithResponse => e - if (response_code = e.http_code) and (response_body = e.http_body) - handle_api_error(response_code, JSON.parse(response_body)) - else + def request api_url, method, params = {} + raise AuthenticationError.new("Set your API Key using Onfleet.api_key = ") unless @api_key + + begin + response = RestClient::Request.execute(method: method, url: base_url + api_url, payload: params.to_json, headers: request_headers) + + if response != '' + JSON.parse(response) + end + rescue RestClient::ExceptionWithResponse => e + if (response_code = e.http_code) and (response_body = e.http_body) + handle_api_error(response_code, JSON.parse(response_body)) + else + handle_restclient_error(e) + end + rescue RestClient::Exception, Errno::ECONNREFUSED => e handle_restclient_error(e) end - rescue RestClient::Exception, Errno::ECONNREFUSED => e - handle_restclient_error(e) end - end - - private - def self.request_headers - { - Authorization: "Basic #{self.encoded_api_key}", - content_type: :json, - accept: :json - } - end + private - def self.encoded_api_key - @encoded_api_key ||= Base64.urlsafe_encode64(@api_key) - end + def request_headers + { + Authorization: "Basic #{encoded_api_key}", + content_type: :json, + accept: :json + } + end - def self.handle_api_error code, body - case code - when 400, 404 - raise InvalidRequestError.new(body["message"]) - when 401 - raise AuthenticationError.new(body["message"]) - else - raise OnfleetError.new(body["message"]) + def encoded_api_key + @encoded_api_key ||= Base64.urlsafe_encode64(api_key) end - end - def self.handle_restclient_error e - case e - when RestClient::RequestTimeout - message = "Could not connect to Onfleet. Check your internet connection and try again." - when RestClient::ServerBrokeConnection - message = "The connetion with onfleet terminated before the request completed. Please try again." - else - message = "There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com" + def handle_api_error code, body + case code + when 400, 404 + raise InvalidRequestError.new(body["message"]) + when 401 + raise AuthenticationError.new(body["message"]) + else + raise OnfleetError.new(body["message"]) + end end - raise ConnectionError.new(message) + def handle_restclient_error e + case e + when RestClient::RequestTimeout + message = "Could not connect to Onfleet. Check your internet connection and try again." + when RestClient::ServerBrokeConnection + message = "The connetion with onfleet terminated before the request completed. Please try again." + else + message = "There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com" + end + + raise ConnectionError.new(message) + end end end From 97cb0f8375a1189cd87f5c5e2472edc581bf81f6 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:27:31 -0400 Subject: [PATCH 12/67] Replace obsolete method call rubocop: Lint/UriEscapeUnescape --- lib/onfleet-ruby/actions/find.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onfleet-ruby/actions/find.rb b/lib/onfleet-ruby/actions/find.rb index 58a0bd3..e4a4137 100644 --- a/lib/onfleet-ruby/actions/find.rb +++ b/lib/onfleet-ruby/actions/find.rb @@ -3,7 +3,7 @@ module Actions module Find module ClassMethods def find field, search_term - encoded_term = URI::encode(search_term) + encoded_term = URI.encode_www_form_component(search_term) api_url = "#{self.api_url}/#{field}/#{encoded_term}" response = Onfleet.request(api_url, :get, search_term) Util.constantize("#{self}").new(response) From 95feef95ee415f978e42f4a745bba010aeacb8e1 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:29:51 -0400 Subject: [PATCH 13/67] Remove useless variable assignments rubocop: Lint/UselessAssignment --- lib/onfleet-ruby/actions/delete.rb | 2 +- lib/onfleet-ruby/onfleet_object.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/onfleet-ruby/actions/delete.rb b/lib/onfleet-ruby/actions/delete.rb index d804fd8..344b562 100644 --- a/lib/onfleet-ruby/actions/delete.rb +++ b/lib/onfleet-ruby/actions/delete.rb @@ -4,7 +4,7 @@ module Delete module ClassMethods def delete id api_url = "#{self.api_url}/#{id}" - response = Onfleet.request(api_url, :delete) + Onfleet.request(api_url, :delete) true end end diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index a1a4795..9272277 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -25,7 +25,7 @@ def attributes str = var.to_s.gsub(/^@/, '') if respond_to?("#{str}=") instance_var = instance_variable_get(var) - if (klass = Util.object_classes[str]) + if Util.object_classes[str] if instance_var.is_a?(OnfleetObject) attrs[Util.to_camel_case_lower(str).to_sym] = parse_onfleet_obj(instance_var) elsif instance_var.is_a?(Array) From 41d9e5b3c443ce3f5eb68c63fab6a9c0c6c2910e Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:35:28 -0400 Subject: [PATCH 14/67] Replace unnecessary hash merge rubocop: Performance/RedundantMerge --- lib/onfleet-ruby/actions/update.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onfleet-ruby/actions/update.rb b/lib/onfleet-ruby/actions/update.rb index 6f32cc9..5ed7503 100644 --- a/lib/onfleet-ruby/actions/update.rb +++ b/lib/onfleet-ruby/actions/update.rb @@ -3,7 +3,7 @@ module Actions module Update module ClassMethods def update id, params - params.merge!(id: id) + params[:id] = id self.new(params).save end end From 32f770f372f8a5f885a412190afa85b159361297 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:36:33 -0400 Subject: [PATCH 15/67] Prefer boolean operator in condition rubocop: Style/AndOr --- lib/onfleet-ruby.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 6038a05..57363ef 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -51,7 +51,7 @@ def request api_url, method, params = {} JSON.parse(response) end rescue RestClient::ExceptionWithResponse => e - if (response_code = e.http_code) and (response_body = e.http_body) + if (response_code = e.http_code) && (response_body = e.http_body) handle_api_error(response_code, JSON.parse(response_body)) else handle_restclient_error(e) From efc5b1ff47eb2e754fbef0f5af61643fd1de39fa Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:38:41 -0400 Subject: [PATCH 16/67] Enforce consistent brace/paren/etc. style rubocop: - Style/BracesAroundHashParameters - Style/EmptyLiteral - Style/EmptyMethod - Style/MethodDefParentheses --- lib/onfleet-ruby.rb | 6 +++--- lib/onfleet-ruby/actions/create.rb | 4 ++-- lib/onfleet-ruby/actions/delete.rb | 4 ++-- lib/onfleet-ruby/actions/find.rb | 4 ++-- lib/onfleet-ruby/actions/get.rb | 4 ++-- lib/onfleet-ruby/actions/list.rb | 4 ++-- lib/onfleet-ruby/actions/query_metadata.rb | 4 ++-- lib/onfleet-ruby/actions/update.rb | 4 ++-- lib/onfleet-ruby/onfleet_object.rb | 14 +++++++------- lib/onfleet-ruby/organization.rb | 2 +- lib/onfleet-ruby/util.rb | 6 +++--- spec/test_data.rb | 9 +++------ 12 files changed, 31 insertions(+), 34 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 57363ef..bb493c4 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -41,7 +41,7 @@ module Onfleet class << self attr_accessor :api_key, :base_url - def request api_url, method, params = {} + def request(api_url, method, params = {}) raise AuthenticationError.new("Set your API Key using Onfleet.api_key = ") unless @api_key begin @@ -75,7 +75,7 @@ def encoded_api_key @encoded_api_key ||= Base64.urlsafe_encode64(api_key) end - def handle_api_error code, body + def handle_api_error(code, body) case code when 400, 404 raise InvalidRequestError.new(body["message"]) @@ -86,7 +86,7 @@ def handle_api_error code, body end end - def handle_restclient_error e + def handle_restclient_error(e) case e when RestClient::RequestTimeout message = "Could not connect to Onfleet. Check your internet connection and try again." diff --git a/lib/onfleet-ruby/actions/create.rb b/lib/onfleet-ruby/actions/create.rb index f749e98..9ad8181 100644 --- a/lib/onfleet-ruby/actions/create.rb +++ b/lib/onfleet-ruby/actions/create.rb @@ -2,12 +2,12 @@ module Onfleet module Actions module Create module ClassMethods - def create params = {} + def create(params = {}) self.new(params).save end end - def self.included base + def self.included(base) base.extend(ClassMethods) end end diff --git a/lib/onfleet-ruby/actions/delete.rb b/lib/onfleet-ruby/actions/delete.rb index 344b562..56f6227 100644 --- a/lib/onfleet-ruby/actions/delete.rb +++ b/lib/onfleet-ruby/actions/delete.rb @@ -2,14 +2,14 @@ module Onfleet module Actions module Delete module ClassMethods - def delete id + def delete(id) api_url = "#{self.api_url}/#{id}" Onfleet.request(api_url, :delete) true end end - def self.included base + def self.included(base) base.extend(ClassMethods) end end diff --git a/lib/onfleet-ruby/actions/find.rb b/lib/onfleet-ruby/actions/find.rb index e4a4137..15d5071 100644 --- a/lib/onfleet-ruby/actions/find.rb +++ b/lib/onfleet-ruby/actions/find.rb @@ -2,7 +2,7 @@ module Onfleet module Actions module Find module ClassMethods - def find field, search_term + def find(field, search_term) encoded_term = URI.encode_www_form_component(search_term) api_url = "#{self.api_url}/#{field}/#{encoded_term}" response = Onfleet.request(api_url, :get, search_term) @@ -10,7 +10,7 @@ def find field, search_term end end - def self.included base + def self.included(base) base.extend(ClassMethods) end end diff --git a/lib/onfleet-ruby/actions/get.rb b/lib/onfleet-ruby/actions/get.rb index 7feb1b7..9e908e7 100644 --- a/lib/onfleet-ruby/actions/get.rb +++ b/lib/onfleet-ruby/actions/get.rb @@ -2,14 +2,14 @@ module Onfleet module Actions module Get module ClassMethods - def get id + def get(id) api_url = "#{self.api_url}/#{id}" response = Onfleet.request(api_url, :get) Util.constantize("#{self}").new(response) end end - def self.included base + def self.included(base) base.extend(ClassMethods) end end diff --git a/lib/onfleet-ruby/actions/list.rb b/lib/onfleet-ruby/actions/list.rb index fd0ee2f..40eb85f 100644 --- a/lib/onfleet-ruby/actions/list.rb +++ b/lib/onfleet-ruby/actions/list.rb @@ -2,7 +2,7 @@ module Onfleet module Actions module List module ClassMethods - def list query_params = {} + def list(query_params = {}) api_url = "#{self.api_url}" if !query_params.empty? @@ -19,7 +19,7 @@ def list query_params = {} end end - def self.included base + def self.included(base) base.extend(ClassMethods) end end diff --git a/lib/onfleet-ruby/actions/query_metadata.rb b/lib/onfleet-ruby/actions/query_metadata.rb index a4591cd..dd7e7b7 100644 --- a/lib/onfleet-ruby/actions/query_metadata.rb +++ b/lib/onfleet-ruby/actions/query_metadata.rb @@ -2,14 +2,14 @@ module Onfleet module Actions module QueryMetadata module ClassMethods - def query_by_metadata metadata + def query_by_metadata(metadata) api_url = "#{self.api_url}/metadata" response = Onfleet.request(api_url, :post, metadata) response.map { |item| Util.constantize("#{self}").new(item) } if response.is_a? Array end end - def self.included base + def self.included(base) base.extend(ClassMethods) end end diff --git a/lib/onfleet-ruby/actions/update.rb b/lib/onfleet-ruby/actions/update.rb index 5ed7503..43bb033 100644 --- a/lib/onfleet-ruby/actions/update.rb +++ b/lib/onfleet-ruby/actions/update.rb @@ -2,13 +2,13 @@ module Onfleet module Actions module Update module ClassMethods - def update id, params + def update(id, params) params[:id] = id self.new(params).save end end - def self.included base + def self.included(base) base.extend(ClassMethods) end end diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 9272277..fe78e18 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -1,7 +1,7 @@ module Onfleet class OnfleetObject attr_reader :params - def initialize params + def initialize(params) if params.kind_of?(Hash) @params = params set_attributes(@params) @@ -13,14 +13,14 @@ def initialize params end end - def parse_response response + def parse_response(response) @params = response set_attributes(response) self end def attributes - attrs = Hash.new + attrs = {} instance_variables.select { |var| var != '@params' }.each do |var| str = var.to_s.gsub(/^@/, '') if respond_to?("#{str}=") @@ -55,7 +55,7 @@ def api_url private - def parse_onfleet_obj obj + def parse_onfleet_obj(obj) if obj.is_a?(OnfleetObject) if obj.respond_to?('id') && obj.id && (obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task)) obj.id @@ -65,7 +65,7 @@ def parse_onfleet_obj obj end end - def set_attributes params + def set_attributes(params) params.each do |key, value| key_underscore = Util.to_underscore(key) @@ -85,12 +85,12 @@ def set_attributes params if respond_to?("#{key_underscore}=") send(:"#{key_underscore}=", value) else - add_attrs({ "#{key_underscore}" => value }) + add_attrs("#{key_underscore}" => value) end end end - def add_attrs attrs + def add_attrs(attrs) attrs.each do |var, value| self.class.class_eval { attr_accessor var } instance_variable_set "@#{var}", value diff --git a/lib/onfleet-ruby/organization.rb b/lib/onfleet-ruby/organization.rb index e6d4d8c..2814aca 100644 --- a/lib/onfleet-ruby/organization.rb +++ b/lib/onfleet-ruby/organization.rb @@ -7,7 +7,7 @@ def get Util.constantize("#{self}").new(response) end - def get_delegatee_details id + def get_delegatee_details(id) url = "/organizations/#{id}" response = Onfleet.request(url, :get) Util.constantize("#{self}").new(response) diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 2062fba..1b0311b 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -2,11 +2,11 @@ module Onfleet class Util SPECIAL_PARSE = { "skip_sms_notifications" => "skipSMSNotifications" } - def self.constantize class_name + def self.constantize(class_name) Object.const_get(class_name) end - def self.to_underscore key + def self.to_underscore(key) if key.kind_of?(Symbol) key = key.to_s end @@ -18,7 +18,7 @@ def self.to_underscore key .downcase end - def self.to_camel_case_lower str + def self.to_camel_case_lower(str) SPECIAL_PARSE[str] || str.camelize(:lower) end diff --git a/spec/test_data.rb b/spec/test_data.rb index cf946ba..075b786 100644 --- a/spec/test_data.rb +++ b/spec/test_data.rb @@ -1,13 +1,10 @@ module Onfleet module TestData - def recipient - end + def recipient; end - def destination - end + def destination; end - def task - end + def task; end end end From 718b1eb9347d3301b3755b8b89a9a860981df951 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:48:02 -0400 Subject: [PATCH 17/67] Use interpolated strings only when required rubocop: Style/StringLiterals --- Gemfile | 2 +- lib/onfleet-ruby.rb | 16 ++++++++-------- lib/onfleet-ruby/actions/list.rb | 2 +- lib/onfleet-ruby/onfleet_object.rb | 2 +- lib/onfleet-ruby/organization.rb | 2 +- lib/onfleet-ruby/recipient.rb | 2 +- lib/onfleet-ruby/task.rb | 2 +- lib/onfleet-ruby/util.rb | 16 ++++++++-------- onfleet-ruby.gemspec | 6 +++--- 9 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Gemfile b/Gemfile index d502b79..6301dfe 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source "https://rubygems.org" +source 'https://rubygems.org' ruby '2.5.1' diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index bb493c4..fac3025 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -36,13 +36,13 @@ require 'onfleet-ruby/webhook' module Onfleet - @base_url = "https://onfleet.com/api/v2" + @base_url = 'https://onfleet.com/api/v2' class << self attr_accessor :api_key, :base_url def request(api_url, method, params = {}) - raise AuthenticationError.new("Set your API Key using Onfleet.api_key = ") unless @api_key + raise AuthenticationError.new('Set your API Key using Onfleet.api_key = ') unless @api_key begin response = RestClient::Request.execute(method: method, url: base_url + api_url, payload: params.to_json, headers: request_headers) @@ -78,22 +78,22 @@ def encoded_api_key def handle_api_error(code, body) case code when 400, 404 - raise InvalidRequestError.new(body["message"]) + raise InvalidRequestError.new(body['message']) when 401 - raise AuthenticationError.new(body["message"]) + raise AuthenticationError.new(body['message']) else - raise OnfleetError.new(body["message"]) + raise OnfleetError.new(body['message']) end end def handle_restclient_error(e) case e when RestClient::RequestTimeout - message = "Could not connect to Onfleet. Check your internet connection and try again." + message = 'Could not connect to Onfleet. Check your internet connection and try again.' when RestClient::ServerBrokeConnection - message = "The connetion with onfleet terminated before the request completed. Please try again." + message = 'The connetion with onfleet terminated before the request completed. Please try again.' else - message = "There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com" + message = 'There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com' end raise ConnectionError.new(message) diff --git a/lib/onfleet-ruby/actions/list.rb b/lib/onfleet-ruby/actions/list.rb index 40eb85f..33907d1 100644 --- a/lib/onfleet-ruby/actions/list.rb +++ b/lib/onfleet-ruby/actions/list.rb @@ -6,7 +6,7 @@ def list(query_params = {}) api_url = "#{self.api_url}" if !query_params.empty? - api_url += "?" + api_url += '?' query_params.each do |key, value| api_url += "#{key}=#{value}&" end diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index fe78e18..3ff80da 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -46,7 +46,7 @@ def attributes end def class_name - self.class.name.split("::").last + self.class.name.split('::').last end def api_url diff --git a/lib/onfleet-ruby/organization.rb b/lib/onfleet-ruby/organization.rb index 2814aca..7c0077d 100644 --- a/lib/onfleet-ruby/organization.rb +++ b/lib/onfleet-ruby/organization.rb @@ -2,7 +2,7 @@ module Onfleet class Organization < OnfleetObject class << self def get - url = "/organization" + url = '/organization' response = Onfleet.request(url, :get) Util.constantize("#{self}").new(response) end diff --git a/lib/onfleet-ruby/recipient.rb b/lib/onfleet-ruby/recipient.rb index 142faef..5f47530 100644 --- a/lib/onfleet-ruby/recipient.rb +++ b/lib/onfleet-ruby/recipient.rb @@ -8,7 +8,7 @@ class Recipient < OnfleetObject include Onfleet::Actions::QueryMetadata def self.api_url - "/recipients" + '/recipients' end end end diff --git a/lib/onfleet-ruby/task.rb b/lib/onfleet-ruby/task.rb index a2cdc7f..49859b7 100644 --- a/lib/onfleet-ruby/task.rb +++ b/lib/onfleet-ruby/task.rb @@ -15,7 +15,7 @@ def self.api_url def complete # CURRENTLY DOESN'T WORK url = "#{self.url}/#{self.id}/complete" - params = { "completionDetails" => { "success" => true } } + params = { 'completionDetails' => { 'success' => true } } Onfleet.request(url, :post, params) true end diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 1b0311b..3f73edb 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -1,6 +1,6 @@ module Onfleet class Util - SPECIAL_PARSE = { "skip_sms_notifications" => "skipSMSNotifications" } + SPECIAL_PARSE = { 'skip_sms_notifications' => 'skipSMSNotifications' } def self.constantize(class_name) Object.const_get(class_name) @@ -14,7 +14,7 @@ def self.to_underscore(key) .gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') - .tr("-", "_") + .tr('-', '_') .downcase end @@ -24,12 +24,12 @@ def self.to_camel_case_lower(str) def self.object_classes @object_classes ||= { - "address" => Address, - "recipients" => Recipient, - "recipient" => Recipient, - "tasks" => Task, - "destination" => Destination, - "vehicle" => Vehicle + 'address' => Address, + 'recipients' => Recipient, + 'recipient' => Recipient, + 'tasks' => Task, + 'destination' => Destination, + 'vehicle' => Vehicle } end end diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index 1150585..c1d8ed6 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -2,16 +2,16 @@ Gem::Specification.new do |s| s.name = 'onfleet-ruby' s.version = '0.1.4' s.date = '2016-04-08' - s.summary = "Onfleet ruby api" + s.summary = 'Onfleet ruby api' s.description = "To interact with Onfleet's API" - s.authors = ["Nick Wargnier"] + s.authors = ['Nick Wargnier'] s.email = 'nick@stylelend.com' s.homepage = 'http://rubygems.org/gems/onfleet-ruby' s.license = 'MIT' s.add_dependency('rest-client', '~> 1.4') - s.add_development_dependency("rspec", '~> 3.3.0', '>= 3.0.0') + s.add_development_dependency('rspec', '~> 3.3.0', '>= 3.0.0') s.add_development_dependency('rubocop', '~> 0.55') s.files = `git ls-files`.split("\n") From 43f892e61888fce9f397bbce3e6df92d669bd485 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:57:04 -0400 Subject: [PATCH 18/67] Remove unnecessary string interpolation rubocop: Style/UnneededInterpolation --- lib/onfleet-ruby/actions/find.rb | 2 +- lib/onfleet-ruby/actions/get.rb | 2 +- lib/onfleet-ruby/actions/list.rb | 4 ++-- lib/onfleet-ruby/actions/query_metadata.rb | 2 +- lib/onfleet-ruby/onfleet_object.rb | 2 +- lib/onfleet-ruby/organization.rb | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/onfleet-ruby/actions/find.rb b/lib/onfleet-ruby/actions/find.rb index 15d5071..5019ffb 100644 --- a/lib/onfleet-ruby/actions/find.rb +++ b/lib/onfleet-ruby/actions/find.rb @@ -6,7 +6,7 @@ def find(field, search_term) encoded_term = URI.encode_www_form_component(search_term) api_url = "#{self.api_url}/#{field}/#{encoded_term}" response = Onfleet.request(api_url, :get, search_term) - Util.constantize("#{self}").new(response) + Util.constantize(name).new(response) end end diff --git a/lib/onfleet-ruby/actions/get.rb b/lib/onfleet-ruby/actions/get.rb index 9e908e7..e91a42f 100644 --- a/lib/onfleet-ruby/actions/get.rb +++ b/lib/onfleet-ruby/actions/get.rb @@ -5,7 +5,7 @@ module ClassMethods def get(id) api_url = "#{self.api_url}/#{id}" response = Onfleet.request(api_url, :get) - Util.constantize("#{self}").new(response) + Util.constantize(name).new(response) end end diff --git a/lib/onfleet-ruby/actions/list.rb b/lib/onfleet-ruby/actions/list.rb index 33907d1..ff72c51 100644 --- a/lib/onfleet-ruby/actions/list.rb +++ b/lib/onfleet-ruby/actions/list.rb @@ -3,7 +3,7 @@ module Actions module List module ClassMethods def list(query_params = {}) - api_url = "#{self.api_url}" + api_url = self.api_url if !query_params.empty? api_url += '?' @@ -14,7 +14,7 @@ def list(query_params = {}) response = Onfleet.request(api_url, :get) response.compact.map do |listObj| - Util.constantize("#{self}").new(listObj) + Util.constantize(name).new(listObj) end end end diff --git a/lib/onfleet-ruby/actions/query_metadata.rb b/lib/onfleet-ruby/actions/query_metadata.rb index dd7e7b7..9036d12 100644 --- a/lib/onfleet-ruby/actions/query_metadata.rb +++ b/lib/onfleet-ruby/actions/query_metadata.rb @@ -5,7 +5,7 @@ module ClassMethods def query_by_metadata(metadata) api_url = "#{self.api_url}/metadata" response = Onfleet.request(api_url, :post, metadata) - response.map { |item| Util.constantize("#{self}").new(item) } if response.is_a? Array + response.map { |item| Util.constantize(name).new(item) } if response.is_a? Array end end diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 3ff80da..910142c 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -85,7 +85,7 @@ def set_attributes(params) if respond_to?("#{key_underscore}=") send(:"#{key_underscore}=", value) else - add_attrs("#{key_underscore}" => value) + add_attrs(key_underscore.to_s => value) end end end diff --git a/lib/onfleet-ruby/organization.rb b/lib/onfleet-ruby/organization.rb index 7c0077d..8d9efed 100644 --- a/lib/onfleet-ruby/organization.rb +++ b/lib/onfleet-ruby/organization.rb @@ -4,13 +4,13 @@ class << self def get url = '/organization' response = Onfleet.request(url, :get) - Util.constantize("#{self}").new(response) + Util.constantize(name).new(response) end def get_delegatee_details(id) url = "/organizations/#{id}" response = Onfleet.request(url, :get) - Util.constantize("#{self}").new(response) + Util.constantize(name).new(response) end end end From 69ee0a912284afb2568e9a7ecb5083ebdbc487d7 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 19:59:45 -0400 Subject: [PATCH 19/67] Remove redundant uses of self rubocop: Style/RedundantSelf --- lib/onfleet-ruby/actions/create.rb | 2 +- lib/onfleet-ruby/actions/save.rb | 8 ++++---- lib/onfleet-ruby/actions/update.rb | 2 +- lib/onfleet-ruby/task.rb | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/onfleet-ruby/actions/create.rb b/lib/onfleet-ruby/actions/create.rb index 9ad8181..8758cea 100644 --- a/lib/onfleet-ruby/actions/create.rb +++ b/lib/onfleet-ruby/actions/create.rb @@ -3,7 +3,7 @@ module Actions module Create module ClassMethods def create(params = {}) - self.new(params).save + new(params).save end end diff --git a/lib/onfleet-ruby/actions/save.rb b/lib/onfleet-ruby/actions/save.rb index 62b46b1..823e949 100644 --- a/lib/onfleet-ruby/actions/save.rb +++ b/lib/onfleet-ruby/actions/save.rb @@ -2,15 +2,15 @@ module Onfleet module Actions module Save def save - if respond_to?('id') && self.id + if respond_to?('id') && id request_type = :put - api_url = "#{self.api_url}/#{self.id}" + api_url = "#{self.api_url}/#{id}" else request_type = :post api_url = self.api_url end - response = Onfleet.request(api_url, request_type, self.attributes) - self.parse_response(response) + response = Onfleet.request(api_url, request_type, attributes) + parse_response(response) end end end diff --git a/lib/onfleet-ruby/actions/update.rb b/lib/onfleet-ruby/actions/update.rb index 43bb033..5f80dd0 100644 --- a/lib/onfleet-ruby/actions/update.rb +++ b/lib/onfleet-ruby/actions/update.rb @@ -4,7 +4,7 @@ module Update module ClassMethods def update(id, params) params[:id] = id - self.new(params).save + new(params).save end end diff --git a/lib/onfleet-ruby/task.rb b/lib/onfleet-ruby/task.rb index 49859b7..bd47ceb 100644 --- a/lib/onfleet-ruby/task.rb +++ b/lib/onfleet-ruby/task.rb @@ -14,7 +14,7 @@ def self.api_url def complete # CURRENTLY DOESN'T WORK - url = "#{self.url}/#{self.id}/complete" + url = "#{self.url}/#{id}/complete" params = { 'completionDetails' => { 'success' => true } } Onfleet.request(url, :post, params) true From 37a817e1e27c2d910337fd228770dde4ec597a9c Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:04:42 -0400 Subject: [PATCH 20/67] Prefer parameterized method of raising exceptions rubocop: Style/RaiseArgs --- lib/onfleet-ruby.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index fac3025..991027e 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -42,7 +42,7 @@ class << self attr_accessor :api_key, :base_url def request(api_url, method, params = {}) - raise AuthenticationError.new('Set your API Key using Onfleet.api_key = ') unless @api_key + raise(AuthenticationError, 'Set your API Key using Onfleet.api_key = ') unless api_key begin response = RestClient::Request.execute(method: method, url: base_url + api_url, payload: params.to_json, headers: request_headers) @@ -78,11 +78,11 @@ def encoded_api_key def handle_api_error(code, body) case code when 400, 404 - raise InvalidRequestError.new(body['message']) + raise InvalidRequestError, body['message'] when 401 - raise AuthenticationError.new(body['message']) + raise AuthenticationError, body['message'] else - raise OnfleetError.new(body['message']) + raise OnfleetError, body['message'] end end @@ -96,7 +96,7 @@ def handle_restclient_error(e) message = 'There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com' end - raise ConnectionError.new(message) + raise ConnectionError, message end end end From cbcf708ef4095f13d01325b0f84d6f9c2d966cc4 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:07:02 -0400 Subject: [PATCH 21/67] Use `next` to skip iteration rubocop: Style/Next --- lib/onfleet-ruby/onfleet_object.rb | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 910142c..5046689 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -23,23 +23,22 @@ def attributes attrs = {} instance_variables.select { |var| var != '@params' }.each do |var| str = var.to_s.gsub(/^@/, '') - if respond_to?("#{str}=") - instance_var = instance_variable_get(var) - if Util.object_classes[str] - if instance_var.is_a?(OnfleetObject) - attrs[Util.to_camel_case_lower(str).to_sym] = parse_onfleet_obj(instance_var) - elsif instance_var.is_a?(Array) - objs = [] - instance_var.each do |object| - objs << parse_onfleet_obj(object) - end - attrs[Util.to_camel_case_lower(str).to_sym] = objs - else - attrs[Util.to_camel_case_lower(str).to_sym] = instance_var + next unless respond_to?("#{str}=") + instance_var = instance_variable_get(var) + if Util.object_classes[str] + if instance_var.is_a?(OnfleetObject) + attrs[Util.to_camel_case_lower(str).to_sym] = parse_onfleet_obj(instance_var) + elsif instance_var.is_a?(Array) + objs = [] + instance_var.each do |object| + objs << parse_onfleet_obj(object) end + attrs[Util.to_camel_case_lower(str).to_sym] = objs else attrs[Util.to_camel_case_lower(str).to_sym] = instance_var end + else + attrs[Util.to_camel_case_lower(str).to_sym] = instance_var end end attrs From b8a12431a8e3f8a9c95ff3ed652cda979fdb7060 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:08:11 -0400 Subject: [PATCH 22/67] Avoid unnecessarily negated conditions rubocop: Style/NegatedIf --- lib/onfleet-ruby/actions/list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onfleet-ruby/actions/list.rb b/lib/onfleet-ruby/actions/list.rb index ff72c51..7c82068 100644 --- a/lib/onfleet-ruby/actions/list.rb +++ b/lib/onfleet-ruby/actions/list.rb @@ -5,7 +5,7 @@ module ClassMethods def list(query_params = {}) api_url = self.api_url - if !query_params.empty? + if query_params.any? api_url += '?' query_params.each do |key, value| api_url += "#{key}=#{value}&" From aba084a686f7d65fa10da3e52dc012dd6703babb Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:09:01 -0400 Subject: [PATCH 23/67] Freeze mutable objects set as constants rubocop: Style/MutableConstant --- lib/onfleet-ruby/util.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 3f73edb..7d02d3b 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -1,6 +1,6 @@ module Onfleet class Util - SPECIAL_PARSE = { 'skip_sms_notifications' => 'skipSMSNotifications' } + SPECIAL_PARSE = { 'skip_sms_notifications' => 'skipSMSNotifications' }.freeze def self.constantize(class_name) Object.const_get(class_name) From f6237f0cd6bc2a2978f74ea5a11b2f2a3aae9aeb Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:10:09 -0400 Subject: [PATCH 24/67] Replace inverted use of `select` with `reject` rubocop: Style/InverseMethods --- lib/onfleet-ruby/onfleet_object.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 5046689..127f12f 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -21,7 +21,7 @@ def parse_response(response) def attributes attrs = {} - instance_variables.select { |var| var != '@params' }.each do |var| + instance_variables.reject { |var| var == '@params' }.each do |var| str = var.to_s.gsub(/^@/, '') next unless respond_to?("#{str}=") instance_var = instance_variable_get(var) From da65008aa21fb9b86dfe82862e72216be73f582d Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:13:14 -0400 Subject: [PATCH 25/67] Use modifier conditional syntax when applicable rubocop: Style/IfUnlessModifier --- lib/onfleet-ruby.rb | 5 +---- lib/onfleet-ruby/util.rb | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 991027e..d94eec0 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -46,10 +46,7 @@ def request(api_url, method, params = {}) begin response = RestClient::Request.execute(method: method, url: base_url + api_url, payload: params.to_json, headers: request_headers) - - if response != '' - JSON.parse(response) - end + JSON.parse(response) unless response.empty? rescue RestClient::ExceptionWithResponse => e if (response_code = e.http_code) && (response_body = e.http_body) handle_api_error(response_code, JSON.parse(response_body)) diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 7d02d3b..38dff33 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -7,10 +7,8 @@ def self.constantize(class_name) end def self.to_underscore(key) - if key.kind_of?(Symbol) - key = key.to_s - end key + .to_s .gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') From 7e822a2c735e25b69b389b6f02a765fbc20280b8 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:17:48 -0400 Subject: [PATCH 26/67] Add guard clause to method rubocop: Style/GuardClause --- lib/onfleet-ruby/onfleet_object.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 127f12f..a539790 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -55,12 +55,11 @@ def api_url private def parse_onfleet_obj(obj) - if obj.is_a?(OnfleetObject) - if obj.respond_to?('id') && obj.id && (obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task)) - obj.id - else - obj.attributes - end + return unless obj.is_a?(OnfleetObject) + if obj.respond_to?('id') && obj.id && (obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task)) + obj.id + else + obj.attributes end end From 382d66878d1d5466a1f9c273eb0841e110ca5c71 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:20:35 -0400 Subject: [PATCH 27/67] Simplify file path expansion rubocop: Style/ExpandPathArguments --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e155c9e..2af98f4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,3 @@ require 'onfleet-ruby' -require File.expand_path('../test_data', __FILE__) +require File.expand_path('test_data', __dir__) From 2b64f885a4bb80d27ed1d5e07d89ee43ce081c26 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:24:48 -0400 Subject: [PATCH 28/67] Make rubocop accept the main gem filename --- .rubocop.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 614e6c0..2597194 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,3 +2,8 @@ Layout/TrailingBlankLines: EnforcedStyle: final_blank_line +# Allow the gem to have a non-snakecase name +Naming/FileName: + Exclude: + - 'lib/onfleet-ruby.rb' + From 873cd933184c8aeaa10e8a7392473ceaa19753d3 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:28:07 -0400 Subject: [PATCH 29/67] Remove redunant variable assignment rubocop: Style/ConditionalAssignment --- lib/onfleet-ruby.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index d94eec0..2b3f7b7 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -84,14 +84,15 @@ def handle_api_error(code, body) end def handle_restclient_error(e) - case e - when RestClient::RequestTimeout - message = 'Could not connect to Onfleet. Check your internet connection and try again.' - when RestClient::ServerBrokeConnection - message = 'The connetion with onfleet terminated before the request completed. Please try again.' - else - message = 'There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com' - end + message = + case e + when RestClient::RequestTimeout + 'Could not connect to Onfleet. Check your internet connection and try again.' + when RestClient::ServerBrokeConnection + 'The connetion with onfleet terminated before the request completed. Please try again.' + else + 'There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com' + end raise ConnectionError, message end From ac574236d1591cb0e921c46b18682f7a4603fe45 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:29:11 -0400 Subject: [PATCH 30/67] Consistently use #is_a? for class check rubocop: Style/ClassCheck --- lib/onfleet-ruby/onfleet_object.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index a539790..9ff9aac 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -2,10 +2,10 @@ module Onfleet class OnfleetObject attr_reader :params def initialize(params) - if params.kind_of?(Hash) + if params.is_a?(Hash) @params = params set_attributes(@params) - elsif params.kind_of?(String) + elsif params.is_a?(String) @params = { id: params } set_attributes(@params) else From 7b8dc0edc68bf37b6123ea2c0952a778a4e2b6f4 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:30:06 -0400 Subject: [PATCH 31/67] Remove rubocop requirement for JavaDoc-style docs --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 2597194..b20012b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,3 +7,6 @@ Naming/FileName: Exclude: - 'lib/onfleet-ruby.rb' +Style/Documentation: + Enabled: false + From 9564afe9e6c7c357c52a91bbb31b05d28a21f3d3 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:31:59 -0400 Subject: [PATCH 32/67] Consistently use ruby-style variable names rubocop: Naming/VariableName --- lib/onfleet-ruby/actions/list.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/onfleet-ruby/actions/list.rb b/lib/onfleet-ruby/actions/list.rb index 7c82068..2fdce27 100644 --- a/lib/onfleet-ruby/actions/list.rb +++ b/lib/onfleet-ruby/actions/list.rb @@ -13,8 +13,8 @@ def list(query_params = {}) end response = Onfleet.request(api_url, :get) - response.compact.map do |listObj| - Util.constantize(name).new(listObj) + response.compact.map do |list_obj| + Util.constantize(name).new(list_obj) end end end From d8c5fac3d74a1507c8327bbfbee8986719bb3389 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:33:44 -0400 Subject: [PATCH 33/67] Use descriptive method parameter names rubocop: Naming/UncommunicativeMethodParamName --- lib/onfleet-ruby.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 2b3f7b7..74c1dfe 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -83,9 +83,9 @@ def handle_api_error(code, body) end end - def handle_restclient_error(e) + def handle_restclient_error(exception) message = - case e + case exception when RestClient::RequestTimeout 'Could not connect to Onfleet. Check your internet connection and try again.' when RestClient::ServerBrokeConnection From 4814ab90d7cbdf61715ed1217157bdb3e7917909 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:40:25 -0400 Subject: [PATCH 34/67] Rename method to avoid ambiguity with accessor rubocop: Naming/AccessorMethodName --- lib/onfleet-ruby/onfleet_object.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 9ff9aac..6731260 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -4,10 +4,10 @@ class OnfleetObject def initialize(params) if params.is_a?(Hash) @params = params - set_attributes(@params) + assign_attributes(@params) elsif params.is_a?(String) @params = { id: params } - set_attributes(@params) + assign_attributes(@params) else @params = {} end @@ -15,7 +15,7 @@ def initialize(params) def parse_response(response) @params = response - set_attributes(response) + assign_attributes(response) self end @@ -63,7 +63,7 @@ def parse_onfleet_obj(obj) end end - def set_attributes(params) + def assign_attributes(params) params.each do |key, value| key_underscore = Util.to_underscore(key) From 2b693e8a184153755529b1ff8ef56f79b670b48e Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:42:57 -0400 Subject: [PATCH 35/67] Creating TODO file for remaining rubocop issues --- .rubocop.yml | 2 ++ .rubocop_todo.yml | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index b20012b..0065049 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,5 @@ +inherit_from: .rubocop_todo.yml + # Keep diffs clean Layout/TrailingBlankLines: EnforcedStyle: final_blank_line diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..996ac2c --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,38 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2018-05-13 20:30:59 -0400 using RuboCop version 0.55.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 2 +Metrics/AbcSize: + Max: 26 + +# Offense count: 1 +Metrics/CyclomaticComplexity: + Max: 7 + +# Offense count: 4 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 22 + +# Offense count: 1 +Metrics/PerceivedComplexity: + Max: 8 + +# Offense count: 29 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: when_needed, always, never +Style/FrozenStringLiteralComment: + Enabled: false + +# Offense count: 9 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Max: 138 + From a079b931ae819e0d4c6f3e7cb885a0f5d1d79b34 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 20:54:11 -0400 Subject: [PATCH 36/67] Add rake task for rubocop, and include in default --- Rakefile | 6 ++++++ onfleet-ruby.gemspec | 1 + 2 files changed, 7 insertions(+) create mode 100644 Rakefile diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..cc73edc --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +require 'bundler/gem_tasks' +require 'rubocop/rake_task' +RuboCop::RakeTask.new + +task default: :rubocop + diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index c1d8ed6..6efd0fb 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -11,6 +11,7 @@ Gem::Specification.new do |s| s.add_dependency('rest-client', '~> 1.4') + s.add_development_dependency('rake') s.add_development_dependency('rspec', '~> 3.3.0', '>= 3.0.0') s.add_development_dependency('rubocop', '~> 0.55') From f71e1991d3ce81b8a078f23694ded95ffbcb92d0 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 00:03:53 -0400 Subject: [PATCH 37/67] Specify old-ish ruby (2.2) for rubocop --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 0065049..7e50208 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,8 @@ inherit_from: .rubocop_todo.yml +AllCops: + TargetRubyVersion: 2.2 + # Keep diffs clean Layout/TrailingBlankLines: EnforcedStyle: final_blank_line From 24d32ab16070afe7530050bc0f1d92992f45d7a2 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:33:00 -0400 Subject: [PATCH 38/67] Remove redundant gem dependency --- Gemfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Gemfile b/Gemfile index 6301dfe..12f6e01 100644 --- a/Gemfile +++ b/Gemfile @@ -3,5 +3,4 @@ source 'https://rubygems.org' ruby '2.5.1' gemspec -gem 'rest-client', '~> 1.6.8' From fd5fe47522cdcb8b8c3dde05a515ca4b6734d58e Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:41:51 -0400 Subject: [PATCH 39/67] Add missing gem dependency --- lib/onfleet-ruby/util.rb | 2 ++ onfleet-ruby.gemspec | 1 + 2 files changed, 3 insertions(+) diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 38dff33..c0a24fa 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/string/inflections' + module Onfleet class Util SPECIAL_PARSE = { 'skip_sms_notifications' => 'skipSMSNotifications' }.freeze diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index 6efd0fb..c8ed079 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -9,6 +9,7 @@ Gem::Specification.new do |s| s.homepage = 'http://rubygems.org/gems/onfleet-ruby' s.license = 'MIT' + s.add_dependency('activesupport', '>= 4.2') s.add_dependency('rest-client', '~> 1.4') s.add_development_dependency('rake') From 92610d2a5e7547ea5e951015cad2419161d5318e Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 21:04:35 -0400 Subject: [PATCH 40/67] Add rspec with default configuration --- .gitignore | 3 ++ .rspec | 3 +- onfleet-ruby.gemspec | 2 +- spec/spec_helper.rb | 71 ++++++++++++++++++++++++++++++++++++++++++-- spec/test_data.rb | 10 ------- 5 files changed, 74 insertions(+), 15 deletions(-) delete mode 100644 spec/test_data.rb diff --git a/.gitignore b/.gitignore index 94bb6bb..c3ebfa2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ # Ignore bundler config /.bundle Gemfile.lock + +spec/examples.txt + diff --git a/.rspec b/.rspec index b3eb8b4..c99d2e7 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1 @@ ---color ---format documentation \ No newline at end of file +--require spec_helper diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index c8ed079..1051995 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |s| s.add_dependency('rest-client', '~> 1.4') s.add_development_dependency('rake') - s.add_development_dependency('rspec', '~> 3.3.0', '>= 3.0.0') + s.add_development_dependency('rspec', '~> 3.3') s.add_development_dependency('rubocop', '~> 0.55') s.files = `git ls-files`.split("\n") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2af98f4..c146d1c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,70 @@ -require 'onfleet-ruby' -require File.expand_path('test_data', __dir__) +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = 'spec/examples.txt' + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +end diff --git a/spec/test_data.rb b/spec/test_data.rb deleted file mode 100644 index 075b786..0000000 --- a/spec/test_data.rb +++ /dev/null @@ -1,10 +0,0 @@ -module Onfleet - module TestData - def recipient; end - - def destination; end - - def task; end - end -end - From f69610da771388f116d1f6e13deececd4aca2744 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 21:41:20 -0400 Subject: [PATCH 41/67] Add rake task for specs, and add to default --- Rakefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index cc73edc..22deda5 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,10 @@ require 'bundler/gem_tasks' + require 'rubocop/rake_task' RuboCop::RakeTask.new -task default: :rubocop +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) + +task default: %i[spec rubocop] From ac5277717a85cbf01fc86062a16148c055974f72 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 21:42:25 -0400 Subject: [PATCH 42/67] Require project files in specs --- spec/spec_helper.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c146d1c..9b1f756 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +require File.expand_path(File.join('..', 'lib', 'onfleet-ruby'), __dir__) + # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| config.expect_with :rspec do |expectations| From d5e3e899cfde1626f2794c024bc78f1edc397e5f Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 21:42:59 -0400 Subject: [PATCH 43/67] Add webmock gem for network testing --- onfleet-ruby.gemspec | 1 + spec/spec_helper.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index 1051995..b786f0c 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -15,6 +15,7 @@ Gem::Specification.new do |s| s.add_development_dependency('rake') s.add_development_dependency('rspec', '~> 3.3') s.add_development_dependency('rubocop', '~> 0.55') + s.add_development_dependency('webmock', '~> 3.4') s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9b1f756..6c6777c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,4 @@ +require 'webmock/rspec' require File.expand_path(File.join('..', 'lib', 'onfleet-ruby'), __dir__) # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration From f3870966e3668d57c58bbfac5dd44df68f22c323 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 22:08:09 -0400 Subject: [PATCH 44/67] Use URI.join to build URLs Rather than string interpolation or concatenation. --- lib/onfleet-ruby.rb | 5 +++-- lib/onfleet-ruby/admin.rb | 2 +- lib/onfleet-ruby/destination.rb | 2 +- lib/onfleet-ruby/onfleet_object.rb | 2 +- lib/onfleet-ruby/organization.rb | 4 ++-- lib/onfleet-ruby/recipient.rb | 2 +- lib/onfleet-ruby/task.rb | 2 +- lib/onfleet-ruby/team.rb | 2 +- lib/onfleet-ruby/webhook.rb | 2 +- lib/onfleet-ruby/worker.rb | 2 +- 10 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index 74c1dfe..a889d60 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -36,7 +36,7 @@ require 'onfleet-ruby/webhook' module Onfleet - @base_url = 'https://onfleet.com/api/v2' + @base_url = 'https://onfleet.com/api/v2/' class << self attr_accessor :api_key, :base_url @@ -45,7 +45,8 @@ def request(api_url, method, params = {}) raise(AuthenticationError, 'Set your API Key using Onfleet.api_key = ') unless api_key begin - response = RestClient::Request.execute(method: method, url: base_url + api_url, payload: params.to_json, headers: request_headers) + url = URI.join(base_url, api_url).to_s + response = RestClient::Request.execute(method: method, url: url, payload: params.to_json, headers: request_headers) JSON.parse(response) unless response.empty? rescue RestClient::ExceptionWithResponse => e if (response_code = e.http_code) && (response_body = e.http_body) diff --git a/lib/onfleet-ruby/admin.rb b/lib/onfleet-ruby/admin.rb index 6a23c0d..887c308 100644 --- a/lib/onfleet-ruby/admin.rb +++ b/lib/onfleet-ruby/admin.rb @@ -8,7 +8,7 @@ class Admin < OnfleetObject include Onfleet::Actions::QueryMetadata def self.api_url - '/admins' + 'admins' end end end diff --git a/lib/onfleet-ruby/destination.rb b/lib/onfleet-ruby/destination.rb index 39cb511..0aa0309 100644 --- a/lib/onfleet-ruby/destination.rb +++ b/lib/onfleet-ruby/destination.rb @@ -6,7 +6,7 @@ class Destination < OnfleetObject include Onfleet::Actions::QueryMetadata def self.api_url - '/destinations' + 'destinations' end end end diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 6731260..22c2696 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -49,7 +49,7 @@ def class_name end def api_url - "/#{CGI.escape(class_name.downcase)}s" + "#{CGI.escape(class_name.downcase)}s" end private diff --git a/lib/onfleet-ruby/organization.rb b/lib/onfleet-ruby/organization.rb index 8d9efed..f4e2da5 100644 --- a/lib/onfleet-ruby/organization.rb +++ b/lib/onfleet-ruby/organization.rb @@ -2,13 +2,13 @@ module Onfleet class Organization < OnfleetObject class << self def get - url = '/organization' + url = 'organization' response = Onfleet.request(url, :get) Util.constantize(name).new(response) end def get_delegatee_details(id) - url = "/organizations/#{id}" + url = "organizations/#{id}" response = Onfleet.request(url, :get) Util.constantize(name).new(response) end diff --git a/lib/onfleet-ruby/recipient.rb b/lib/onfleet-ruby/recipient.rb index 5f47530..5041cb5 100644 --- a/lib/onfleet-ruby/recipient.rb +++ b/lib/onfleet-ruby/recipient.rb @@ -8,7 +8,7 @@ class Recipient < OnfleetObject include Onfleet::Actions::QueryMetadata def self.api_url - '/recipients' + 'recipients' end end end diff --git a/lib/onfleet-ruby/task.rb b/lib/onfleet-ruby/task.rb index bd47ceb..46749e5 100644 --- a/lib/onfleet-ruby/task.rb +++ b/lib/onfleet-ruby/task.rb @@ -9,7 +9,7 @@ class Task < OnfleetObject include Onfleet::Actions::QueryMetadata def self.api_url - '/tasks' + 'tasks' end def complete diff --git a/lib/onfleet-ruby/team.rb b/lib/onfleet-ruby/team.rb index d105ff6..9a75350 100644 --- a/lib/onfleet-ruby/team.rb +++ b/lib/onfleet-ruby/team.rb @@ -4,7 +4,7 @@ class Team < OnfleetObject include Onfleet::Actions::Get def self.api_url - '/teams' + 'teams' end end end diff --git a/lib/onfleet-ruby/webhook.rb b/lib/onfleet-ruby/webhook.rb index fd45dcf..611640f 100644 --- a/lib/onfleet-ruby/webhook.rb +++ b/lib/onfleet-ruby/webhook.rb @@ -6,7 +6,7 @@ class Webhook < OnfleetObject include Onfleet::Actions::Delete def self.api_url - '/webhooks' + 'webhooks' end end end diff --git a/lib/onfleet-ruby/worker.rb b/lib/onfleet-ruby/worker.rb index d6b633c..2485cee 100644 --- a/lib/onfleet-ruby/worker.rb +++ b/lib/onfleet-ruby/worker.rb @@ -9,7 +9,7 @@ class Worker < OnfleetObject include Onfleet::Actions::QueryMetadata def self.api_url - '/workers' + 'workers' end end end From 42f29600cb716cc054271f8f876cdfe33e69edb1 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 22:43:06 -0400 Subject: [PATCH 45/67] Add basic test for HTTP requests --- .rubocop.yml | 9 ++++++ spec/onfleet/organization_spec.rb | 48 +++++++++++++++++++++++++++++++ spec/spec_helper.rb | 2 ++ 3 files changed, 59 insertions(+) create mode 100644 spec/onfleet/organization_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index 7e50208..4f67231 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,6 +7,11 @@ AllCops: Layout/TrailingBlankLines: EnforcedStyle: final_blank_line +# No way to avoid large blocks in RSpec +Metrics/BlockLength: + Exclude: + - spec/**/* + # Allow the gem to have a non-snakecase name Naming/FileName: Exclude: @@ -15,3 +20,7 @@ Naming/FileName: Style/Documentation: Enabled: false +Style/StringLiterals: + Exclude: + - spec/**/* + diff --git a/spec/onfleet/organization_spec.rb b/spec/onfleet/organization_spec.rb new file mode 100644 index 0000000..7258429 --- /dev/null +++ b/spec/onfleet/organization_spec.rb @@ -0,0 +1,48 @@ +RSpec.describe Onfleet::Organization do + let(:organization) { described_class.new(params) } + let(:params) { { id: 'an-org' } } + + describe ".get" do + subject { -> { described_class.get } } + let(:url) { URI.join(Onfleet.base_url, 'organization').to_s } + let(:response) { { status: 200, body: params.to_json } } + before { stub_request(:get, url).to_return(response) } + + it "should include the base64-encoded API key in the auth header" do + encoded_api_key = Base64.urlsafe_encode64(Onfleet.api_key) + + subject.call + expect( + a_request(:get, url).with(headers: { 'Authorization' => "Basic #{encoded_api_key}" }) + ).to have_been_made.once + end + + it "should specify that it will accept JSON" do + subject.call + expect( + a_request(:get, url).with(headers: { 'Accept' => 'application/json' }) + ).to have_been_made.once + end + + context "without valid authentication" do + let(:response) { { status: 401, body: { message: 'bad auth' }.to_json } } + it { should raise_error(Onfleet::AuthenticationError) } + end + + context "without valid authorization" do + let(:response) { { status: 404, body: { message: 'bad auth' }.to_json } } + it { should raise_error(Onfleet::InvalidRequestError) } + end + + context "when an unspecified error occurs" do + let(:response) { { status: 500, body: { message: 'all bad' }.to_json } } + it { should raise_error(Onfleet::OnfleetError) } + end + end + + describe "#id" do + subject { organization.id } + it { should == params[:id] } + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6c6777c..ac984a0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -69,5 +69,7 @@ # test failures related to randomization by passing the same `--seed` value # as the one that triggered the failure. Kernel.srand config.seed + + config.before { Onfleet.api_key = 'TEST API KEY' } end From 0641421e6468c4d97b4fa7a562f4bf17a02d562c Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 23:08:29 -0400 Subject: [PATCH 46/67] Extract common HTTP request test behavior --- spec/onfleet/organization_spec.rb | 39 +++------------- spec/spec_helper.rb | 2 + spec/support/http_requests/shared_examples.rb | 46 +++++++++++++++++++ 3 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 spec/support/http_requests/shared_examples.rb diff --git a/spec/onfleet/organization_spec.rb b/spec/onfleet/organization_spec.rb index 7258429..eccdc94 100644 --- a/spec/onfleet/organization_spec.rb +++ b/spec/onfleet/organization_spec.rb @@ -4,40 +4,13 @@ describe ".get" do subject { -> { described_class.get } } - let(:url) { URI.join(Onfleet.base_url, 'organization').to_s } - let(:response) { { status: 200, body: params.to_json } } - before { stub_request(:get, url).to_return(response) } - - it "should include the base64-encoded API key in the auth header" do - encoded_api_key = Base64.urlsafe_encode64(Onfleet.api_key) - - subject.call - expect( - a_request(:get, url).with(headers: { 'Authorization' => "Basic #{encoded_api_key}" }) - ).to have_been_made.once - end - - it "should specify that it will accept JSON" do - subject.call - expect( - a_request(:get, url).with(headers: { 'Accept' => 'application/json' }) - ).to have_been_made.once - end - - context "without valid authentication" do - let(:response) { { status: 401, body: { message: 'bad auth' }.to_json } } - it { should raise_error(Onfleet::AuthenticationError) } - end - - context "without valid authorization" do - let(:response) { { status: 404, body: { message: 'bad auth' }.to_json } } - it { should raise_error(Onfleet::InvalidRequestError) } - end + it_should_behave_like "an action that makes a request to Onfleet", path: 'organization' + end - context "when an unspecified error occurs" do - let(:response) { { status: 500, body: { message: 'all bad' }.to_json } } - it { should raise_error(Onfleet::OnfleetError) } - end + describe ".get_delegatee_details" do + subject { -> { described_class.get_delegatee_details(id) } } + let(:id) { 'my-org' } + it_should_behave_like "an action that makes a request to Onfleet", path: 'organizations/my-org' end describe "#id" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ac984a0..faecc05 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,8 @@ require 'webmock/rspec' require File.expand_path(File.join('..', 'lib', 'onfleet-ruby'), __dir__) +Dir.glob(File.expand_path(File.join('support', '**', '*.rb'), __dir__)).each { |file| require file } + # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| config.expect_with :rspec do |expectations| diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb new file mode 100644 index 0000000..36ebf57 --- /dev/null +++ b/spec/support/http_requests/shared_examples.rb @@ -0,0 +1,46 @@ +RSpec.shared_examples_for "an action that makes a request to Onfleet" do |path:, method: :get| + let(:url) { URI.join(Onfleet.base_url, path).to_s } + let(:response) { { status: 200, body: { id: 'an-object' }.to_json } } + before { stub_request(method, url).to_return(response) } + + it "should include the base64-encoded API key in the auth header" do + encoded_api_key = Base64.urlsafe_encode64(Onfleet.api_key) + + subject.call + expect( + a_request(method, url).with(headers: { 'Authorization' => "Basic #{encoded_api_key}" }) + ).to have_been_made.once + end + + it "should specify that it will accept JSON" do + subject.call + expect( + a_request(method, url).with(headers: { 'Accept' => 'application/json' }) + ).to have_been_made.once + end + + if %i[post put patch].include?(method.to_sym) + it "should set the content type to JSON" do + subject.call + expect( + a_request(method, url).with(headers: { 'Content-Type' => 'application/json' }) + ).to have_been_made.once + end + end + + context "without valid authentication" do + let(:response) { { status: 401, body: { message: 'bad auth' }.to_json } } + it { should raise_error(Onfleet::AuthenticationError) } + end + + context "without valid authorization" do + let(:response) { { status: 404, body: { message: 'bad auth' }.to_json } } + it { should raise_error(Onfleet::InvalidRequestError) } + end + + context "when an unspecified error occurs" do + let(:response) { { status: 500, body: { message: 'all bad' }.to_json } } + it { should raise_error(Onfleet::OnfleetError) } + end +end + From f1ca55dd7d088de3ada3d635236fdf6e1d1d30f0 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 23:14:57 -0400 Subject: [PATCH 47/67] Simplify Organization creation methods --- lib/onfleet-ruby/organization.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/onfleet-ruby/organization.rb b/lib/onfleet-ruby/organization.rb index f4e2da5..c67b512 100644 --- a/lib/onfleet-ruby/organization.rb +++ b/lib/onfleet-ruby/organization.rb @@ -2,15 +2,11 @@ module Onfleet class Organization < OnfleetObject class << self def get - url = 'organization' - response = Onfleet.request(url, :get) - Util.constantize(name).new(response) + new(Onfleet.request('organization', :get)) end def get_delegatee_details(id) - url = "organizations/#{id}" - response = Onfleet.request(url, :get) - Util.constantize(name).new(response) + new(Onfleet.request("organizations/#{id}", :get)) end end end From b60431523518f27a41839595a762f2d72ad2dc08 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Sun, 13 May 2018 23:33:38 -0400 Subject: [PATCH 48/67] Add basic specs for Organization attributes --- spec/onfleet/organization_spec.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spec/onfleet/organization_spec.rb b/spec/onfleet/organization_spec.rb index eccdc94..31df63c 100644 --- a/spec/onfleet/organization_spec.rb +++ b/spec/onfleet/organization_spec.rb @@ -13,9 +13,13 @@ it_should_behave_like "an action that makes a request to Onfleet", path: 'organizations/my-org' end - describe "#id" do - subject { organization.id } - it { should == params[:id] } + %i[id name email country timezone time_created time_last_modified].each do |attr| + describe "##{attr}" do + subject { organization.public_send(attr) } + let(:params) { { attr => value } } + let(:value) { 'pizza' } + it { should == value } + end end end From 560498b7fcb13af5d6983eba431f0b204ed0642b Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 09:54:44 -0400 Subject: [PATCH 49/67] Specialize shared examples for different requests --- spec/onfleet/organization_spec.rb | 4 ++-- spec/support/http_requests/shared_examples.rb | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/spec/onfleet/organization_spec.rb b/spec/onfleet/organization_spec.rb index 31df63c..efc3069 100644 --- a/spec/onfleet/organization_spec.rb +++ b/spec/onfleet/organization_spec.rb @@ -4,13 +4,13 @@ describe ".get" do subject { -> { described_class.get } } - it_should_behave_like "an action that makes a request to Onfleet", path: 'organization' + it_should_behave_like Onfleet::Actions::Get, path: 'organization' end describe ".get_delegatee_details" do subject { -> { described_class.get_delegatee_details(id) } } let(:id) { 'my-org' } - it_should_behave_like "an action that makes a request to Onfleet", path: 'organizations/my-org' + it_should_behave_like Onfleet::Actions::Get, path: 'organizations/my-org' end %i[id name email country timezone time_created time_last_modified].each do |attr| diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb index 36ebf57..05b09d7 100644 --- a/spec/support/http_requests/shared_examples.rb +++ b/spec/support/http_requests/shared_examples.rb @@ -1,6 +1,6 @@ -RSpec.shared_examples_for "an action that makes a request to Onfleet" do |path:, method: :get| +RSpec.shared_examples_for "an action that makes a request to Onfleet" do |path:, method:| let(:url) { URI.join(Onfleet.base_url, path).to_s } - let(:response) { { status: 200, body: { id: 'an-object' }.to_json } } + let(:response) { { status: 200, body: response_body.to_json } } before { stub_request(method, url).to_return(response) } it "should include the base64-encoded API key in the auth header" do @@ -44,3 +44,8 @@ end end +RSpec.shared_examples_for Onfleet::Actions::Get do |path:| + let(:response_body) { { id: 'an-object' } } + it_should_behave_like "an action that makes a request to Onfleet", path: path, method: :get +end + From 86b00235b549bbe92e56a358320b42f075353069 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:04:25 -0400 Subject: [PATCH 50/67] Add specs for API list behavior --- lib/onfleet-ruby/actions/list.rb | 25 +++++++------- spec/onfleet/admin_spec.rb | 33 +++++++++++++++++++ spec/support/http_requests/shared_examples.rb | 5 +++ 3 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 spec/onfleet/admin_spec.rb diff --git a/lib/onfleet-ruby/actions/list.rb b/lib/onfleet-ruby/actions/list.rb index 2fdce27..6bd10e3 100644 --- a/lib/onfleet-ruby/actions/list.rb +++ b/lib/onfleet-ruby/actions/list.rb @@ -2,20 +2,21 @@ module Onfleet module Actions module List module ClassMethods - def list(query_params = {}) - api_url = self.api_url + def list(filters = {}) + response = Onfleet.request(list_url_for(filters), :get) + response.compact.map { |item| new(item) } + end + + private - if query_params.any? - api_url += '?' - query_params.each do |key, value| - api_url += "#{key}=#{value}&" - end - end + def list_url_for(filters) + [api_url, query_params(filters)].compact.join('?') + end - response = Onfleet.request(api_url, :get) - response.compact.map do |list_obj| - Util.constantize(name).new(list_obj) - end + def query_params(filters) + filters && filters + .collect { |key, value| "#{key}=#{URI.encode_www_form_component(value)}" } + .join('&') end end diff --git a/spec/onfleet/admin_spec.rb b/spec/onfleet/admin_spec.rb new file mode 100644 index 0000000..d2c7cbe --- /dev/null +++ b/spec/onfleet/admin_spec.rb @@ -0,0 +1,33 @@ +RSpec.describe Onfleet::Admin do + let(:admin) { described_class.new(params) } + let(:params) { { name: 'an admin' } } + + describe ".list" do + subject { -> { described_class.list(query_params) } } + + context "with no filter" do + let(:query_params) { nil } + it_should_behave_like Onfleet::Actions::List, path: 'admins' + end + + context "with query params" do + let(:query_params) { { food: 'pizza', topping: 'mushroom' } } + it_should_behave_like Onfleet::Actions::List, path: 'admins?food=pizza&topping=mushroom' + end + + context "with a URL-unsafe query param" do + let(:query_params) { { food: 'green eggs & ham' } } + it_should_behave_like Onfleet::Actions::List, path: 'admins?food=green+eggs+%26+ham' + end + end + + %i[id name email type metadata].each do |attr| + describe "##{attr}" do + subject { admin.public_send(attr) } + let(:params) { { attr => value } } + let(:value) { 'pizza' } + it { should == value } + end + end +end + diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb index 05b09d7..37dddc8 100644 --- a/spec/support/http_requests/shared_examples.rb +++ b/spec/support/http_requests/shared_examples.rb @@ -49,3 +49,8 @@ it_should_behave_like "an action that makes a request to Onfleet", path: path, method: :get end +RSpec.shared_examples_for Onfleet::Actions::List do |path:| + let(:response_body) { [{ id: 'an-object' }, { id: 'another-object' }] } + it_should_behave_like "an action that makes a request to Onfleet", path: path, method: :get +end + From 00ba2e1f48d01be2df38938f3b36ef6ad279e05e Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:04:25 -0400 Subject: [PATCH 51/67] Add specs for API create behavior --- lib/onfleet-ruby/actions/create.rb | 4 ++- spec/onfleet/admin_spec.rb | 7 +++- spec/support/http_requests/shared_examples.rb | 32 +++++++++++++++---- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/onfleet-ruby/actions/create.rb b/lib/onfleet-ruby/actions/create.rb index 8758cea..d5db07c 100644 --- a/lib/onfleet-ruby/actions/create.rb +++ b/lib/onfleet-ruby/actions/create.rb @@ -1,9 +1,11 @@ +require 'active_support/core_ext/hash' + module Onfleet module Actions module Create module ClassMethods def create(params = {}) - new(params).save + new(params.symbolize_keys.except(:id)).save end end diff --git a/spec/onfleet/admin_spec.rb b/spec/onfleet/admin_spec.rb index d2c7cbe..189a05d 100644 --- a/spec/onfleet/admin_spec.rb +++ b/spec/onfleet/admin_spec.rb @@ -1,6 +1,6 @@ RSpec.describe Onfleet::Admin do let(:admin) { described_class.new(params) } - let(:params) { { name: 'an admin' } } + let(:params) { { id: 'an-admin', name: 'An Admin' } } describe ".list" do subject { -> { described_class.list(query_params) } } @@ -21,6 +21,11 @@ end end + describe ".create" do + subject { -> { described_class.create(params) } } + it_should_behave_like Onfleet::Actions::Create, path: 'admins' + end + %i[id name email type metadata].each do |attr| describe "##{attr}" do subject { admin.public_send(attr) } diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb index 37dddc8..a6804de 100644 --- a/spec/support/http_requests/shared_examples.rb +++ b/spec/support/http_requests/shared_examples.rb @@ -1,8 +1,4 @@ -RSpec.shared_examples_for "an action that makes a request to Onfleet" do |path:, method:| - let(:url) { URI.join(Onfleet.base_url, path).to_s } - let(:response) { { status: 200, body: response_body.to_json } } - before { stub_request(method, url).to_return(response) } - +RSpec.shared_examples_for "an action that makes a request to Onfleet" do |method:| it "should include the base64-encoded API key in the auth header" do encoded_api_key = Base64.urlsafe_encode64(Onfleet.api_key) @@ -45,12 +41,34 @@ end RSpec.shared_examples_for Onfleet::Actions::Get do |path:| + set_up_request_stub(:get, path) let(:response_body) { { id: 'an-object' } } - it_should_behave_like "an action that makes a request to Onfleet", path: path, method: :get + it_should_behave_like "an action that makes a request to Onfleet", method: :get end RSpec.shared_examples_for Onfleet::Actions::List do |path:| + set_up_request_stub(:get, path) let(:response_body) { [{ id: 'an-object' }, { id: 'another-object' }] } - it_should_behave_like "an action that makes a request to Onfleet", path: path, method: :get + it_should_behave_like "an action that makes a request to Onfleet", method: :get +end + +RSpec.shared_examples_for Onfleet::Actions::Create do |path:| + set_up_request_stub(:post, path) + let(:response_body) { { id: 'an-object' } } + + it_should_behave_like "an action that makes a request to Onfleet", method: :post + + it "should send the object params, not including ID, in JSON" do + subject.call + expect( + a_request(:post, url).with(body: params.symbolize_keys.except(:id).to_json) + ).to have_been_made.once + end +end + +def set_up_request_stub(method, path) + let(:url) { URI.join(Onfleet.base_url, path).to_s } + let(:response) { { status: 200, body: response_body.to_json } } + before { stub_request(method, url).to_return(response) } end From 8971fa7b8a51c3ee62933156ca83c3c949c9d746 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:04:25 -0400 Subject: [PATCH 52/67] Add specs for API delete behavior --- lib/onfleet-ruby/actions/delete.rb | 3 +-- spec/onfleet/admin_spec.rb | 6 ++++++ spec/support/http_requests/shared_examples.rb | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/onfleet-ruby/actions/delete.rb b/lib/onfleet-ruby/actions/delete.rb index 56f6227..e9f4a44 100644 --- a/lib/onfleet-ruby/actions/delete.rb +++ b/lib/onfleet-ruby/actions/delete.rb @@ -3,8 +3,7 @@ module Actions module Delete module ClassMethods def delete(id) - api_url = "#{self.api_url}/#{id}" - Onfleet.request(api_url, :delete) + Onfleet.request("#{api_url}/#{id}", :delete) true end end diff --git a/spec/onfleet/admin_spec.rb b/spec/onfleet/admin_spec.rb index 189a05d..2329af6 100644 --- a/spec/onfleet/admin_spec.rb +++ b/spec/onfleet/admin_spec.rb @@ -26,6 +26,12 @@ it_should_behave_like Onfleet::Actions::Create, path: 'admins' end + describe ".delete" do + subject { -> { described_class.delete(id) } } + let(:id) { 'an-admin' } + it_should_behave_like Onfleet::Actions::Delete, path: 'admins/an-admin' + end + %i[id name email type metadata].each do |attr| describe "##{attr}" do subject { admin.public_send(attr) } diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb index a6804de..0865479 100644 --- a/spec/support/http_requests/shared_examples.rb +++ b/spec/support/http_requests/shared_examples.rb @@ -66,6 +66,13 @@ end end +RSpec.shared_examples_for Onfleet::Actions::Delete do |path:| + set_up_request_stub(:delete, path) + let(:response_body) { '' } + + it_should_behave_like "an action that makes a request to Onfleet", method: :delete +end + def set_up_request_stub(method, path) let(:url) { URI.join(Onfleet.base_url, path).to_s } let(:response) { { status: 200, body: response_body.to_json } } From 3d0ca01ac5686a35d1ff94643c671a95925dfc42 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:04:25 -0400 Subject: [PATCH 53/67] Add specs for API update behavior --- lib/onfleet-ruby/actions/update.rb | 3 +-- spec/onfleet/admin_spec.rb | 6 ++++++ spec/support/http_requests/shared_examples.rb | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/onfleet-ruby/actions/update.rb b/lib/onfleet-ruby/actions/update.rb index 5f80dd0..bcb3974 100644 --- a/lib/onfleet-ruby/actions/update.rb +++ b/lib/onfleet-ruby/actions/update.rb @@ -3,8 +3,7 @@ module Actions module Update module ClassMethods def update(id, params) - params[:id] = id - new(params).save + new(params.merge(id: id)).save end end diff --git a/spec/onfleet/admin_spec.rb b/spec/onfleet/admin_spec.rb index 2329af6..395ec8d 100644 --- a/spec/onfleet/admin_spec.rb +++ b/spec/onfleet/admin_spec.rb @@ -26,6 +26,12 @@ it_should_behave_like Onfleet::Actions::Create, path: 'admins' end + describe ".update" do + subject { -> { described_class.update(id, params) } } + let(:id) { 'an-admin' } + it_should_behave_like Onfleet::Actions::Update, path: 'admins/an-admin' + end + describe ".delete" do subject { -> { described_class.delete(id) } } let(:id) { 'an-admin' } diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb index 0865479..bab0999 100644 --- a/spec/support/http_requests/shared_examples.rb +++ b/spec/support/http_requests/shared_examples.rb @@ -66,6 +66,22 @@ end end +RSpec.shared_examples_for Onfleet::Actions::Update do |path:| + set_up_request_stub(:put, path) + let(:response_body) { { id: 'an-object' } } + + it_should_behave_like "an action that makes a request to Onfleet", method: :put + + # The current implementation -- using instance variables -- makes it impossible + # to have this example pass deterministically. + xit "should send the object params, including ID, in JSON" do + subject.call + expect( + a_request(:put, url).with(body: params.merge(id: id).to_json) + ).to have_been_made.once + end +end + RSpec.shared_examples_for Onfleet::Actions::Delete do |path:| set_up_request_stub(:delete, path) let(:response_body) { '' } From 9079ae0b399346fefd62d848191dd2cb184bde76 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:04:25 -0400 Subject: [PATCH 54/67] Add specs for API query by metadata behavior --- lib/onfleet-ruby/actions/query_metadata.rb | 5 ++--- spec/onfleet/admin_spec.rb | 6 ++++++ spec/support/http_requests/shared_examples.rb | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/onfleet-ruby/actions/query_metadata.rb b/lib/onfleet-ruby/actions/query_metadata.rb index 9036d12..bdcfca7 100644 --- a/lib/onfleet-ruby/actions/query_metadata.rb +++ b/lib/onfleet-ruby/actions/query_metadata.rb @@ -3,9 +3,8 @@ module Actions module QueryMetadata module ClassMethods def query_by_metadata(metadata) - api_url = "#{self.api_url}/metadata" - response = Onfleet.request(api_url, :post, metadata) - response.map { |item| Util.constantize(name).new(item) } if response.is_a? Array + response = Onfleet.request("#{api_url}/metadata", :post, metadata) + [*response].compact.map { |item| new(item) } end end diff --git a/spec/onfleet/admin_spec.rb b/spec/onfleet/admin_spec.rb index 395ec8d..d93ef74 100644 --- a/spec/onfleet/admin_spec.rb +++ b/spec/onfleet/admin_spec.rb @@ -38,6 +38,12 @@ it_should_behave_like Onfleet::Actions::Delete, path: 'admins/an-admin' end + describe ".query_by_metadata" do + subject { -> { described_class.query_by_metadata(metadata) } } + let(:metadata) { [{ name: 'color', type: 'string', value: 'ochre' }] } + it_should_behave_like Onfleet::Actions::QueryMetadata, path: 'admins' + end + %i[id name email type metadata].each do |attr| describe "##{attr}" do subject { admin.public_send(attr) } diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb index bab0999..967a13f 100644 --- a/spec/support/http_requests/shared_examples.rb +++ b/spec/support/http_requests/shared_examples.rb @@ -89,6 +89,19 @@ it_should_behave_like "an action that makes a request to Onfleet", method: :delete end +RSpec.shared_examples_for Onfleet::Actions::QueryMetadata do |path:| + set_up_request_stub(:post, path + '/metadata') + let(:response_body) { [{ id: 'an-object' }, { id: 'another-object' }] } + it_should_behave_like "an action that makes a request to Onfleet", method: :post + + it "should send metadata in JSON" do + subject.call + expect( + a_request(:post, url).with(body: metadata.to_json) + ).to have_been_made.once + end +end + def set_up_request_stub(method, path) let(:url) { URI.join(Onfleet.base_url, path).to_s } let(:response) { { status: 200, body: response_body.to_json } } From 2c120d6cf4df1c58dc76f6182b148266f3cfd31d Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:04:25 -0400 Subject: [PATCH 55/67] Add specs for API save behavior --- lib/onfleet-ruby/actions/save.rb | 19 +++++++++++-------- lib/onfleet-ruby/onfleet_object.rb | 2 ++ spec/onfleet/admin_spec.rb | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/onfleet-ruby/actions/save.rb b/lib/onfleet-ruby/actions/save.rb index 823e949..205d5d2 100644 --- a/lib/onfleet-ruby/actions/save.rb +++ b/lib/onfleet-ruby/actions/save.rb @@ -2,16 +2,19 @@ module Onfleet module Actions module Save def save - if respond_to?('id') && id - request_type = :put - api_url = "#{self.api_url}/#{id}" - else - request_type = :post - api_url = self.api_url - end - response = Onfleet.request(api_url, request_type, attributes) + response = Onfleet.request(save_url, request_type, attributes) parse_response(response) end + + private + + def request_type + id ? :put : :post + end + + def save_url + [api_url, id].compact.join('/') + end end end end diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index 22c2696..df5b27a 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -1,6 +1,8 @@ module Onfleet class OnfleetObject attr_reader :params + attr_accessor :id + def initialize(params) if params.is_a?(Hash) @params = params diff --git a/spec/onfleet/admin_spec.rb b/spec/onfleet/admin_spec.rb index d93ef74..2f45d5c 100644 --- a/spec/onfleet/admin_spec.rb +++ b/spec/onfleet/admin_spec.rb @@ -44,6 +44,20 @@ it_should_behave_like Onfleet::Actions::QueryMetadata, path: 'admins' end + describe "#save" do + subject { -> { admin.save } } + + context "with an ID attribute" do + before { expect(params[:id]).to be } + it_should_behave_like Onfleet::Actions::Update, path: 'admins/an-admin' + end + + context "without an ID attribute" do + let(:params) { { name: 'An Admin' } } + it_should_behave_like Onfleet::Actions::Create, path: 'admins' + end + end + %i[id name email type metadata].each do |attr| describe "##{attr}" do subject { admin.public_send(attr) } From d46407963e896d02e1bef64d73a095e8157da286 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 21:28:10 -0400 Subject: [PATCH 56/67] Add rspec helper dependency --- onfleet-ruby.gemspec | 1 + spec/spec_helper.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/onfleet-ruby.gemspec b/onfleet-ruby.gemspec index b786f0c..f5ccc59 100644 --- a/onfleet-ruby.gemspec +++ b/onfleet-ruby.gemspec @@ -14,6 +14,7 @@ Gem::Specification.new do |s| s.add_development_dependency('rake') s.add_development_dependency('rspec', '~> 3.3') + s.add_development_dependency('rspec-its') s.add_development_dependency('rubocop', '~> 0.55') s.add_development_dependency('webmock', '~> 3.4') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index faecc05..feb56fb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,4 @@ +require 'rspec/its' require 'webmock/rspec' require File.expand_path(File.join('..', 'lib', 'onfleet-ruby'), __dir__) From 90aedd7f1f41a49c5b971162d5ff4c3dfb405c69 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 21:28:37 -0400 Subject: [PATCH 57/67] Add specs for Destination object --- spec/onfleet/destination_spec.rb | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 spec/onfleet/destination_spec.rb diff --git a/spec/onfleet/destination_spec.rb b/spec/onfleet/destination_spec.rb new file mode 100644 index 0000000..d02172f --- /dev/null +++ b/spec/onfleet/destination_spec.rb @@ -0,0 +1,62 @@ +RSpec.describe Onfleet::Destination do + let(:destination) { described_class.new(params) } + let(:params) { { id: 'a-destination', address: address_params } } + let(:address_params) { { street: '123 Main', city: 'Foo', state: 'TX' } } + + describe ".create" do + subject { -> { described_class.create(params) } } + it_should_behave_like Onfleet::Actions::Create, path: 'destinations' + end + + describe ".get" do + subject { -> { described_class.get(id) } } + let(:id) { 'a-destination' } + it_should_behave_like Onfleet::Actions::Get, path: 'destinations/a-destination' + end + + describe ".query_by_metadata" do + subject { -> { described_class.query_by_metadata(metadata) } } + let(:metadata) { [{ name: 'color', type: 'string', value: 'ochre' }] } + it_should_behave_like Onfleet::Actions::QueryMetadata, path: 'destinations' + end + + describe "#save" do + subject { -> { destination.save } } + + context "with an ID attribute" do + before { expect(params[:id]).to be } + it_should_behave_like Onfleet::Actions::Update, path: 'destinations/a-destination' + end + + context "without an ID attribute" do + let(:params) { { address: address_params } } + it_should_behave_like Onfleet::Actions::Create, path: 'destinations' + end + end + + describe "#address" do + subject { destination.address } + + context "when initialized with address params" do + let(:address_params) do + { + number: '123', + street: 'Main St.', + apartment: '', + city: 'Foo', + state: 'TX', + postalCode: '99999', + country: 'United States' + } + end + its(:number) { should == '123' } + its(:postal_code) { should == '99999' } + end + + context "when initialized with no address params" do + let(:address_params) { nil } + it { should be_nil } + end + end +end + From 597a067338da92a1df060c6c56e97a9a250230bb Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 21:42:37 -0400 Subject: [PATCH 58/67] Properly camelize request keys in expectations --- spec/support/http_requests/shared_examples.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb index 967a13f..b0db2e1 100644 --- a/spec/support/http_requests/shared_examples.rb +++ b/spec/support/http_requests/shared_examples.rb @@ -59,9 +59,11 @@ it_should_behave_like "an action that makes a request to Onfleet", method: :post it "should send the object params, not including ID, in JSON" do + expected_params = camelize_keys(params.stringify_keys.except('id')) + subject.call expect( - a_request(:post, url).with(body: params.symbolize_keys.except(:id).to_json) + a_request(:post, url).with(body: expected_params.to_json) ).to have_been_made.once end end @@ -75,9 +77,11 @@ # The current implementation -- using instance variables -- makes it impossible # to have this example pass deterministically. xit "should send the object params, including ID, in JSON" do + expected_params = camelize_keys(params.merge(id: id)) + subject.call expect( - a_request(:put, url).with(body: params.merge(id: id).to_json) + a_request(:put, url).with(body: expected_params.to_json) ).to have_been_made.once end end @@ -108,3 +112,9 @@ def set_up_request_stub(method, path) before { stub_request(method, url).to_return(response) } end +def camelize_keys(hash) + hash.inject({}) do |accumulator, (key, value)| + accumulator.merge(key.camelize(:lower) => value) + end +end + From 08ec9dd01872c5cf172d195b51e88a46e56897c4 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 21:28:37 -0400 Subject: [PATCH 59/67] Add specs for Recipient object --- spec/onfleet/recipient_spec.rb | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 spec/onfleet/recipient_spec.rb diff --git a/spec/onfleet/recipient_spec.rb b/spec/onfleet/recipient_spec.rb new file mode 100644 index 0000000..82e9fd2 --- /dev/null +++ b/spec/onfleet/recipient_spec.rb @@ -0,0 +1,68 @@ +RSpec.describe Onfleet::Recipient do + let(:recipient) { described_class.new(params) } + let(:params) { { id: 'a-recipient', name: 'Recipient Jones' } } + + describe ".create" do + subject { -> { described_class.create(params) } } + it_should_behave_like Onfleet::Actions::Create, path: 'recipients' + + context "with the `skip_sms_notifications` attribute" do + set_up_request_stub(:post, 'recipients') + let(:params) { { skip_sms_notifications: true } } + let(:response_body) { { id: 'an-object' } } + + it "should camelize the attribute name properly" do + subject.call + expect( + a_request(:post, url).with(body: { 'skipSMSNotifications' => true }.to_json) + ).to have_been_made.once + end + end + end + + describe ".get" do + subject { -> { described_class.get(id) } } + let(:id) { 'a-recipient' } + it_should_behave_like Onfleet::Actions::Get, path: 'recipients/a-recipient' + end + + describe ".update" do + subject { -> { described_class.update(id, params) } } + let(:id) { 'a-recipient' } + it_should_behave_like Onfleet::Actions::Update, path: 'recipients/a-recipient' + + context "with the `skip_sms_notifications` attribute" do + set_up_request_stub(:put, 'recipients/a-recipient') + let(:params) { { id: 'a-recipient', skip_sms_notifications: true } } + let(:response_body) { { id: 'an-object' } } + + it "should camelize the attribute name properly" do + subject.call + expect( + a_request(:put, url).with(body: { id: 'a-recipient', 'skipSMSNotifications' => true }.to_json) + ).to have_been_made.once + end + end + end + + describe ".query_by_metadata" do + subject { -> { described_class.query_by_metadata(metadata) } } + let(:metadata) { [{ name: 'color', type: 'string', value: 'ochre' }] } + it_should_behave_like Onfleet::Actions::QueryMetadata, path: 'recipients' + end + + describe "#save" do + subject { -> { recipient.save } } + + context "with an ID attribute" do + before { expect(params[:id]).to be } + it_should_behave_like Onfleet::Actions::Update, path: 'recipients/a-recipient' + end + + context "without an ID attribute" do + let(:params) { { name: 'Recipient Jones' } } + it_should_behave_like Onfleet::Actions::Create, path: 'recipients' + end + end +end + From d897ac1fc6f52ac98fc1d92e9186b62779f283fe Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 10:04:25 -0400 Subject: [PATCH 60/67] Add specs for API find behavior --- lib/onfleet-ruby/actions/find.rb | 7 ++++--- spec/onfleet/recipient_spec.rb | 7 +++++++ spec/support/http_requests/shared_examples.rb | 6 ++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/onfleet-ruby/actions/find.rb b/lib/onfleet-ruby/actions/find.rb index 5019ffb..52411f4 100644 --- a/lib/onfleet-ruby/actions/find.rb +++ b/lib/onfleet-ruby/actions/find.rb @@ -4,9 +4,10 @@ module Find module ClassMethods def find(field, search_term) encoded_term = URI.encode_www_form_component(search_term) - api_url = "#{self.api_url}/#{field}/#{encoded_term}" - response = Onfleet.request(api_url, :get, search_term) - Util.constantize(name).new(response) + url = "#{api_url}/#{field}/#{encoded_term}" + + response = Onfleet.request(url, :get) + new(response) end end diff --git a/spec/onfleet/recipient_spec.rb b/spec/onfleet/recipient_spec.rb index 82e9fd2..0314486 100644 --- a/spec/onfleet/recipient_spec.rb +++ b/spec/onfleet/recipient_spec.rb @@ -45,6 +45,13 @@ end end + describe ".find" do + subject { -> { described_class.find(attribute, value) } } + let(:attribute) { 'name' } + let(:value) { 'Ma Bell' } + it_should_behave_like Onfleet::Actions::Find, path: "recipients/name/Ma+Bell" + end + describe ".query_by_metadata" do subject { -> { described_class.query_by_metadata(metadata) } } let(:metadata) { [{ name: 'color', type: 'string', value: 'ochre' }] } diff --git a/spec/support/http_requests/shared_examples.rb b/spec/support/http_requests/shared_examples.rb index b0db2e1..48d5c22 100644 --- a/spec/support/http_requests/shared_examples.rb +++ b/spec/support/http_requests/shared_examples.rb @@ -93,6 +93,12 @@ it_should_behave_like "an action that makes a request to Onfleet", method: :delete end +RSpec.shared_examples_for Onfleet::Actions::Find do |path:| + set_up_request_stub(:get, path) + let(:response_body) { { id: 'an-object' } } + it_should_behave_like "an action that makes a request to Onfleet", method: :get +end + RSpec.shared_examples_for Onfleet::Actions::QueryMetadata do |path:| set_up_request_stub(:post, path + '/metadata') let(:response_body) { [{ id: 'an-object' }, { id: 'another-object' }] } From eb3cfd7463cb14ebcd573af186cf12155187dbc2 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 21:28:37 -0400 Subject: [PATCH 61/67] Add specs for Task object --- spec/onfleet/task_spec.rb | 123 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 spec/onfleet/task_spec.rb diff --git a/spec/onfleet/task_spec.rb b/spec/onfleet/task_spec.rb new file mode 100644 index 0000000..a2e95b6 --- /dev/null +++ b/spec/onfleet/task_spec.rb @@ -0,0 +1,123 @@ +RSpec.describe Onfleet::Task do + let(:task) { described_class.new(params) } + let(:params) { { id: 'a-task', short_id: 'at', recipients: ['jeff'] } } + + describe ".list" do + subject { -> { described_class.list(query_params) } } + + context "with no filter" do + let(:query_params) { nil } + it_should_behave_like Onfleet::Actions::List, path: 'tasks' + end + + context "with query params" do + let(:query_params) { { food: 'pizza', topping: 'mushroom' } } + it_should_behave_like Onfleet::Actions::List, path: 'tasks?food=pizza&topping=mushroom' + end + + context "with a URL-unsafe query param" do + let(:query_params) { { food: 'green eggs & ham' } } + it_should_behave_like Onfleet::Actions::List, path: 'tasks?food=green+eggs+%26+ham' + end + end + + describe ".create" do + subject { -> { described_class.create(params) } } + it_should_behave_like Onfleet::Actions::Create, path: 'tasks' + + context "with the skip_sms_notification override attribute" do + set_up_request_stub(:post, 'tasks') + let(:params) { { recipient_skip_sms_notifications: true } } + let(:response_body) { { id: 'an-object' } } + + it "should camelize the attribute name properly" do + pending('The SMS acronym does not camelize consistently') + subject.call + expect( + a_request(:post, url).with(body: { recipientSkipSMSNotifications: true }.to_json) + ).to have_been_made.once + end + end + + context "with barcode attributes" do + set_up_request_stub(:post, 'tasks') + let(:params) { { barcodes: [{ data: 'abc', block_completion: true }] } } + let(:response_body) { { id: 'an-object' } } + + it "should camelize the attribute name properly" do + pending('JSON subkeys of non-Onfleet objects do not camelize') + subject.call + expect( + a_request(:post, url).with(body: { barcodes: [{ data: 'abc', 'blockCompletion' => true }] }.to_json) + ).to have_been_made.once + end + end + end + + describe ".get" do + subject { -> { described_class.get(id) } } + let(:id) { 'a-task' } + it_should_behave_like Onfleet::Actions::Get, path: 'tasks/a-task' + end + + describe ".update" do + subject { -> { described_class.update(id, params) } } + let(:id) { 'a-task' } + it_should_behave_like Onfleet::Actions::Update, path: 'tasks/a-task' + + context "with the skip_sms_notification override attribute" do + set_up_request_stub(:put, 'tasks/a-task') + let(:params) { { id: 'a-task', recipient_skip_sms_notifications: true } } + let(:response_body) { { id: 'an-object' } } + + it "should camelize the attribute name properly" do + pending('The SMS acronym does not camelize consistently') + subject.call + expect( + a_request(:put, url).with(body: { recipientSkipSMSNotifications: true }.to_json) + ).to have_been_made.once + end + end + + context "with barcode attributes" do + set_up_request_stub(:put, 'tasks/a-task') + let(:params) { { id: 'a-task', barcodes: [{ data: 'abc', block_completion: true }] } } + let(:response_body) { { id: 'an-object' } } + + it "should camelize the attribute name properly" do + pending('JSON subkeys of non-Onfleet objects do not camelize') + subject.call + expect( + a_request(:put, url).with(body: { barcodes: [{ data: 'abc', 'blockCompletion' => true }] }.to_json) + ).to have_been_made.once + end + end + end + + describe ".delete" do + subject { -> { described_class.delete(id) } } + let(:id) { 'an-task' } + it_should_behave_like Onfleet::Actions::Delete, path: 'tasks/an-task' + end + + describe ".query_by_metadata" do + subject { -> { described_class.query_by_metadata(metadata) } } + let(:metadata) { [{ name: 'color', type: 'string', value: 'ochre' }] } + it_should_behave_like Onfleet::Actions::QueryMetadata, path: 'tasks' + end + + describe "#save" do + subject { -> { task.save } } + + context "with an ID attribute" do + before { expect(params[:id]).to be } + it_should_behave_like Onfleet::Actions::Update, path: 'tasks/a-task' + end + + context "without an ID attribute" do + let(:params) { { short_id: 'at', recipients: ['jeff'] } } + it_should_behave_like Onfleet::Actions::Create, path: 'tasks' + end + end +end + From 726407a1021b4da0489b8134bcf3b4a1d76119fc Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 21:28:37 -0400 Subject: [PATCH 62/67] Add specs for Team object --- spec/onfleet/team_spec.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 spec/onfleet/team_spec.rb diff --git a/spec/onfleet/team_spec.rb b/spec/onfleet/team_spec.rb new file mode 100644 index 0000000..5a62461 --- /dev/null +++ b/spec/onfleet/team_spec.rb @@ -0,0 +1,30 @@ +RSpec.describe Onfleet::Team do + let(:team) { described_class.new(params) } + let(:params) { { id: 'a-team', name: 'Detroit Redwings' } } + + describe ".list" do + subject { -> { described_class.list(query_params) } } + + context "with no filter" do + let(:query_params) { nil } + it_should_behave_like Onfleet::Actions::List, path: 'teams' + end + + context "with query params" do + let(:query_params) { { food: 'pizza', topping: 'mushroom' } } + it_should_behave_like Onfleet::Actions::List, path: 'teams?food=pizza&topping=mushroom' + end + + context "with a URL-unsafe query param" do + let(:query_params) { { food: 'green eggs & ham' } } + it_should_behave_like Onfleet::Actions::List, path: 'teams?food=green+eggs+%26+ham' + end + end + + describe ".get" do + subject { -> { described_class.get(id) } } + let(:id) { 'a-team' } + it_should_behave_like Onfleet::Actions::Get, path: 'teams/a-team' + end +end + From f37610c0380d04a219a538e9b51b65404bc1911b Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 21:28:37 -0400 Subject: [PATCH 63/67] Add specs for Webhook object --- spec/onfleet/webhook_spec.rb | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 spec/onfleet/webhook_spec.rb diff --git a/spec/onfleet/webhook_spec.rb b/spec/onfleet/webhook_spec.rb new file mode 100644 index 0000000..05eec79 --- /dev/null +++ b/spec/onfleet/webhook_spec.rb @@ -0,0 +1,49 @@ +RSpec.describe Onfleet::Webhook do + let(:webhook) { described_class.new(params) } + let(:params) { { id: 'a-webhook', url: 'https://example.com', is_enabled: true } } + + describe ".list" do + subject { -> { described_class.list(query_params) } } + + context "with no filter" do + let(:query_params) { nil } + it_should_behave_like Onfleet::Actions::List, path: 'webhooks' + end + + context "with query params" do + let(:query_params) { { food: 'pizza', topping: 'mushroom' } } + it_should_behave_like Onfleet::Actions::List, path: 'webhooks?food=pizza&topping=mushroom' + end + + context "with a URL-unsafe query param" do + let(:query_params) { { food: 'green eggs & ham' } } + it_should_behave_like Onfleet::Actions::List, path: 'webhooks?food=green+eggs+%26+ham' + end + end + + describe ".create" do + subject { -> { described_class.create(params) } } + it_should_behave_like Onfleet::Actions::Create, path: 'webhooks' + end + + describe ".delete" do + subject { -> { described_class.delete(id) } } + let(:id) { 'a-webhook' } + it_should_behave_like Onfleet::Actions::Delete, path: 'webhooks/a-webhook' + end + + describe "#save" do + subject { -> { webhook.save } } + + context "with an ID attribute" do + before { expect(params[:id]).to be } + it_should_behave_like Onfleet::Actions::Update, path: 'webhooks/a-webhook' + end + + context "without an ID attribute" do + let(:params) { { name: 'An Webhook' } } + it_should_behave_like Onfleet::Actions::Create, path: 'webhooks' + end + end +end + From cb77a2e64fe44bb08640a2eb32ca96467fa96154 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Mon, 14 May 2018 21:28:37 -0400 Subject: [PATCH 64/67] Add specs for Worker object --- spec/onfleet/worker_spec.rb | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 spec/onfleet/worker_spec.rb diff --git a/spec/onfleet/worker_spec.rb b/spec/onfleet/worker_spec.rb new file mode 100644 index 0000000..dded9b6 --- /dev/null +++ b/spec/onfleet/worker_spec.rb @@ -0,0 +1,67 @@ +RSpec.describe Onfleet::Worker do + let(:worker) { described_class.new(params) } + let(:params) { { id: 'a-worker', name: 'F. Prefect', phone: '5551212' } } + + describe ".list" do + subject { -> { described_class.list(query_params) } } + + context "with no filter" do + let(:query_params) { nil } + it_should_behave_like Onfleet::Actions::List, path: 'workers' + end + + context "with query params" do + let(:query_params) { { food: 'pizza', topping: 'mushroom' } } + it_should_behave_like Onfleet::Actions::List, path: 'workers?food=pizza&topping=mushroom' + end + + context "with a URL-unsafe query param" do + let(:query_params) { { food: 'green eggs & ham' } } + it_should_behave_like Onfleet::Actions::List, path: 'workers?food=green+eggs+%26+ham' + end + end + + describe ".create" do + subject { -> { described_class.create(params) } } + it_should_behave_like Onfleet::Actions::Create, path: 'workers' + end + + describe ".get" do + subject { -> { described_class.get(id) } } + let(:id) { 'a-worker' } + it_should_behave_like Onfleet::Actions::Get, path: 'workers/a-worker' + end + + describe ".update" do + subject { -> { described_class.update(id, params) } } + let(:id) { 'a-worker' } + it_should_behave_like Onfleet::Actions::Update, path: 'workers/a-worker' + end + + describe ".delete" do + subject { -> { described_class.delete(id) } } + let(:id) { 'a-worker' } + it_should_behave_like Onfleet::Actions::Delete, path: 'workers/a-worker' + end + + describe ".query_by_metadata" do + subject { -> { described_class.query_by_metadata(metadata) } } + let(:metadata) { [{ name: 'color', type: 'string', value: 'ochre' }] } + it_should_behave_like Onfleet::Actions::QueryMetadata, path: 'workers' + end + + describe "#save" do + subject { -> { worker.save } } + + context "with an ID attribute" do + before { expect(params[:id]).to be } + it_should_behave_like Onfleet::Actions::Update, path: 'workers/a-worker' + end + + context "without an ID attribute" do + let(:params) { { name: 'An Worker' } } + it_should_behave_like Onfleet::Actions::Create, path: 'workers' + end + end +end + From 0f1043490f340788f264d1e325f8327401186f3a Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Tue, 15 May 2018 09:56:57 -0400 Subject: [PATCH 65/67] Remove unnecessary utility method --- lib/onfleet-ruby/actions/get.rb | 5 ++--- lib/onfleet-ruby/util.rb | 4 ---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/onfleet-ruby/actions/get.rb b/lib/onfleet-ruby/actions/get.rb index e91a42f..df386c1 100644 --- a/lib/onfleet-ruby/actions/get.rb +++ b/lib/onfleet-ruby/actions/get.rb @@ -3,9 +3,8 @@ module Actions module Get module ClassMethods def get(id) - api_url = "#{self.api_url}/#{id}" - response = Onfleet.request(api_url, :get) - Util.constantize(name).new(response) + url = "#{api_url}/#{id}" + new(Onfleet.request(url, :get)) end end diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index c0a24fa..08e1bb3 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -4,10 +4,6 @@ module Onfleet class Util SPECIAL_PARSE = { 'skip_sms_notifications' => 'skipSMSNotifications' }.freeze - def self.constantize(class_name) - Object.const_get(class_name) - end - def self.to_underscore(key) key .to_s From 2cbfd934acb224a583dbb89fc489a030186c0b55 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Tue, 15 May 2018 19:25:16 -0400 Subject: [PATCH 66/67] Replace utility methods with existing methods --- lib/onfleet-ruby/onfleet_object.rb | 17 ++++++++++++----- lib/onfleet-ruby/util.rb | 16 ---------------- spec/onfleet/task_spec.rb | 8 ++++---- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index df5b27a..a859f2b 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/string/inflections' + module Onfleet class OnfleetObject attr_reader :params @@ -29,18 +31,18 @@ def attributes instance_var = instance_variable_get(var) if Util.object_classes[str] if instance_var.is_a?(OnfleetObject) - attrs[Util.to_camel_case_lower(str).to_sym] = parse_onfleet_obj(instance_var) + attrs[camelize(str).to_sym] = parse_onfleet_obj(instance_var) elsif instance_var.is_a?(Array) objs = [] instance_var.each do |object| objs << parse_onfleet_obj(object) end - attrs[Util.to_camel_case_lower(str).to_sym] = objs + attrs[camelize(str).to_sym] = objs else - attrs[Util.to_camel_case_lower(str).to_sym] = instance_var + attrs[camelize(str).to_sym] = instance_var end else - attrs[Util.to_camel_case_lower(str).to_sym] = instance_var + attrs[camelize(str).to_sym] = instance_var end end attrs @@ -56,6 +58,11 @@ def api_url private + def camelize(string) + camelized = string.camelize(:lower) + camelized.gsub('Sms', 'SMS') + end + def parse_onfleet_obj(obj) return unless obj.is_a?(OnfleetObject) if obj.respond_to?('id') && obj.id && (obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task)) @@ -67,7 +74,7 @@ def parse_onfleet_obj(obj) def assign_attributes(params) params.each do |key, value| - key_underscore = Util.to_underscore(key) + key_underscore = key.to_s.underscore if (klass = Util.object_classes[key.to_s]) case value diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb index 08e1bb3..8512170 100644 --- a/lib/onfleet-ruby/util.rb +++ b/lib/onfleet-ruby/util.rb @@ -2,22 +2,6 @@ module Onfleet class Util - SPECIAL_PARSE = { 'skip_sms_notifications' => 'skipSMSNotifications' }.freeze - - def self.to_underscore(key) - key - .to_s - .gsub(/::/, '/') - .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') - .gsub(/([a-z\d])([A-Z])/, '\1_\2') - .tr('-', '_') - .downcase - end - - def self.to_camel_case_lower(str) - SPECIAL_PARSE[str] || str.camelize(:lower) - end - def self.object_classes @object_classes ||= { 'address' => Address, diff --git a/spec/onfleet/task_spec.rb b/spec/onfleet/task_spec.rb index a2e95b6..593abdf 100644 --- a/spec/onfleet/task_spec.rb +++ b/spec/onfleet/task_spec.rb @@ -31,7 +31,6 @@ let(:response_body) { { id: 'an-object' } } it "should camelize the attribute name properly" do - pending('The SMS acronym does not camelize consistently') subject.call expect( a_request(:post, url).with(body: { recipientSkipSMSNotifications: true }.to_json) @@ -70,11 +69,12 @@ let(:params) { { id: 'a-task', recipient_skip_sms_notifications: true } } let(:response_body) { { id: 'an-object' } } - it "should camelize the attribute name properly" do - pending('The SMS acronym does not camelize consistently') + # The current implementation -- using instance variables -- makes it impossible + # to have this example pass deterministically. + xit "should camelize the attribute name properly" do subject.call expect( - a_request(:put, url).with(body: { recipientSkipSMSNotifications: true }.to_json) + a_request(:put, url).with(body: { id: 'a-task', recipientSkipSMSNotifications: true }.to_json) ).to have_been_made.once end end From 80cad85dba9fd7450a2f3f23069d268a9674c150 Mon Sep 17 00:00:00 2001 From: Adam Milligan Date: Tue, 15 May 2018 19:40:46 -0400 Subject: [PATCH 67/67] Move global function to class where it is used --- lib/onfleet-ruby.rb | 3 --- lib/onfleet-ruby/onfleet_object.rb | 15 +++++++++++++-- lib/onfleet-ruby/util.rb | 17 ----------------- 3 files changed, 13 insertions(+), 22 deletions(-) delete mode 100644 lib/onfleet-ruby/util.rb diff --git a/lib/onfleet-ruby.rb b/lib/onfleet-ruby.rb index a889d60..149ab78 100644 --- a/lib/onfleet-ruby.rb +++ b/lib/onfleet-ruby.rb @@ -3,9 +3,6 @@ require 'base64' require 'uri' -# Utils -require 'onfleet-ruby/util' - # Errors require 'onfleet-ruby/errors/onfleet_error' require 'onfleet-ruby/errors/authentication_error' diff --git a/lib/onfleet-ruby/onfleet_object.rb b/lib/onfleet-ruby/onfleet_object.rb index a859f2b..549b6a9 100644 --- a/lib/onfleet-ruby/onfleet_object.rb +++ b/lib/onfleet-ruby/onfleet_object.rb @@ -29,7 +29,7 @@ def attributes str = var.to_s.gsub(/^@/, '') next unless respond_to?("#{str}=") instance_var = instance_variable_get(var) - if Util.object_classes[str] + if object_classes[str] if instance_var.is_a?(OnfleetObject) attrs[camelize(str).to_sym] = parse_onfleet_obj(instance_var) elsif instance_var.is_a?(Array) @@ -63,6 +63,17 @@ def camelize(string) camelized.gsub('Sms', 'SMS') end + def object_classes + @object_classes ||= { + 'address' => Address, + 'recipients' => Recipient, + 'recipient' => Recipient, + 'tasks' => Task, + 'destination' => Destination, + 'vehicle' => Vehicle + } + end + def parse_onfleet_obj(obj) return unless obj.is_a?(OnfleetObject) if obj.respond_to?('id') && obj.id && (obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task)) @@ -76,7 +87,7 @@ def assign_attributes(params) params.each do |key, value| key_underscore = key.to_s.underscore - if (klass = Util.object_classes[key.to_s]) + if (klass = object_classes[key.to_s]) case value when Array objs = [] diff --git a/lib/onfleet-ruby/util.rb b/lib/onfleet-ruby/util.rb deleted file mode 100644 index 8512170..0000000 --- a/lib/onfleet-ruby/util.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'active_support/core_ext/string/inflections' - -module Onfleet - class Util - def self.object_classes - @object_classes ||= { - 'address' => Address, - 'recipients' => Recipient, - 'recipient' => Recipient, - 'tasks' => Task, - 'destination' => Destination, - 'vehicle' => Vehicle - } - end - end -end -