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.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/.rspec +1 -2
- data/.rubocop.yml +26 -0
- data/.rubocop_todo.yml +38 -0
- data/.ruby-version +2 -0
- data/Gemfile +3 -3
- data/Rakefile +10 -0
- data/lib/onfleet-ruby.rb +40 -38
- data/lib/onfleet-ruby/actions/create.rb +6 -3
- data/lib/onfleet-ruby/actions/delete.rb +4 -4
- data/lib/onfleet-ruby/actions/find.rb +8 -6
- data/lib/onfleet-ruby/actions/get.rb +5 -4
- data/lib/onfleet-ruby/actions/list.rb +15 -13
- data/lib/onfleet-ruby/actions/query_metadata.rb +5 -5
- data/lib/onfleet-ruby/actions/save.rb +13 -9
- data/lib/onfleet-ruby/actions/update.rb +4 -4
- data/lib/onfleet-ruby/address.rb +1 -0
- data/lib/onfleet-ruby/admin.rb +2 -1
- data/lib/onfleet-ruby/destination.rb +2 -1
- data/lib/onfleet-ruby/errors/authentication_error.rb +1 -0
- data/lib/onfleet-ruby/errors/connection_error.rb +1 -0
- data/lib/onfleet-ruby/errors/invalid_request_error.rb +1 -0
- data/lib/onfleet-ruby/errors/onfleet_error.rb +1 -0
- data/lib/onfleet-ruby/onfleet_object.rb +58 -59
- data/lib/onfleet-ruby/organization.rb +4 -9
- data/lib/onfleet-ruby/recipient.rb +2 -1
- data/lib/onfleet-ruby/task.rb +4 -4
- data/lib/onfleet-ruby/team.rb +2 -1
- data/lib/onfleet-ruby/util.rb +22 -20
- data/lib/onfleet-ruby/vehicle.rb +1 -0
- data/lib/onfleet-ruby/webhook.rb +2 -2
- data/lib/onfleet-ruby/worker.rb +2 -1
- data/onfleet-ruby.gemspec +11 -6
- data/spec/onfleet/admin_spec.rb +70 -0
- data/spec/onfleet/destination_spec.rb +62 -0
- data/spec/onfleet/organization_spec.rb +25 -0
- data/spec/onfleet/recipient_spec.rb +75 -0
- data/spec/onfleet/task_spec.rb +123 -0
- data/spec/onfleet/team_spec.rb +30 -0
- data/spec/onfleet/webhook_spec.rb +49 -0
- data/spec/onfleet/worker_spec.rb +67 -0
- data/spec/spec_helper.rb +78 -2
- data/spec/support/http_requests/shared_examples.rb +126 -0
- metadata +97 -13
- data/spec/test_data.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5fd31c23992d29f9cc2eed9330212acf71eab0988dbb1d7d99f9c06447af56f6
|
4
|
+
data.tar.gz: 82499d975b1ecaf3487096580a775a00d08c5f59d4dd7769627e1c1800dfc6fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01ab99e61bcadbb621cfe07fbc23aa36454f7972c4dbeb70cb8f69547c09113de7a1d13f57109d910abfa1f30f9bfed31658afa6626a3c114b1d815ce5496bda
|
7
|
+
data.tar.gz: 76795dae4abf9bb143a5226617128aa08ed6b97a937384ef806386a20bc2730f42503553795b7e27291f353077af3e5c6147639a590f278d9dbefee05a6d8b81
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
--
|
2
|
-
--format documentation
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
@@ -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
|
+
|
data/.rubocop_todo.yml
ADDED
@@ -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
|
+
|
data/.ruby-version
ADDED
data/Gemfile
CHANGED
data/Rakefile
ADDED
data/lib/onfleet-ruby.rb
CHANGED
@@ -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 =
|
39
|
+
@base_url = 'https://onfleet.com/api/v2/'
|
41
40
|
|
42
41
|
class << self
|
43
|
-
attr_accessor :api_key, :base_url
|
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
|
-
|
50
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
67
|
-
|
62
|
+
private
|
63
|
+
|
64
|
+
def request_headers
|
68
65
|
{
|
69
|
-
Authorization: "Basic #{
|
66
|
+
Authorization: "Basic #{encoded_api_key}",
|
67
|
+
content_type: :json,
|
68
|
+
accept: :json
|
70
69
|
}
|
71
70
|
end
|
72
71
|
|
73
|
-
def
|
74
|
-
@encoded_api_key ||= Base64.urlsafe_encode64(
|
72
|
+
def encoded_api_key
|
73
|
+
@encoded_api_key ||= Base64.urlsafe_encode64(api_key)
|
75
74
|
end
|
76
75
|
|
77
|
-
def
|
76
|
+
def handle_api_error(code, body)
|
78
77
|
case code
|
79
78
|
when 400, 404
|
80
|
-
raise InvalidRequestError
|
79
|
+
raise InvalidRequestError, body['message']
|
81
80
|
when 401
|
82
|
-
raise AuthenticationError
|
81
|
+
raise AuthenticationError, body['message']
|
83
82
|
else
|
84
|
-
raise OnfleetError
|
83
|
+
raise OnfleetError, body['message']
|
85
84
|
end
|
86
85
|
end
|
87
86
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
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
|
6
|
-
|
7
|
+
def create(params = {})
|
8
|
+
new(params.symbolize_keys.except(:id)).save
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
10
|
-
def self.included
|
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
|
6
|
-
|
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
|
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
|
6
|
-
encoded_term = URI
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
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
|
5
|
+
def get(id)
|
6
6
|
api_url = "#{self.api_url}/#{id}"
|
7
|
-
response
|
8
|
-
Util.constantize(
|
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
|
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
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
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
|
6
|
-
|
7
|
-
response
|
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
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
6
|
-
params.merge
|
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
|
10
|
+
def self.included(base)
|
12
11
|
base.extend(ClassMethods)
|
13
12
|
end
|
14
13
|
end
|
15
14
|
end
|
16
15
|
end
|
16
|
+
|
data/lib/onfleet-ruby/address.rb
CHANGED