onfleet-ruby 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -0
  3. data/.rspec +1 -2
  4. data/.rubocop.yml +26 -0
  5. data/.rubocop_todo.yml +38 -0
  6. data/.ruby-version +2 -0
  7. data/Gemfile +3 -3
  8. data/Rakefile +10 -0
  9. data/lib/onfleet-ruby.rb +40 -38
  10. data/lib/onfleet-ruby/actions/create.rb +6 -3
  11. data/lib/onfleet-ruby/actions/delete.rb +4 -4
  12. data/lib/onfleet-ruby/actions/find.rb +8 -6
  13. data/lib/onfleet-ruby/actions/get.rb +5 -4
  14. data/lib/onfleet-ruby/actions/list.rb +15 -13
  15. data/lib/onfleet-ruby/actions/query_metadata.rb +5 -5
  16. data/lib/onfleet-ruby/actions/save.rb +13 -9
  17. data/lib/onfleet-ruby/actions/update.rb +4 -4
  18. data/lib/onfleet-ruby/address.rb +1 -0
  19. data/lib/onfleet-ruby/admin.rb +2 -1
  20. data/lib/onfleet-ruby/destination.rb +2 -1
  21. data/lib/onfleet-ruby/errors/authentication_error.rb +1 -0
  22. data/lib/onfleet-ruby/errors/connection_error.rb +1 -0
  23. data/lib/onfleet-ruby/errors/invalid_request_error.rb +1 -0
  24. data/lib/onfleet-ruby/errors/onfleet_error.rb +1 -0
  25. data/lib/onfleet-ruby/onfleet_object.rb +58 -59
  26. data/lib/onfleet-ruby/organization.rb +4 -9
  27. data/lib/onfleet-ruby/recipient.rb +2 -1
  28. data/lib/onfleet-ruby/task.rb +4 -4
  29. data/lib/onfleet-ruby/team.rb +2 -1
  30. data/lib/onfleet-ruby/util.rb +22 -20
  31. data/lib/onfleet-ruby/vehicle.rb +1 -0
  32. data/lib/onfleet-ruby/webhook.rb +2 -2
  33. data/lib/onfleet-ruby/worker.rb +2 -1
  34. data/onfleet-ruby.gemspec +11 -6
  35. data/spec/onfleet/admin_spec.rb +70 -0
  36. data/spec/onfleet/destination_spec.rb +62 -0
  37. data/spec/onfleet/organization_spec.rb +25 -0
  38. data/spec/onfleet/recipient_spec.rb +75 -0
  39. data/spec/onfleet/task_spec.rb +123 -0
  40. data/spec/onfleet/team_spec.rb +30 -0
  41. data/spec/onfleet/webhook_spec.rb +49 -0
  42. data/spec/onfleet/worker_spec.rb +67 -0
  43. data/spec/spec_helper.rb +78 -2
  44. data/spec/support/http_requests/shared_examples.rb +126 -0
  45. metadata +97 -13
  46. data/spec/test_data.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 298c2c7a63d016fd61101c6c30229a3e7700868d
4
- data.tar.gz: 285a8280869fe460c29561093272d7d4cb682169
2
+ SHA256:
3
+ metadata.gz: 5fd31c23992d29f9cc2eed9330212acf71eab0988dbb1d7d99f9c06447af56f6
4
+ data.tar.gz: 82499d975b1ecaf3487096580a775a00d08c5f59d4dd7769627e1c1800dfc6fa
5
5
  SHA512:
6
- metadata.gz: 840dd631d9fe75349761d363dcf220f4829b49af17b40cd49abdabb1c775bfd271475217b730be4ae174467246c9142959ff853d3c746dd033bf7b478f965e45
7
- data.tar.gz: 1b2c32961b36185eeaed57d7949c44950119b34a0c6799bb41d6324719b1c1aebe322e59f5cad0ccb4fd54a639d11eddc0019eee6036861438d5e84913aed97b
6
+ metadata.gz: 01ab99e61bcadbb621cfe07fbc23aa36454f7972c4dbeb70cb8f69547c09113de7a1d13f57109d910abfa1f30f9bfed31658afa6626a3c114b1d815ce5496bda
7
+ data.tar.gz: 76795dae4abf9bb143a5226617128aa08ed6b97a937384ef806386a20bc2730f42503553795b7e27291f353077af3e5c6147639a590f278d9dbefee05a6d8b81
data/.gitignore CHANGED
@@ -5,3 +5,6 @@
5
5
  # Ignore bundler config
6
6
  /.bundle
7
7
  Gemfile.lock
8
+
9
+ spec/examples.txt
10
+
data/.rspec CHANGED
@@ -1,2 +1 @@
1
- --color
2
- --format documentation
1
+ --require spec_helper
@@ -0,0 +1,26 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.2
5
+
6
+ # Keep diffs clean
7
+ Layout/TrailingBlankLines:
8
+ EnforcedStyle: final_blank_line
9
+
10
+ # No way to avoid large blocks in RSpec
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - spec/**/*
14
+
15
+ # Allow the gem to have a non-snakecase name
16
+ Naming/FileName:
17
+ Exclude:
18
+ - 'lib/onfleet-ruby.rb'
19
+
20
+ Style/Documentation:
21
+ Enabled: false
22
+
23
+ Style/StringLiterals:
24
+ Exclude:
25
+ - spec/**/*
26
+
@@ -0,0 +1,38 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2018-05-13 20:30:59 -0400 using RuboCop version 0.55.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ Metrics/AbcSize:
11
+ Max: 26
12
+
13
+ # Offense count: 1
14
+ Metrics/CyclomaticComplexity:
15
+ Max: 7
16
+
17
+ # Offense count: 4
18
+ # Configuration parameters: CountComments.
19
+ Metrics/MethodLength:
20
+ Max: 22
21
+
22
+ # Offense count: 1
23
+ Metrics/PerceivedComplexity:
24
+ Max: 8
25
+
26
+ # Offense count: 29
27
+ # Cop supports --auto-correct.
28
+ # Configuration parameters: EnforcedStyle.
29
+ # SupportedStyles: when_needed, always, never
30
+ Style/FrozenStringLiteralComment:
31
+ Enabled: false
32
+
33
+ # Offense count: 9
34
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
35
+ # URISchemes: http, https
36
+ Metrics/LineLength:
37
+ Max: 138
38
+
@@ -0,0 +1,2 @@
1
+ 2.5.1
2
+
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- gemspec
4
- gem 'rest-client', '~> 1.6.8'
3
+ ruby '2.5.1'
5
4
 
5
+ gemspec
6
6
 
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rubocop/rake_task'
4
+ RuboCop::RakeTask.new
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: %i[spec rubocop]
10
+
@@ -35,66 +35,68 @@ require 'onfleet-ruby/vehicle'
35
35
  require 'onfleet-ruby/worker'
36
36
  require 'onfleet-ruby/webhook'
37
37
 
38
-
39
38
  module Onfleet
40
- @base_url = "https://onfleet.com/api/v2"
39
+ @base_url = 'https://onfleet.com/api/v2/'
41
40
 
42
41
  class << self
43
- attr_accessor :api_key, :base_url, :encoded_api_key
44
- end
45
-
46
- def self.request api_url, method, params={}
47
- raise AuthenticationError.new("Set your API Key using Onfleet.api_key = <API_KEY>") unless @api_key
42
+ attr_accessor :api_key, :base_url
48
43
 
49
- begin
50
- response = RestClient::Request.execute(method: method, url: self.base_url+api_url, payload: params.to_json, headers: self.request_headers)
44
+ def request(api_url, method, params = {})
45
+ raise(AuthenticationError, 'Set your API Key using Onfleet.api_key = <API_KEY>') unless api_key
51
46
 
52
- if response != ''
53
- JSON.parse(response)
54
- end
55
- rescue RestClient::ExceptionWithResponse => e
56
- if response_code = e.http_code and response_body = e.http_body
57
- handle_api_error(response_code, JSON.parse(response_body))
58
- else
47
+ begin
48
+ url = URI.join(base_url, api_url).to_s
49
+ response = RestClient::Request.execute(method: method, url: url, payload: params.to_json, headers: request_headers)
50
+ JSON.parse(response) unless response.empty?
51
+ rescue RestClient::ExceptionWithResponse => e
52
+ if (response_code = e.http_code) && (response_body = e.http_body)
53
+ handle_api_error(response_code, JSON.parse(response_body))
54
+ else
55
+ handle_restclient_error(e)
56
+ end
57
+ rescue RestClient::Exception, Errno::ECONNREFUSED => e
59
58
  handle_restclient_error(e)
60
59
  end
61
- rescue RestClient::Exception, Errno::ECONNREFUSED => e
62
- handle_restclient_error(e)
63
60
  end
64
- end
65
61
 
66
- private
67
- def self.request_headers
62
+ private
63
+
64
+ def request_headers
68
65
  {
69
- Authorization: "Basic #{self.encoded_api_key}"
66
+ Authorization: "Basic #{encoded_api_key}",
67
+ content_type: :json,
68
+ accept: :json
70
69
  }
71
70
  end
72
71
 
73
- def self.encoded_api_key
74
- @encoded_api_key ||= Base64.urlsafe_encode64(@api_key)
72
+ def encoded_api_key
73
+ @encoded_api_key ||= Base64.urlsafe_encode64(api_key)
75
74
  end
76
75
 
77
- def self.handle_api_error code, body
76
+ def handle_api_error(code, body)
78
77
  case code
79
78
  when 400, 404
80
- raise InvalidRequestError.new(body["message"])
79
+ raise InvalidRequestError, body['message']
81
80
  when 401
82
- raise AuthenticationError.new(body["message"])
81
+ raise AuthenticationError, body['message']
83
82
  else
84
- raise OnfleetError.new(body["message"])
83
+ raise OnfleetError, body['message']
85
84
  end
86
85
  end
87
86
 
88
- def self.handle_restclient_error e
89
- case e
90
- when RestClient::RequestTimeout
91
- message = "Could not connect to Onfleet. Check your internet connection and try again."
92
- when RestClient::ServerBrokeConnection
93
- message = "The connetion with onfleet terminated before the request completed. Please try again."
94
- else
95
- message = "There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com"
96
- end
87
+ def handle_restclient_error(exception)
88
+ message =
89
+ case exception
90
+ when RestClient::RequestTimeout
91
+ 'Could not connect to Onfleet. Check your internet connection and try again.'
92
+ when RestClient::ServerBrokeConnection
93
+ 'The connetion with onfleet terminated before the request completed. Please try again.'
94
+ else
95
+ 'There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com'
96
+ end
97
97
 
98
- raise ConnectionError.new(message)
98
+ raise ConnectionError, message
99
99
  end
100
+ end
100
101
  end
102
+
@@ -1,15 +1,18 @@
1
+ require 'active_support/core_ext/hash'
2
+
1
3
  module Onfleet
2
4
  module Actions
3
5
  module Create
4
6
  module ClassMethods
5
- def create params={}
6
- self.new(params).save
7
+ def create(params = {})
8
+ new(params.symbolize_keys.except(:id)).save
7
9
  end
8
10
  end
9
11
 
10
- def self.included base
12
+ def self.included(base)
11
13
  base.extend(ClassMethods)
12
14
  end
13
15
  end
14
16
  end
15
17
  end
18
+
@@ -2,16 +2,16 @@ module Onfleet
2
2
  module Actions
3
3
  module Delete
4
4
  module ClassMethods
5
- def delete id
6
- api_url = "#{self.api_url}/#{id}"
7
- response = Onfleet.request(api_url, :delete)
5
+ def delete(id)
6
+ Onfleet.request("#{api_url}/#{id}", :delete)
8
7
  true
9
8
  end
10
9
  end
11
10
 
12
- def self.included base
11
+ def self.included(base)
13
12
  base.extend(ClassMethods)
14
13
  end
15
14
  end
16
15
  end
17
16
  end
17
+
@@ -2,17 +2,19 @@ module Onfleet
2
2
  module Actions
3
3
  module Find
4
4
  module ClassMethods
5
- def find field, search_term
6
- encoded_term = URI::encode(search_term)
7
- api_url = "#{self.api_url}/#{field}/#{encoded_term}"
8
- response = Onfleet.request(api_url, :get, search_term)
9
- Util.constantize("#{self}").new(response)
5
+ def find(field, search_term)
6
+ encoded_term = URI.encode_www_form_component(search_term)
7
+ url = "#{api_url}/#{field}/#{encoded_term}"
8
+
9
+ response = Onfleet.request(url, :get)
10
+ new(response)
10
11
  end
11
12
  end
12
13
 
13
- def self.included base
14
+ def self.included(base)
14
15
  base.extend(ClassMethods)
15
16
  end
16
17
  end
17
18
  end
18
19
  end
20
+
@@ -2,16 +2,17 @@ module Onfleet
2
2
  module Actions
3
3
  module Get
4
4
  module ClassMethods
5
- def get id
5
+ def get(id)
6
6
  api_url = "#{self.api_url}/#{id}"
7
- response = Onfleet.request(api_url, :get)
8
- Util.constantize("#{self}").new(response)
7
+ response = Onfleet.request(api_url, :get)
8
+ Util.constantize(name).new(response)
9
9
  end
10
10
  end
11
11
 
12
- def self.included base
12
+ def self.included(base)
13
13
  base.extend(ClassMethods)
14
14
  end
15
15
  end
16
16
  end
17
17
  end
18
+
@@ -2,26 +2,28 @@ module Onfleet
2
2
  module Actions
3
3
  module List
4
4
  module ClassMethods
5
- def list query_params={}
6
- api_url = "#{self.api_url}"
5
+ def list(filters = {})
6
+ response = Onfleet.request(list_url_for(filters), :get)
7
+ response.compact.map { |item| new(item) }
8
+ end
9
+
10
+ private
7
11
 
8
- if !query_params.empty?
9
- api_url += "?"
10
- query_params.each do |key, value|
11
- api_url += "#{key}=#{value}&"
12
- end
13
- end
12
+ def list_url_for(filters)
13
+ [api_url, query_params(filters)].compact.join('?')
14
+ end
14
15
 
15
- response = Onfleet.request(api_url, :get)
16
- response.compact.map do |listObj|
17
- Util.constantize("#{self}").new(listObj)
18
- end
16
+ def query_params(filters)
17
+ filters && filters
18
+ .collect { |key, value| "#{key}=#{URI.encode_www_form_component(value)}" }
19
+ .join('&')
19
20
  end
20
21
  end
21
22
 
22
- def self.included base
23
+ def self.included(base)
23
24
  base.extend(ClassMethods)
24
25
  end
25
26
  end
26
27
  end
27
28
  end
29
+
@@ -2,16 +2,16 @@ module Onfleet
2
2
  module Actions
3
3
  module QueryMetadata
4
4
  module ClassMethods
5
- def query_by_metadata metadata
6
- api_url = "#{self.api_url}/metadata"
7
- response = Onfleet.request(api_url, :post, metadata)
8
- response.map { |item| Util.constantize("#{self}").new(item) } if response.is_a? Array
5
+ def query_by_metadata(metadata)
6
+ response = Onfleet.request("#{api_url}/metadata", :post, metadata)
7
+ [*response].compact.map { |item| new(item) }
9
8
  end
10
9
  end
11
10
 
12
- def self.included base
11
+ def self.included(base)
13
12
  base.extend(ClassMethods)
14
13
  end
15
14
  end
16
15
  end
17
16
  end
17
+
@@ -2,16 +2,20 @@ module Onfleet
2
2
  module Actions
3
3
  module Save
4
4
  def save
5
- if respond_to?('id') && self.id
6
- request_type = :put
7
- api_url = "#{self.api_url}/#{self.id}"
8
- else
9
- request_type = :post
10
- api_url = self.api_url
11
- end
12
- response = Onfleet.request(api_url, request_type, self.attributes)
13
- self.parse_response(response)
5
+ response = Onfleet.request(save_url, request_type, attributes)
6
+ parse_response(response)
7
+ end
8
+
9
+ private
10
+
11
+ def request_type
12
+ id ? :put : :post
13
+ end
14
+
15
+ def save_url
16
+ [api_url, id].compact.join('/')
14
17
  end
15
18
  end
16
19
  end
17
20
  end
21
+
@@ -2,15 +2,15 @@ module Onfleet
2
2
  module Actions
3
3
  module Update
4
4
  module ClassMethods
5
- def update id, params
6
- params.merge!(id: id)
7
- self.new(params).save
5
+ def update(id, params)
6
+ new(params.merge(id: id)).save
8
7
  end
9
8
  end
10
9
 
11
- def self.included base
10
+ def self.included(base)
12
11
  base.extend(ClassMethods)
13
12
  end
14
13
  end
15
14
  end
16
15
  end
16
+
@@ -2,3 +2,4 @@ module Onfleet
2
2
  class Address < OnfleetObject
3
3
  end
4
4
  end
5
+
@@ -8,7 +8,8 @@ module Onfleet
8
8
  include Onfleet::Actions::QueryMetadata
9
9
 
10
10
  def self.api_url
11
- '/admins'
11
+ 'admins'
12
12
  end
13
13
  end
14
14
  end
15
+