onfleet-ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d7d26f7373bddd5060db0f7aadda92c8896fe089
4
+ data.tar.gz: af207526096c436c17ed7cc766558637d8847e32
5
+ SHA512:
6
+ metadata.gz: cbea220cb12f1dba49722f296f13f3c7e412ecdd8685f0e04b73f058b62299312ffce454b0624c8b19d53034c61272cf6dbdf2065be0aa519a812d2296043361
7
+ data.tar.gz: ef25b4472e269677728b9c333cc0ef25893085c6ec426e50a0bb46bc3c733a774e2ddcf311f4bfc8edd145312cc117a279b849109ba38e77d853b35807238e10
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ *.rbc
3
+ /tmp/
4
+
5
+ # Ignore bundler config
6
+ /.bundle
7
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+ gem 'rest-client', '~> 1.6.8'
5
+
6
+
data/README.md ADDED
@@ -0,0 +1,293 @@
1
+ # Onfleet Ruby SDK
2
+
3
+ Ruby SDK for Onfleet.com's API. **This is NOT an official SDK**. [Official Documentation](http://docs.onfleet.com/docs/). The entirety of the response payload is accessible by instance methods. The raw response can is accessible through the `params` instance method on all objects.
4
+
5
+
6
+ ## Usage
7
+ Install the gem.
8
+ ```bash
9
+ gem install onfleet-ruby
10
+ ```
11
+
12
+ Set the API Key.
13
+ ```ruby
14
+ Onfleet.api_key = API_KEY
15
+ ```
16
+
17
+ Objects
18
+ ```ruby
19
+ Onfleet::Organization
20
+ Onfleet::Admin
21
+ Onfleet::Worker
22
+ Onfleet::Team
23
+ Onfleet::Destination
24
+ Onfleet::Recipient
25
+ Onfleet::Task
26
+ ```
27
+
28
+ ## Organizations
29
+
30
+ **GET**
31
+
32
+ ```ruby
33
+ org = Onfleet::Organization.get
34
+ org.id # => 1234567890
35
+ org.name # => OrgName
36
+ org.email # => org@email.com
37
+ org.country # => United States
38
+ org.delegatees # => ['delegatees_id']
39
+ org.timezone # => "America/Los_Angeles"
40
+ org.time_created # => 1438713844000
41
+ org.time_last_modified # => 1438713844000
42
+ ```
43
+
44
+ **GET delegatee details**
45
+
46
+ ```ruby
47
+ delegatee = Onfleet::Organization.get_delegatee_details(id)
48
+ delegatee.id # => "4eKRvRGA7JW6C8TaGyuJeSrK"
49
+ delegatee.name # => "North Beach Runners"
50
+ delegatee.email # => "hello@nbr.co"
51
+ delegatee.timezone # => "America/Los_Angeles"
52
+ delegatee.country # => "US"
53
+ ```
54
+
55
+
56
+ ## Administrators
57
+
58
+
59
+ | Name | Type | Description |
60
+ | ----------- |--------| --------------|
61
+ | name | string | The administrator’s complete name. |
62
+ | email | string | The administrator’s email address. |
63
+ | phone | string | (Optional) The administrator's E.164-formatted phone number. |
64
+
65
+ **Create**
66
+
67
+ ```ruby
68
+ admin = Onfleet::Admin.create({name: 'John Doe', email: 'john@company.com', phone: '41555546782'})
69
+ ```
70
+
71
+ **Update**
72
+ ```ruby
73
+ admin = Onfleet::Admin.update('ADMIN_ID', {name: 'New Name'})
74
+ # or
75
+ admin.name = "New Name"
76
+ admin.save
77
+ ```
78
+
79
+ **Delete**
80
+ ```ruby
81
+ Onfleet::Admin.delete('ADMIN_ID') # => true
82
+ ```
83
+
84
+ **List**
85
+ ```ruby
86
+ list = Onfleet::Admin.list # => [<Onfleet::Admin>]
87
+ list.first # => Onfleet::Admin
88
+ ```
89
+
90
+ ## Workers
91
+ Worker
92
+
93
+ | Name | Type | Description |
94
+ | ----------- |--------| --------------|
95
+ | name | string | The workers complete name. |
96
+ | phone | string | The worker's phone number. |
97
+ | teams | string Array | One or more team IDs of which the worker is a member.|
98
+ | vehicle | obect | (Optional) The worker’s vehicle, providing no vehicle details is interpreted as the worker being on foot. |
99
+
100
+ Vehicle
101
+
102
+ | Name | Type | Description |
103
+ | ------------- |--------| --------------|
104
+ | type | string | The vehicle’s type, must be one of `CAR`, `MOTORCYCLE`, `BICYCLE` or `TRUCK`. |
105
+ | description | string | (Optional) The vehicle’s make, model, year, or any other relevant identifying details. |
106
+ | license_plate | string | (Optional) The vehicle’s license plate number.|
107
+ | color | string | (Optional) The worker’s vehicle, providing no vehicle details is interpreted as the worker being on foot. |
108
+
109
+
110
+ **Create**
111
+
112
+ ```ruby
113
+ worker = Onfleet::Worker.create({name: 'John Doe', email: 'john@company.com', teams: ["TEAM_ID"], vehicle: {type: 'CAR'} })
114
+ worker.name # => "John Doe"
115
+ worker.vehicle.type # => "CAR"
116
+
117
+ worker.vehicle.color = "Blue"
118
+ worker.save
119
+
120
+ worker.vehicle.color # => "Blue"
121
+ ```
122
+
123
+ **Update**
124
+ ```ruby
125
+ Onfleet::Worker.update({name: "New Name"}
126
+ # or
127
+ worker.name = "New Name"
128
+ work.save
129
+ worker.name # => "New Name"
130
+ ```
131
+
132
+ **Delete**
133
+ ```ruby
134
+ Onfleet::Worker.delete('WORKER_ID') # => true
135
+ ```
136
+
137
+ **List**
138
+ ```ruby
139
+ Onfleet::Worker.list
140
+ ```
141
+
142
+ **Get**
143
+ ```ruby
144
+ worker = Onfleet::Worker.get('WORKER_ID')
145
+ ```
146
+
147
+ ## Teams
148
+
149
+ **List**
150
+ ```ruby
151
+ Onfleet::Team.list
152
+ ```
153
+
154
+ **Get**
155
+ ```ruby
156
+ Onfleet::Team.get('TEAM_ID')
157
+ ```
158
+
159
+ ## Destinations
160
+ Destination
161
+
162
+ | Name | Type | Description |
163
+ | ----------- |--------| --------------|
164
+ | address | object | The destination’s street address details. |
165
+ | location | array | (Optional) The `[ longitude, latitude ]` geographic coordinates. If missing, the API will geocode based on the `address` details provided. Note that geocoding may slightly modify the format of the address properties. |
166
+ | notes | string | (Optional) Notes for the destination |
167
+
168
+ Address
169
+
170
+ | Name | Type | Description |
171
+ | ----------- |--------| --------------|
172
+ | number | string | The number component of this address, it may also contain letters. |
173
+ | street | string | The street name |
174
+ | city | string | The city name |
175
+ | country | string | Name Of Country|
176
+ | apartment | string | (Optional) The apartment or suite number |
177
+ | name | string | (Optional) A name associated with this address |
178
+ | state | string | (Optional) State name |
179
+ | postal_code | string | (Optional) The postal code |
180
+ | unparsed | string | (Optional) A complete comma seperated address for ex. `148 townsend, 94102, USA`. Including this field, all other address details will be ignored. The address will be automatically geocoded. |
181
+
182
+
183
+
184
+
185
+ **Create**
186
+ ```ruby
187
+ destination = Onfleet::Destination.create({address: {unparsed: '200 12th st, 94103, ca'} })
188
+
189
+ destination.street # => '12th street'
190
+ destination.number # => '200'
191
+ destination.postal_code # => '94103'
192
+ ```
193
+
194
+ **Get**
195
+ ```ruby
196
+ Onfleet::Destination('DEST_ID')
197
+ ```
198
+
199
+ ## Recipients
200
+ | Name | Type | Description |
201
+ | ----------- |--------| --------------|
202
+ | name | string | The recipient's full name. |
203
+ | phone | string | A unique valid phone number. |
204
+ | notes | string | (Optional) Notes for the recipient. |
205
+ | skip_sms_notifications | boolean | (Optional) To disable sms notification. Defaults to `false` |
206
+ | skip_phone_number_verificaton | boolean | (Optional) Whether to skip validation of the phone number. |
207
+
208
+ **Create**
209
+ ```ruby
210
+ recipient = Onfleet::Recipient.create({name: 'John Doe', phone: '4155556789'})
211
+ recipient.id # => ChdA82dA~Dn232
212
+ recpient.name # => 'John Doe'
213
+ ```
214
+
215
+ **Update**
216
+ ```ruby
217
+ Onfleet::Recipient.update('REC_ID', {name: 'New Name'})
218
+ # or
219
+ recipient.name = "New Name"
220
+ recipient.save
221
+ ```
222
+
223
+ **Get**
224
+ ```ruby
225
+ Onfleet::Recipient.get('REC_ID')
226
+ ```
227
+
228
+ **Find**
229
+
230
+ >######Note:
231
+ **Throws `InvalidRequestError` if cannot find resource**
232
+
233
+ ```ruby
234
+ # by name (case sensitive)
235
+ rec = Onfleet::Recipient.find('name', 'John Doe')
236
+ rec.name = "John Doe"
237
+
238
+ #by phone
239
+ rec = Onfleet::Recipient.find('phone', '4155556789')
240
+ ```
241
+
242
+ ##Tasks
243
+ | Name | Type | Description |
244
+ | ----------- |--------| --------------|
245
+ | destination | string | `ID` of the destination. |
246
+ | recipients | string array | An array containing zero or one IDs of the task's recipients |
247
+ | merchant | string | (Optional) `ID` of merchant organization. |
248
+ | executor | string | (Optional) `ID` of the executor organization. |
249
+ | complete_after | number | (Optional) A timestamp for the earliest time the task should be completed. |
250
+ | complete_before | number | (Optional) A timestamp for the latest time the task should be completed. |
251
+ | pickup_task | boolean | (Optional) Whether the task is a pickup task. |
252
+ | dependencies | string array | (Optional) One or more IDs of tasks which must be completed prior to this task. |
253
+ | notes | notes | (Optional) Notes for the task. |
254
+ | auto_assign | object | (Optional) The automatic assignment options for the newly created task. See above for exact object structure and allowed values. |
255
+
256
+ **Create**
257
+ ```ruby
258
+ # First Create a destination and Recipient
259
+ # Then create the task
260
+ task = Onfleet::Task.create({recipient: ['REC_ID'], destination: 'DEC_ID' })
261
+ ```
262
+
263
+ **Update**
264
+ ```ruby
265
+ Onfleet::Task.update('TASK_ID', {notes: 'Adding some notes'})
266
+ # or
267
+ task.notes = "Adding some notes"
268
+ task.save
269
+ ```
270
+
271
+ **Get**
272
+ ```ruby
273
+ task = Onfleet::Task.get('TASK_ID')
274
+ ```
275
+
276
+ **Delete**
277
+ ```ruby
278
+ Onfleet::Task.delete('TASK_ID')
279
+ ```
280
+ **List**
281
+ ```ruby
282
+ Onfleet::Task.list
283
+ # You can also list tasks of certain states
284
+ Onfleet::Task.list({state: 0}) # => returns all tasks with state 0, see official docs for valid states
285
+ ```
286
+
287
+ **Complete**
288
+ Currently not supported
289
+
290
+
291
+ ## TODO
292
+ 1. Tests
293
+ 2. Better error handling
@@ -0,0 +1,15 @@
1
+ module Onfleet
2
+ module Actions
3
+ module Create
4
+ module ClassMethods
5
+ def create params={}
6
+ self.new(params).save
7
+ end
8
+ end
9
+
10
+ def self.included base
11
+ base.extend(ClassMethods)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module Onfleet
2
+ module Actions
3
+ module Delete
4
+ module ClassMethods
5
+ def delete id
6
+ url = "#{self.url}/#{id}"
7
+ response = Onfleet.request(url, :delete)
8
+ true
9
+ end
10
+ end
11
+
12
+ def self.included base
13
+ base.extend(ClassMethods)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Onfleet
2
+ module Actions
3
+ module Find
4
+ module ClassMethods
5
+ def find field, search_term
6
+ url = "#{self.url}/#{field}/#{search_term}"
7
+ response = Onfleet.request(url, :get, search_term)
8
+ Util.constantize("#{self}").new(response)
9
+ end
10
+ end
11
+
12
+ def self.included base
13
+ base.extend(ClassMethods)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Onfleet
2
+ module Actions
3
+ module Get
4
+ module ClassMethods
5
+ def get id
6
+ url = "#{self.url}/#{id}"
7
+ response = Onfleet.request(url, :get)
8
+ Util.constantize("#{self}").new(response)
9
+ end
10
+ end
11
+
12
+ def self.included base
13
+ base.extend(ClassMethods)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ module Onfleet
2
+ module Actions
3
+ module List
4
+ module ClassMethods
5
+ def list query_params={}
6
+ url = "#{self.url}"
7
+
8
+ if !query_params.empty?
9
+ url += "?"
10
+ query_params.each do |key, value|
11
+ url += "#{key}=#{value}&"
12
+ end
13
+ end
14
+
15
+ response = Onfleet.request(url, :get)
16
+ response.compact.map do |listObj|
17
+ Util.constantize("#{self}").new(listObj)
18
+ end
19
+ end
20
+ end
21
+
22
+ def self.included base
23
+ base.extend(ClassMethods)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ module Onfleet
2
+ module Actions
3
+ module Save
4
+ def save
5
+ if respond_to?('id') && self.id
6
+ request_type = :put
7
+ url = "#{self.url}/#{self.id}"
8
+ else
9
+ request_type = :post
10
+ url = self.url
11
+ end
12
+ response = Onfleet.request(url, request_type, self.attributes)
13
+ self.parse_response(response)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module Onfleet
2
+ module Actions
3
+ module Update
4
+ module ClassMethods
5
+ def update id, params
6
+ params.merge!(id: id)
7
+ self.new(params).save
8
+ end
9
+ end
10
+
11
+ def self.included base
12
+ base.extend(ClassMethods)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ module Onfleet
2
+ class Address < OnfleetObject
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ module Onfleet
2
+ class Admin < OnfleetObject
3
+ include Onfleet::Actions::Create
4
+ include Onfleet::Actions::Save
5
+ include Onfleet::Actions::Update
6
+ include Onfleet::Actions::List
7
+ include Onfleet::Actions::Delete
8
+
9
+
10
+ def self.url
11
+ '/admins'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module Onfleet
2
+ class Destination < OnfleetObject
3
+ include Onfleet::Actions::Create
4
+ include Onfleet::Actions::Save
5
+ include Onfleet::Actions::Get
6
+
7
+
8
+ def self.url
9
+ '/destinations'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Onfleet
2
+ class AuthenticationError < OnfleetError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Onfleet
2
+ class ConnectionError < OnfleetError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Onfleet
2
+ class InvalidRequestError < OnfleetError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Onfleet
2
+ class OnfleetError < StandardError; end
3
+ end
@@ -0,0 +1,102 @@
1
+ module Onfleet
2
+ class OnfleetObject
3
+ attr_reader :params
4
+ def initialize params
5
+ if params.kind_of?(Hash)
6
+ @params = params
7
+ set_attributes(@params)
8
+ elsif params.kind_of?(String)
9
+ @params = {id: params}
10
+ set_attributes(@params)
11
+ else
12
+ @params = {}
13
+ end
14
+ end
15
+
16
+ def parse_response response
17
+ @params = response
18
+ set_attributes(response)
19
+ self
20
+ end
21
+
22
+ def attributes
23
+ attrs = Hash.new
24
+ instance_variables.each do |var|
25
+ str = var.to_s.gsub /^@/, ''
26
+ if respond_to? "#{str}="
27
+ instance_var = instance_variable_get(var)
28
+ if klass = Util.object_classes[str]
29
+ if instance_var.is_a?(OnfleetObject)
30
+ attrs[Util.to_camel_case_lower(str).to_sym] = parse_onfleet_obj(instance_var)
31
+ elsif instance_var.is_a?(Array)
32
+ objs = []
33
+ instance_var.each do |object|
34
+ objs << parse_onfleet_obj(object)
35
+ end
36
+ attrs[Util.to_camel_case_lower(str).to_sym] = objs
37
+ else
38
+ attrs[Util.to_camel_case_lower(str).to_sym] = instance_var
39
+ end
40
+ else
41
+ attrs[Util.to_camel_case_lower(str).to_sym] = instance_var
42
+ end
43
+ end
44
+ end
45
+ attrs
46
+ end
47
+
48
+ def class_name
49
+ self.class.name.split("::").last
50
+ end
51
+
52
+ def url
53
+ "/#{CGI.escape(class_name.downcase)}s"
54
+ end
55
+
56
+ private
57
+
58
+ def parse_onfleet_obj obj
59
+ if obj.is_a?(OnfleetObject)
60
+ if obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task)
61
+ obj.id
62
+ else
63
+ obj.attributes
64
+ end
65
+ end
66
+ end
67
+
68
+ def set_attributes params
69
+ params.each do |key, value|
70
+ key_underscore = Util.to_underscore(key)
71
+
72
+ if klass = Util.object_classes[key.to_s]
73
+ case value
74
+ when Array
75
+ objs = []
76
+ value.each do |v|
77
+ objs << klass.new(v)
78
+ end
79
+ value = objs
80
+ when Hash
81
+ value = klass.new(value)
82
+ end
83
+ end
84
+
85
+ if respond_to?("#{key_underscore}=")
86
+ send(:"#{key_underscore}=", value)
87
+ else
88
+ add_attrs({"#{key_underscore}" => value})
89
+ end
90
+
91
+ end
92
+ end
93
+
94
+ def add_attrs attrs
95
+ attrs.each do |var, value|
96
+ self.class.class_eval { attr_accessor var }
97
+ instance_variable_set "@#{var}", value
98
+ end
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,19 @@
1
+ module Onfleet
2
+ class Organization < OnfleetObject
3
+
4
+ class << self
5
+ def get
6
+ url = "/organization"
7
+ response = Onfleet.request(url, :get)
8
+ Util.constantize("#{self}").new(response)
9
+ end
10
+
11
+ def get_delegatee_details id
12
+ url = "/organizations/#{id}"
13
+ response = Onfleet.request(url, :get)
14
+ Util.constantize("#{self}").new(response)
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ module Onfleet
2
+ class Recipient < OnfleetObject
3
+ include Onfleet::Actions::Create
4
+ include Onfleet::Actions::Update
5
+ include Onfleet::Actions::Save
6
+ include Onfleet::Actions::Find
7
+ include Onfleet::Actions::Get
8
+
9
+ def self.url
10
+ "/recipients"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module Onfleet
2
+ class Task < OnfleetObject
3
+ include Onfleet::Actions::Create
4
+ include Onfleet::Actions::Save
5
+ include Onfleet::Actions::Update
6
+ include Onfleet::Actions::Get
7
+ include Onfleet::Actions::List
8
+ include Onfleet::Actions::Delete
9
+
10
+
11
+ def self.url
12
+ '/tasks'
13
+ end
14
+
15
+ def complete
16
+ # CURRENTLY DOESN'T WORK
17
+ url = "#{self.url}/#{self.id}/complete"
18
+ params = {"completionDetails" => {"success" => true }}
19
+ Onfleet.request(url, :post, params)
20
+ true
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ module Onfleet
2
+ class Team < OnfleetObject
3
+ include Onfleet::Actions::List
4
+ include Onfleet::Actions::Get
5
+
6
+ def self.url
7
+ '/teams'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,35 @@
1
+ module Onfleet
2
+ class Util
3
+ def self.constantize class_name
4
+ Object.const_get(class_name)
5
+ end
6
+
7
+ def self.to_underscore key
8
+ if key.kind_of?(Symbol)
9
+ key = key.to_s
10
+ end
11
+ key.gsub(/::/, '/').
12
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
13
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
14
+ tr("-", "_").
15
+ downcase
16
+ end
17
+
18
+ def self.to_camel_case_lower str
19
+ str.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
20
+
21
+ end
22
+
23
+ def self.object_classes
24
+ @object_classes ||= {
25
+ "address" => Address,
26
+ "recipients" => Recipient,
27
+ "recipient" => Recipient,
28
+ "tasks" => Task,
29
+ "destination" => Destination,
30
+ "vehicle" => Vehicle
31
+ }
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,4 @@
1
+ module Onfleet
2
+ class Vehicle < OnfleetObject
3
+ end
4
+ end
@@ -0,0 +1,15 @@
1
+ module Onfleet
2
+ class Worker < OnfleetObject
3
+ include Onfleet::Actions::Create
4
+ include Onfleet::Actions::List
5
+ include Onfleet::Actions::Get
6
+ include Onfleet::Actions::Save
7
+ include Onfleet::Actions::Update
8
+ include Onfleet::Actions::Delete
9
+
10
+
11
+ def self.url
12
+ '/workers'
13
+ end
14
+ end
15
+ end
data/lib/onfleet.rb ADDED
@@ -0,0 +1,98 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'base64'
4
+
5
+ # Utils
6
+ require 'onfleet/Util'
7
+
8
+ # Errors
9
+ require 'onfleet/errors/onfleet_error'
10
+ require 'onfleet/errors/authentication_error'
11
+ require 'onfleet/errors/invalid_request_error'
12
+ require 'onfleet/errors/connection_error'
13
+
14
+ # Actions
15
+ require 'onfleet/actions/create'
16
+ require 'onfleet/actions/find'
17
+ require 'onfleet/actions/save'
18
+ require 'onfleet/actions/update'
19
+ require 'onfleet/actions/get'
20
+ require 'onfleet/actions/list'
21
+ require 'onfleet/actions/delete'
22
+
23
+
24
+ # Resources
25
+ require 'onfleet/onfleet_object'
26
+ require 'onfleet/recipient'
27
+ require 'onfleet/destination'
28
+ require 'onfleet/address'
29
+ require 'onfleet/task'
30
+ require 'onfleet/organization'
31
+ require 'onfleet/admin'
32
+ require 'onfleet/team'
33
+ require 'onfleet/vehicle'
34
+ require 'onfleet/worker'
35
+
36
+
37
+ module Onfleet
38
+ @base_url = "https://onfleet.com/api/v2"
39
+
40
+ class << self
41
+ attr_accessor :api_key, :base_url, :encoded_api_key
42
+ end
43
+
44
+ def self.request url, method, params={}
45
+ raise AuthenticationError.new("Set your API Key using Onfleet.api_key = <API_KEY>") unless @api_key
46
+
47
+ begin
48
+ response = RestClient::Request.execute(method: method, url: self.base_url+url, payload: params.to_json, headers: self.request_headers)
49
+
50
+ if response != ''
51
+ JSON.parse(response)
52
+ end
53
+ rescue RestClient::ExceptionWithResponse => e
54
+ if response_code = e.http_code and response_body = e.http_body
55
+ handle_api_error(response_code, JSON.parse(response_body))
56
+ else
57
+ handle_restclient_error(e)
58
+ end
59
+ rescue RestClient::Exception, Errno::ECONNREFUSED => e
60
+ handle_restclient_error(e)
61
+ end
62
+ end
63
+
64
+ private
65
+ def self.request_headers
66
+ {
67
+ Authorization: "Basic #{self.encoded_api_key}"
68
+ }
69
+ end
70
+
71
+ def self.encoded_api_key
72
+ @encoded_api_key ||= Base64.urlsafe_encode64(@api_key)
73
+ end
74
+
75
+ def self.handle_api_error code, body
76
+ case code
77
+ when 400, 404
78
+ raise InvalidRequestError.new(body["message"])
79
+ when 401
80
+ raise AuthenticationError.new(body["message"])
81
+ else
82
+ raise OnfleetError.new(body["message"])
83
+ end
84
+ end
85
+
86
+ def self.handle_restclient_error e
87
+ case e
88
+ when RestClient::RequestTimeout
89
+ message = "Could not connect to Onfleet. Check your internet connection and try again."
90
+ when RestClient::ServerBrokeConnection
91
+ message = "The connetion with onfleet terminated before the request completed. Please try again."
92
+ else
93
+ message = "There was a problem connection with Onfleet. Please try again. If the problem persists contact contact@onfleet.com"
94
+ end
95
+
96
+ raise ConnectionError.new(message)
97
+ end
98
+ end
data/onfleet.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'onfleet-ruby'
3
+ s.version = '0.0.1'
4
+ s.date = '2015-08-03'
5
+ s.summary = "Onfleet ruby api"
6
+ s.description = "To interact with Onfleet's API"
7
+ s.authors = ["Nick Wargnier"]
8
+ s.email = 'nick@stylelend.com'
9
+ s.homepage =
10
+ 'http://rubygems.org/gems/onfleet'
11
+ s.license = 'MIT'
12
+
13
+ s.add_dependency('rest-client', '~> 1.4')
14
+
15
+ s.add_development_dependency "rspec"
16
+
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.require_paths = ['lib']
21
+ end
@@ -0,0 +1,2 @@
1
+ require 'onfleet'
2
+ require File.expand_path('./test_data', __FILE__)
data/spec/test_data.rb ADDED
@@ -0,0 +1,14 @@
1
+ module Onfleet
2
+ module TestData
3
+ def recipient
4
+
5
+ end
6
+
7
+ def destination
8
+
9
+ end
10
+
11
+ def task
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onfleet-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nick Wargnier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: To interact with Onfleet's API
42
+ email: nick@stylelend.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - .rspec
49
+ - Gemfile
50
+ - README.md
51
+ - lib/onfleet.rb
52
+ - lib/onfleet/actions/create.rb
53
+ - lib/onfleet/actions/delete.rb
54
+ - lib/onfleet/actions/find.rb
55
+ - lib/onfleet/actions/get.rb
56
+ - lib/onfleet/actions/list.rb
57
+ - lib/onfleet/actions/save.rb
58
+ - lib/onfleet/actions/update.rb
59
+ - lib/onfleet/address.rb
60
+ - lib/onfleet/admin.rb
61
+ - lib/onfleet/destination.rb
62
+ - lib/onfleet/errors/authentication_error.rb
63
+ - lib/onfleet/errors/connection_error.rb
64
+ - lib/onfleet/errors/invalid_request_error.rb
65
+ - lib/onfleet/errors/onfleet_error.rb
66
+ - lib/onfleet/onfleet_object.rb
67
+ - lib/onfleet/organization.rb
68
+ - lib/onfleet/recipient.rb
69
+ - lib/onfleet/task.rb
70
+ - lib/onfleet/team.rb
71
+ - lib/onfleet/util.rb
72
+ - lib/onfleet/vehicle.rb
73
+ - lib/onfleet/worker.rb
74
+ - onfleet.gemspec
75
+ - spec/spec_helper.rb
76
+ - spec/test_data.rb
77
+ homepage: http://rubygems.org/gems/onfleet
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Onfleet ruby api
101
+ test_files:
102
+ - spec/spec_helper.rb
103
+ - spec/test_data.rb