foxtrot-client 0.0.4

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 97cd0d96398916ec13f13e9d34c2fe9e1034ed37
4
+ data.tar.gz: d940719a600210e030be9b6d3cf28e38185bb311
5
+ SHA512:
6
+ metadata.gz: 752d0d4226b4b24665dadcd3e4b355c506b45e790f812d0cbebd12357abd54eeb76d7f633248be5ee47c2d001a2400605df1c1bfdc71407c551eb912e4b0a26a
7
+ data.tar.gz: 7ca3c3bdbeabc4151da87eaf5d7fdb3be46766a3324999073e483ef34ecd649658458939f67502daf3573da17ddd8713887e0b9c6e97e57b77a1007c5b6b4f9c
@@ -0,0 +1,41 @@
1
+ # Created by https://www.gitignore.io
2
+
3
+ ### Ruby ###
4
+ *.gem
5
+ *.rbc
6
+ /.config
7
+ /coverage/
8
+ /InstalledFiles
9
+ /pkg/
10
+ /spec/reports/
11
+ /test/tmp/
12
+ /test/version_tmp/
13
+ /tmp/
14
+
15
+ ## Specific to RubyMotion:
16
+ .dat*
17
+ .repl_history
18
+ build/
19
+
20
+ ## Documentation cache and generated files:
21
+ /.yardoc/
22
+ /_yardoc/
23
+ /doc/
24
+ /rdoc/
25
+
26
+ ## Environment normalisation:
27
+ /.bundle/
28
+ /lib/bundler/man/
29
+
30
+ # for a library or gem, you might want to ignore these files since the code is
31
+ # intended to run in multiple environments; otherwise, check them in:
32
+ # Gemfile.lock
33
+ # .ruby-version
34
+ # .ruby-gemset
35
+
36
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
37
+ .rvmrc
38
+
39
+ *.pid
40
+
41
+ gems/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ foxtrot-client (0.0.1)
5
+ addressable (~> 2.3.6)
6
+ rest_client (~> 1.7.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.3.6)
12
+ coderay (1.1.0)
13
+ method_source (0.8.2)
14
+ netrc (0.7.9)
15
+ pry (0.10.1)
16
+ coderay (~> 1.1.0)
17
+ method_source (~> 0.8.1)
18
+ slop (~> 3.4)
19
+ rake (10.4.2)
20
+ rest_client (1.7.3)
21
+ netrc (~> 0.7.7)
22
+ slop (3.6.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.6)
29
+ foxtrot-client!
30
+ pry
31
+ rake
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Yasyf Mohamedali, Foxtrot Systems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Foxtrot Ruby Client Library
2
+
3
+ This is the Ruby client library for interacting with the Foxtrot API. The only endpoint currently exposed is the route optimization endpoint (`Foxtrot::Client.optimize!`).
4
+
5
+ In order to make requests, you need a valid API key. Your API key can be found at the bottom of any page in the [Foxtrot web app](http://app.foxtrot.io/).
6
+
7
+ ## Installation
8
+
9
+ `gem install foxtrot-client`
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ data = {
15
+ file_url: "https://www.domain.io/your_file.xlsx",
16
+ file_name: "your_file.xlsx",
17
+ geocode: "false",
18
+ stop_name: "Customer",
19
+ lat: "Lat",
20
+ lng: "Long",
21
+ load: "Load",
22
+ service_time: "Service Time",
23
+ time_window: "Time Window",
24
+ extra_info: "Contact Info",
25
+ date_starting: "1407712069593",
26
+ warehouse: "77 Massachusetts Ave, Cambridge MA",
27
+ num_drivers: 1,
28
+ num_avg_service_time: 10,
29
+ float_fuel_cost: 3.56,
30
+ float_driver_wage: 6.01,
31
+ float_mpg: 8.32
32
+ }
33
+
34
+ api_key = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
35
+
36
+ require 'foxtrot'
37
+ fox = Foxtrot::Client.new api_key
38
+
39
+ resp = fox.optimize!(data).poll_and_block!
40
+ result = resp.get_result
41
+ ```
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'foxtrot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "foxtrot-client"
8
+ spec.version = Foxtrot::VERSION
9
+ spec.authors = ["Yasyf Mohamedali"]
10
+ spec.email = ["yasyf@foxtrot.io"]
11
+ spec.summary = "Foxtrot Ruby Client Library"
12
+ spec.homepage = "https://github.com/FoxtrotSystems/api-client-ruby"
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = ">= 2.0"
15
+
16
+ spec.description = File.read('README.md')
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_runtime_dependency 'rest_client', '~> 1.7', '>= 1.7.2'
27
+ spec.add_runtime_dependency 'addressable', '~> 2.3', '>= 2.3.6'
28
+ end
@@ -0,0 +1 @@
1
+ require "foxtrot/client"
@@ -0,0 +1,66 @@
1
+ require 'addressable/uri'
2
+ require 'json'
3
+ require 'rest_client'
4
+ require 'foxtrot/required_fields'
5
+ require 'foxtrot/response'
6
+ require 'foxtrot/errors'
7
+
8
+ module Foxtrot
9
+ class Client
10
+
11
+ class << self
12
+ attr_accessor :host, :ssl
13
+ end
14
+
15
+ @host = 'app.foxtrot.io'
16
+ @ssl = true
17
+
18
+ def initialize(api_key, version='v1')
19
+ @api_key = api_key
20
+ @version = version
21
+ @headers = {:content_type => :json, :accept => :json}
22
+ end
23
+
24
+ def poll!(endpoint, txid, args={})
25
+ resp = get_request(endpoint, {txid: txid})
26
+ Foxtrot::Response::Response.response_for(endpoint, resp, self)
27
+ end
28
+
29
+ def optimize!(data, args={})
30
+ check_required 'optimize', data
31
+ resp = post_request 'optimize', data, args
32
+ Foxtrot::Response::Response.response_for('optimize', resp, self)
33
+ end
34
+
35
+ private
36
+
37
+ def check_required(endpoint, data)
38
+ required = Foxtrot::RequiredFields.const_get(endpoint.upcase)
39
+ data = data.inject({}){ |h, (k,v)| h.merge({k.to_sym => v}) }
40
+ required.each do |field|
41
+ unless data.include?(field)
42
+ raise Foxtrot::Errors::ParameterError.new(field)
43
+ end
44
+ end
45
+ end
46
+
47
+ def build_url(endpoint, args)
48
+ uri = Addressable::URI.new
49
+ uri.host = self.class.host
50
+ uri.scheme = self.class.ssl ? 'https' : 'http'
51
+ uri.query_values = args.merge({key: @api_key})
52
+ uri.path = "/api/#{@version}/#{endpoint}"
53
+ uri.to_s
54
+ end
55
+
56
+ def get_request(endpoint, args)
57
+ resp = RestClient.get build_url(endpoint, args), @headers
58
+ JSON.load resp
59
+ end
60
+
61
+ def post_request(endpoint, data, args)
62
+ resp = RestClient.post build_url(endpoint, args), data.to_json, @headers
63
+ JSON.load resp
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,24 @@
1
+ module Foxtrot
2
+ module Errors
3
+ class FoxtrotError < StandardError
4
+ end
5
+
6
+ class ParameterError < FoxtrotError
7
+ def initialize(param)
8
+ @param = param
9
+ super "Missing Parameter: #{param}"
10
+ end
11
+ end
12
+
13
+ class APIResponseError < FoxtrotError
14
+ end
15
+
16
+ class APITimeoutError < FoxtrotError
17
+ def initialize(count)
18
+ @count = count
19
+ super "Request timed out after #{count} seconds."
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module Foxtrot
2
+ module RequiredFields
3
+ OPTIMIZE = [:file_url, :file_name, :geocode, :stop_name, :date_starting,
4
+ :warehouse, :num_drivers, :num_avg_service_time, :float_fuel_cost,
5
+ :float_driver_wage, :float_mpg]
6
+ end
7
+ end
@@ -0,0 +1,110 @@
1
+ require 'foxtrot/errors'
2
+
3
+ module Foxtrot
4
+ module Response
5
+ class Response
6
+ def initialize(endpoint, resp, api_instance)
7
+ @endpoint = endpoint
8
+ @resp = resp
9
+ @api_instance = api_instance
10
+ end
11
+
12
+ def poll_and_block!(interval=1, timeout=60)
13
+ count = 0
14
+ instance = self
15
+ while !instance.complete?
16
+ instance = instance.poll!
17
+ sleep interval
18
+ count += interval
19
+ if count > timeout
20
+ raise Foxtrot::Errors::APITimeoutError.new(count)
21
+ end
22
+ end
23
+ instance
24
+ end
25
+
26
+ def self.response_for(endpoint, resp, api_instance)
27
+ case resp['status']
28
+ when 'pending'
29
+ PendingResponse.new(endpoint, resp, api_instance)
30
+ when 'success'
31
+ SuccessResponse.new(endpoint, resp, api_instance)
32
+ else
33
+ ErrorResponse.new(endpoint, resp, api_instance)
34
+ end
35
+ end
36
+ end
37
+
38
+ class ErrorResponse < Response
39
+ def initialize(endpoint, resp, api_instance)
40
+ super
41
+ @message = resp['message']
42
+ @exception = Foxtrot::Errors::APIResponseError.new @message
43
+ raise @exception
44
+ end
45
+
46
+ def complete?
47
+ true
48
+ end
49
+
50
+ def poll!
51
+ raise @exception
52
+ end
53
+
54
+ def get_result
55
+ nil
56
+ end
57
+
58
+ def to_s
59
+ "ErrorResponse (#{@message})"
60
+ end
61
+ end
62
+
63
+ class PendingResponse < Response
64
+ def initialize(endpoint, resp, api_instance)
65
+ super
66
+ @txid = resp['txid']
67
+ end
68
+
69
+ def complete?
70
+ false
71
+ end
72
+
73
+ def poll!
74
+ @api_instance.poll! @endpoint, @txid
75
+ end
76
+
77
+ def get_result
78
+ nil
79
+ end
80
+
81
+ def to_s
82
+ "PendingResponse (#{@txid})"
83
+ end
84
+ end
85
+
86
+ class SuccessResponse < Response
87
+ def initialize(endpoint, resp, api_instance)
88
+ super
89
+ @txid = resp['txid']
90
+ @data = resp['response']['api_data']
91
+ end
92
+
93
+ def complete?
94
+ true
95
+ end
96
+
97
+ def poll!
98
+ self
99
+ end
100
+
101
+ def get_result
102
+ @data
103
+ end
104
+
105
+ def to_s
106
+ "SuccessResponse (#{@txid})"
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,3 @@
1
+ module Foxtrot
2
+ VERSION = "0.0.4"
3
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foxtrot-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Yasyf Mohamedali
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest_client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.7.2
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.7'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.7.2
75
+ - !ruby/object:Gem::Dependency
76
+ name: addressable
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.3'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.3.6
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '2.3'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.3.6
95
+ description: |
96
+ # Foxtrot Ruby Client Library
97
+
98
+ This is the Ruby client library for interacting with the Foxtrot API. The only endpoint currently exposed is the route optimization endpoint (`Foxtrot::Client.optimize!`).
99
+
100
+ In order to make requests, you need a valid API key. Your API key can be found at the bottom of any page in the [Foxtrot web app](http://app.foxtrot.io/).
101
+
102
+ ## Installation
103
+
104
+ `gem install foxtrot-client`
105
+
106
+ ## Usage
107
+
108
+ ```ruby
109
+ data = {
110
+ file_url: "https://www.domain.io/your_file.xlsx",
111
+ file_name: "your_file.xlsx",
112
+ geocode: "false",
113
+ stop_name: "Customer",
114
+ lat: "Lat",
115
+ lng: "Long",
116
+ load: "Load",
117
+ service_time: "Service Time",
118
+ time_window: "Time Window",
119
+ extra_info: "Contact Info",
120
+ date_starting: "1407712069593",
121
+ warehouse: "77 Massachusetts Ave, Cambridge MA",
122
+ num_drivers: 1,
123
+ num_avg_service_time: 10,
124
+ float_fuel_cost: 3.56,
125
+ float_driver_wage: 6.01,
126
+ float_mpg: 8.32
127
+ }
128
+
129
+ api_key = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
130
+
131
+ require 'foxtrot'
132
+ fox = Foxtrot::Client.new api_key
133
+
134
+ resp = fox.optimize!(data).poll_and_block!
135
+ result = resp.get_result
136
+ ```
137
+ email:
138
+ - yasyf@foxtrot.io
139
+ executables: []
140
+ extensions: []
141
+ extra_rdoc_files: []
142
+ files:
143
+ - ".gitignore"
144
+ - Gemfile
145
+ - Gemfile.lock
146
+ - LICENSE.txt
147
+ - README.md
148
+ - Rakefile
149
+ - foxtrot-client.gemspec
150
+ - lib/foxtrot.rb
151
+ - lib/foxtrot/client.rb
152
+ - lib/foxtrot/errors.rb
153
+ - lib/foxtrot/required_fields.rb
154
+ - lib/foxtrot/response.rb
155
+ - lib/foxtrot/version.rb
156
+ homepage: https://github.com/FoxtrotSystems/api-client-ruby
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '2.0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.2.2
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Foxtrot Ruby Client Library
180
+ test_files: []