onfleet 1.0.0
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.
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +114 -0
- data/Rakefile +2 -0
- data/lib/onfleet.rb +11 -0
- data/lib/onfleet/api.rb +44 -0
- data/lib/onfleet/api_category.rb +127 -0
- data/lib/onfleet/api_logger.rb +19 -0
- data/lib/onfleet/onfleet_error.rb +5 -0
- data/lib/onfleet/version.rb +3 -0
- data/onfleet.gemspec +26 -0
- metadata +122 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 siddharth@vinsol.com
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# Onfleet
|
2
|
+
|
3
|
+
Onfleet is an API wrapper for [Onfleet's APIs](http://docs.onfleet.com).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'onfleet'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install onfleet
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
You can create an instance of the API wrapper:
|
24
|
+
|
25
|
+
onfleet = Onfleet::API.new("your_api_key")
|
26
|
+
|
27
|
+
You can set `api_key`, `timeout`, `throws_exceptions`, `retry_if_fails` and `logger` globally:
|
28
|
+
|
29
|
+
Onfleet::API.api_key = "your_api_key"
|
30
|
+
Onfleet::API.timeout = 15
|
31
|
+
Onfleet::API.throws_exceptions = false
|
32
|
+
Onfleet::API.retry_if_fails = true
|
33
|
+
Onfleet::API.logger = Logger.new("#{Rails.root}/log/onfleet.log")
|
34
|
+
|
35
|
+
|
36
|
+
For example, you could set the values above in an `initializer` file in your `Rails` app (e.g. your\_app/config/initializers/onfleet.rb).
|
37
|
+
|
38
|
+
Assuming you've set an `api_key` on Onfleet, you can conveniently make API calls on the class itself:
|
39
|
+
|
40
|
+
Onfleet::API.tasks.all
|
41
|
+
|
42
|
+
You can also set the environment variable `ONFLEET_API_KEY` and Onfleet will use it when you create an instance:
|
43
|
+
|
44
|
+
onfleet = Onfleet::API.new
|
45
|
+
|
46
|
+
|
47
|
+
### Fetching Tasks
|
48
|
+
|
49
|
+
For example, to fetch all the tasks of your organisation:
|
50
|
+
|
51
|
+
tasks = onfleet.tasks.all
|
52
|
+
|
53
|
+
### Fetching Destinations
|
54
|
+
|
55
|
+
Similarly, to fetch your destinations:
|
56
|
+
|
57
|
+
lists = onfleet.destinations.all
|
58
|
+
|
59
|
+
Or, to fetch a task by id:
|
60
|
+
|
61
|
+
task = onfleet.tasks.find('task_id')
|
62
|
+
|
63
|
+
Or, to delete a task by id:
|
64
|
+
|
65
|
+
task = onfleet.tasks.delete('task_id')
|
66
|
+
|
67
|
+
|
68
|
+
Or, to update a task by id:
|
69
|
+
|
70
|
+
task = onfleet.tasks.delete(id: 'task_id', other_updating_params: value)
|
71
|
+
|
72
|
+
passing id to update any resource is necessory
|
73
|
+
|
74
|
+
The above examples were for only task resource. Same way it can be used for other resources i.e. organization,
|
75
|
+
admins, workers, teams, destinations, recipients, tasks, webhooks.
|
76
|
+
|
77
|
+
### Setting timeouts
|
78
|
+
|
79
|
+
Onfleet defaults to a 30 second timeout. You can optionally set your own timeout (in seconds) like so:
|
80
|
+
|
81
|
+
onfleet = Onfleet::API.new("your_api_key", {:timeout => 5})
|
82
|
+
|
83
|
+
or
|
84
|
+
|
85
|
+
onfleet.timeout = 5
|
86
|
+
|
87
|
+
### Error handling
|
88
|
+
|
89
|
+
By default Onfleet will attempt to raise errors returned by the API automatically.
|
90
|
+
|
91
|
+
If you set the `throws_exceptions` boolean attribute to false, for a given instance,
|
92
|
+
then Onfleet will not raise exceptions. This allows you to handle errors manually. The
|
93
|
+
APIs will return a Hash with two keys "message", a hash containing two keys 'message' and 'error'.
|
94
|
+
'message' contains textual information about the error, 'error' the numeric code of the error,
|
95
|
+
and "code", the name code of the error.
|
96
|
+
|
97
|
+
If you rescue Onfleet::OnfleetError, you are provided with the error message itself as well as
|
98
|
+
a `code` attribute that you can map onto the API's error list. The API docs list possible errors
|
99
|
+
at the bottom of each page. Here's how you might do that:
|
100
|
+
|
101
|
+
begin
|
102
|
+
onfleet.tasks.all
|
103
|
+
rescue Onfleet::OnfleetError => e
|
104
|
+
# do something with e.message here
|
105
|
+
# do something wiht e.code here
|
106
|
+
end
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
1. Fork it ( https://github.com/[my-github-username]/onfleet/fork )
|
111
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
112
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
113
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
114
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/onfleet.rb
ADDED
data/lib/onfleet/api.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Onfleet
|
2
|
+
class API
|
3
|
+
|
4
|
+
API_RESOURCES = %w{organization admins workers teams destinations recipients tasks webhooks}
|
5
|
+
attr_accessor :api_key, :api_endpoint, :timeout, :throws_exceptions
|
6
|
+
|
7
|
+
def initialize(api_key=nil, default_parameters={})
|
8
|
+
@api_key = api_key || self.class.api_key || ENV['ONFLEET_API_KEY']
|
9
|
+
@api_key = @api_key.strip if @api_key
|
10
|
+
|
11
|
+
@api_endpoint = default_parameters.delete(:api_endpoint) || self.class.api_endpoint
|
12
|
+
@timeout = default_parameters.delete(:timeout) || self.class.timeout
|
13
|
+
@throws_exceptions = default_parameters.has_key?(:throws_exceptions) ? default_parameters.delete(:throws_exceptions) : self.class.throws_exceptions
|
14
|
+
|
15
|
+
@default_params = default_parameters
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(method, *args)
|
19
|
+
if API_RESOURCES.include?(method.to_s)
|
20
|
+
api = APICategory.new(method.to_s, @api_key, @timeout, @throws_exceptions, @api_endpoint, @default_params)
|
21
|
+
api.api_endpoint = @api_endpoint if @api_endpoint
|
22
|
+
api
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class << self
|
29
|
+
attr_accessor :api_key, :timeout, :throws_exceptions, :api_endpoint, :retry_if_fails, :max_try_count, :logger
|
30
|
+
|
31
|
+
def logger
|
32
|
+
@logger || Onfleet::APILogger.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_missing(sym, *args, &block)
|
36
|
+
if API_RESOURCES.include?(sym.to_s)
|
37
|
+
new(self.api_key, {:api_endpoint => self.api_endpoint, :timeout => self.timeout, :throws_exceptions => self.throws_exceptions}).send(sym, *args, &block)
|
38
|
+
else
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'onfleet/api'
|
2
|
+
|
3
|
+
module Onfleet
|
4
|
+
class APICategory
|
5
|
+
include HTTParty
|
6
|
+
format :plain
|
7
|
+
default_timeout 30
|
8
|
+
|
9
|
+
attr_accessor :category_name, :api_key, :api_endpoint, :timeout, :throws_exceptions, :default_params
|
10
|
+
|
11
|
+
def initialize(category_name, api_key, timeout, throws_exceptions, api_endpoint, default_params)
|
12
|
+
@category_name = category_name
|
13
|
+
@api_key = api_key
|
14
|
+
@api_endpoint = api_endpoint || get_api_endpoint
|
15
|
+
@default_params = default_params
|
16
|
+
@throws_exceptions = throws_exceptions
|
17
|
+
@timeout = timeout
|
18
|
+
@try_count = 0
|
19
|
+
|
20
|
+
set_instance_defaults
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(method, params = {})
|
24
|
+
ensure_api_key
|
25
|
+
set_api_url
|
26
|
+
params = @default_params.merge(params)
|
27
|
+
if (response = send_api_request(method, params))
|
28
|
+
parsed_response = response.parsed_response
|
29
|
+
raise_error_with_message(parsed_response) if should_raise_for_response?(response.code)
|
30
|
+
parsed_response
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def send_api_request(method, params)
|
35
|
+
begin
|
36
|
+
Onfleet::API.logger.info "Try Count: #{ @try_count } Start Onfleet API Request #{ @api_url }"
|
37
|
+
response = HTTParty.send(method, @api_url, :body => MultiJson.dump(params), basic_auth: { username: @api_key }, :timeout => @timeout)
|
38
|
+
Onfleet::API.logger.info "Try Count: #{ @try_count } End Onfleet API Request Response Code: #{response.code}"
|
39
|
+
raise OnfleetError, 'Retrying TooManyRequestsError Response Code: 429' if (response.code == 429 && should_retry_if_fails?)
|
40
|
+
return response
|
41
|
+
rescue StandardError => e
|
42
|
+
Onfleet::API.logger.error "ERROR:#{e} #{e.message}"
|
43
|
+
if should_retry_if_fails?
|
44
|
+
@try_count += 1
|
45
|
+
call(method, params)
|
46
|
+
else
|
47
|
+
raise e
|
48
|
+
end
|
49
|
+
end
|
50
|
+
return nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def set_api_url
|
54
|
+
@api_url = @api_endpoint + @category_name
|
55
|
+
end
|
56
|
+
|
57
|
+
def raise_error_with_message(parsed_response={})
|
58
|
+
parsed_response['message'] ||= {}
|
59
|
+
error = OnfleetError.new(parsed_response['message']['message'])
|
60
|
+
error.code = parsed_response['message']['error']
|
61
|
+
error.name = parsed_response['code']
|
62
|
+
Onfleet::API.logger.error "ERROR:#{error.name} #{error.message} ERROR CODE: #{error.code}"
|
63
|
+
raise error
|
64
|
+
end
|
65
|
+
|
66
|
+
def find(id)
|
67
|
+
self.category_name = "#{category_name}/#{id}"
|
68
|
+
call(:get)
|
69
|
+
end
|
70
|
+
|
71
|
+
def all
|
72
|
+
call(:get)
|
73
|
+
end
|
74
|
+
|
75
|
+
def create(args={})
|
76
|
+
call(:post, args)
|
77
|
+
end
|
78
|
+
|
79
|
+
def delete(id)
|
80
|
+
self.category_name = "#{category_name}/#{id}"
|
81
|
+
call(:delete)
|
82
|
+
end
|
83
|
+
|
84
|
+
def update(args={})
|
85
|
+
if id = args.delete(:id)
|
86
|
+
self.category_name = "#{category_name}/#{id}"
|
87
|
+
call(:put, args)
|
88
|
+
else
|
89
|
+
Onfleet::API.logger.error "ERROR: Please give id of #{@category_name} for update"
|
90
|
+
raise OnfleetError, "Please give id of #{@category_name} for update"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def set_instance_defaults
|
95
|
+
@timeout = (API.timeout || 30) if @timeout.nil?
|
96
|
+
@throws_exceptions = API.throws_exceptions if @throws_exceptions.nil?
|
97
|
+
@throws_exceptions = true if @throws_exceptions.nil?
|
98
|
+
end
|
99
|
+
|
100
|
+
def api_key=(value)
|
101
|
+
@api_key = value.strip if value
|
102
|
+
end
|
103
|
+
|
104
|
+
def should_raise_for_response?(response_code)
|
105
|
+
@throws_exceptions && (400..500).include?(response_code)
|
106
|
+
end
|
107
|
+
|
108
|
+
def should_retry_if_fails?
|
109
|
+
Onfleet::API.retry_if_fails && (@try_count < (Onfleet::API.max_try_count || 5))
|
110
|
+
end
|
111
|
+
|
112
|
+
def get_api_endpoint
|
113
|
+
"https://onfleet.com/api/v2/"
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
def ensure_api_key
|
121
|
+
unless @api_key
|
122
|
+
Onfleet::API.logger.error "ERROR: You must set an api_key prior to making a call"
|
123
|
+
raise Onfleet::OnfleetError, "You must set an api_key prior to making a call"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Onfleet
|
2
|
+
class APILogger
|
3
|
+
def info(msg)
|
4
|
+
$stdout.puts msg
|
5
|
+
end
|
6
|
+
def warn(msg)
|
7
|
+
$stdout.puts msg
|
8
|
+
end
|
9
|
+
def error(msg)
|
10
|
+
$stdout.puts msg
|
11
|
+
end
|
12
|
+
def debug(msg)
|
13
|
+
$stdout.puts msg
|
14
|
+
end
|
15
|
+
def fatal(msg)
|
16
|
+
$stdout.puts msg
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/onfleet.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'onfleet/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "onfleet"
|
8
|
+
spec.version = Onfleet::VERSION
|
9
|
+
spec.authors = ["Siddharth Sharma"]
|
10
|
+
spec.email = ["siddharth@vinsol.com"]
|
11
|
+
spec.summary = %q{Write a short summary. Required.}
|
12
|
+
spec.description = %q{Write a longer description. Optional.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "httparty"
|
22
|
+
spec.add_dependency "multi_json"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: onfleet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Siddharth Sharma
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-06-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: multi_json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.7'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.7'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '10.0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '10.0'
|
78
|
+
description: Write a longer description. Optional.
|
79
|
+
email:
|
80
|
+
- siddharth@vinsol.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/onfleet.rb
|
91
|
+
- lib/onfleet/api.rb
|
92
|
+
- lib/onfleet/api_category.rb
|
93
|
+
- lib/onfleet/api_logger.rb
|
94
|
+
- lib/onfleet/onfleet_error.rb
|
95
|
+
- lib/onfleet/version.rb
|
96
|
+
- onfleet.gemspec
|
97
|
+
homepage: ''
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.23
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Write a short summary. Required.
|
122
|
+
test_files: []
|