aceroute 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.8.7.6
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ hasFrames = window.top.frames.main ? true : false;
19
+ relpath = '';
20
+ framesUrl = "frames.html#!top-level-namespace.html";
21
+ </script>
22
+
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
25
+
26
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
27
+
28
+
29
+ </head>
30
+ <body>
31
+ <div id="header">
32
+ <div id="menu">
33
+
34
+ <a href="_index.html">Index</a> &raquo;
35
+
36
+
37
+ <span class="title">Top Level Namespace</span>
38
+
39
+
40
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
41
+ </div>
42
+
43
+ <div id="search">
44
+
45
+ <a class="full_list_link" id="class_list_link"
46
+ href="class_list.html">
47
+ Class List
48
+ </a>
49
+
50
+ <a class="full_list_link" id="method_list_link"
51
+ href="method_list.html">
52
+ Method List
53
+ </a>
54
+
55
+ <a class="full_list_link" id="file_list_link"
56
+ href="file_list.html">
57
+ File List
58
+ </a>
59
+
60
+ </div>
61
+ <div class="clear"></div>
62
+ </div>
63
+
64
+ <iframe id="search_frame"></iframe>
65
+
66
+ <div id="content"><h1>Top Level Namespace
67
+
68
+
69
+
70
+ </h1>
71
+
72
+ <dl class="box">
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+ </dl>
82
+ <div class="clear"></div>
83
+
84
+ <h2>Defined Under Namespace</h2>
85
+ <p class="children">
86
+
87
+
88
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Aceroute.html" title="Aceroute (module)">Aceroute</a></span>
89
+
90
+
91
+
92
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="Hashit.html" title="Hashit (class)">Hashit</a></span>
93
+
94
+
95
+ </p>
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+ </div>
106
+
107
+ <div id="footer">
108
+ Generated on Sun Dec 4 11:17:52 2016 by
109
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
110
+ 0.8.7.6 (ruby-2.3.1).
111
+ </div>
112
+
113
+ </body>
114
+ </html>
@@ -0,0 +1,20 @@
1
+ module Aceroute
2
+
3
+
4
+ # Base class for all other Aceroute module classes to extend.
5
+ class Base
6
+
7
+
8
+ protected
9
+ #takes a Hashit class, extracts the instance variables, and sets them on our instance
10
+ # @param hashit [Hashit] Hashit object, typically created from a response from Aceroute API
11
+ def update_attrs(hashit)
12
+ hashit.instance_variables.each do |name|
13
+ singleton_class.class_eval {attr_accessor "#{name[1..-1]}"} #remove leading @ from varname
14
+ send("#{name[1..-1]}=", hashit.instance_variable_get(name))
15
+ end
16
+ end
17
+
18
+
19
+ end
20
+ end
@@ -0,0 +1,192 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'aceroute/base'
4
+ require 'aceroute/hashit'
5
+ require 'aceroute/customer'
6
+ require 'aceroute/location'
7
+ require 'aceroute/order'
8
+
9
+ module Aceroute
10
+ include HTTParty
11
+ @@DEBUG = false
12
+ debug_output $stdout if @@DEBUG
13
+ attr_accessor :http_result
14
+
15
+
16
+ base_uri 'http://air.aceroute.com'
17
+ @@API_KEY = ENV['ACEROUTE_API_TOKEN']
18
+
19
+
20
+ @@query_params = {
21
+ token: @@API_KEY,
22
+ updsince: '0'
23
+ }
24
+
25
+
26
+ # List all customers
27
+ # @return [Hash] list of customer objects
28
+ def self.list_customers
29
+ customers = []
30
+ res = self.call_api("customer.list", nil)
31
+ res.cnts.cnt.each do |r|
32
+ c = Aceroute::Customer.new(name: r['nm'], email: r['eml'], cid: r['cid'])
33
+ customers << c
34
+ #find corresponding addresses for this customer
35
+ locations = res.locs.loc.find_all{|l| l["cid"] == c.cid }
36
+ locations.each do |a|
37
+ c.locations << Aceroute::Location.new(address1: a['adr'], address2: a['adr2'],
38
+ phone: a['tel'], description: a['nm'])
39
+ end
40
+ end
41
+ customers
42
+ end
43
+
44
+
45
+ # Create a new customer
46
+ # @param customer [Hash]
47
+ # * :name (String) the name of customer
48
+ # * :email (String) the email for this customer
49
+ # * :address [Hash]
50
+ # * :description (String) the description of this address, eg 'home'
51
+ # * :address1 (String) line 1 of the address, eg '123 Fake Street'
52
+ # * :address2 (String) line 2 of the address, eg 'New York, NY 12345'
53
+ # * :name (String) address name
54
+ # * :phone (String) address phone number
55
+ # @return [Hash] customer and address
56
+ def self.create_customer(customer)
57
+ recs = "<data>
58
+ <cst>
59
+ <nm>#{customer[:name]}</nm>
60
+ <locnm>#{customer[:address][:description]}</locnm>
61
+ <adr>#{customer[:address][:address1]}</adr>
62
+ <adr2>#{customer[:address][:address2]}</adr2>
63
+ <cntnm>#{customer[:address][:name]}</cntnm>
64
+ <tel>#{customer[:address][:phone]}</tel>
65
+ <eml>#{customer[:email]}</eml>
66
+ </cst>
67
+ </data>"
68
+
69
+ data = self.call_api("customer.create", recs)
70
+ location = data.locs.loc
71
+ customer = data.cnts.cnt
72
+ return customer, location
73
+ end
74
+
75
+
76
+ # Delete a customer
77
+ # @param customer_id [Integer] id of Aceroute Customer object
78
+ # @return success or failure hash
79
+ def self.delete_customer(customer_id)
80
+ recs = "<data><del><id>#{customer_id}</id></del></data>"
81
+ self.call_api("customer.delete", recs)
82
+ end
83
+
84
+
85
+ # Create a new location
86
+ # @param location [Hash]
87
+ # * :id (Integer)
88
+ # * :description (String) the description of this address, eg 'home'
89
+ # * :address1 (String) line 1 of the address, eg '123 Fake Street'
90
+ # * :address2 (String) line 2 of the address, eg 'New York, NY 12345'
91
+ # * :customer
92
+ # * :cid (Integer) cid from Aceroute Customer object
93
+ # @return Aceroute location object
94
+ def self.create_location(location)
95
+ recs = "<data><loc><id>#{location[:id]}</id>
96
+ <cid>#{location[:customer][:cid]}</cid>
97
+ <nm>#{location[:description]}</nm>
98
+ <adr>#{location[:address1]}</adr>
99
+ <adr2>#{location[:address2]}</adr2>
100
+ </loc></data>"
101
+ data = self.call_api("customer.location.save", recs)
102
+ loc = data.loc
103
+ end
104
+
105
+
106
+ # Delete a location
107
+ # @param location_id (Integer) id from Aceroute Location object
108
+ # @return nil
109
+ def self.delete_location(location_id)
110
+ recs = "<data><del><id>#{location_id}</id></del></data>"
111
+ types = self.call_api("customer.location.delete", recs).otype
112
+ end
113
+
114
+ # List order types
115
+ # @return list of available order types for this account
116
+ def self.list_order_types
117
+ self.call_api("order.type.list", nil)
118
+ end
119
+
120
+ # List service types
121
+ # @return list of available service types for this account
122
+ def self.list_service_types
123
+ self.call_api("product.type.list", nil)
124
+ end
125
+
126
+
127
+ # List all workers
128
+ # @return list of available workers for this account
129
+ def self.list_workers
130
+ workers = self.call_api("worker.list", nil).res
131
+ end
132
+
133
+ # List all orders
134
+ # @return list of all orders in this account
135
+ def self.list_orders
136
+ workers = self.call_api("order.list", nil).event
137
+ end
138
+
139
+
140
+
141
+
142
+ # Create new order
143
+ # @param order [Hash]
144
+ # * :cid (Integer) Aceroute customer id
145
+ # * :nm (String) 'name', descriptive field for this order
146
+ # * :dir (Integer) 'duration', in 5 minute increments
147
+ # * :sched (Integer) 1 = scheduled, 0 = unscheduled
148
+ # * :start_epoch (Integer) time in msec since epoch
149
+ # * :lid (Integer) optional -- customer location id
150
+ # * :cntid (Integer) optional -- customer contact id
151
+ # * :rid (Integer) optional -- worker id, to assign this order to a specific worker
152
+ # * :dtl (String) optional -- order summary
153
+ # * po (String) optional -- 'purchase order', descriptive field for use as desired
154
+ # @return Aceroute Order object
155
+ def self.create_order(order)
156
+ recs = "<data>
157
+ <event>
158
+ <cid>#{order[:cid]}</cid>
159
+ <nm>#{order[:nm]}</nm>
160
+ <dur>#{order[:dur]}</dur>
161
+ <schd>#{order[:schd]}</schd>
162
+ <start_epoch>#{order[:start_epoch]}</start_epoch>
163
+ <lid>#{order[:lid]}</lid>
164
+ <cntid>#{order[:cntid]}</cntid>
165
+ <rid>#{order[:rid]}</rid>
166
+ <dtl>#{order[:dtl]}</dtl>
167
+ <po>#{order[:po]}</po>
168
+ </event>
169
+ </data>"
170
+ puts recs if @@DEBUG
171
+ data = self.call_api("order.create", recs)
172
+ puts data if @@DEBUG
173
+ order = data.event
174
+ end
175
+
176
+ # Delete an order
177
+ def self.delete_order(order_id)
178
+ recs = "<data><del><id>#{order_id}</id></del></data>"
179
+ self.call_api("order.delete", recs)
180
+ end
181
+
182
+ private
183
+ def self.call_api(method, recs)
184
+ params = @@query_params.merge!({method: method})
185
+ params[:recs] = recs unless recs.nil?
186
+ options = {query: params}
187
+ http_result = self.get("/api", options).parsed_response
188
+ puts http_result if @@DEBUG
189
+ data = Hashit.new(http_result['data'])
190
+ end
191
+
192
+ end
@@ -0,0 +1,69 @@
1
+ module Aceroute
2
+ class Customer < Base
3
+ attr_accessor :locations
4
+ attr_accessor :email
5
+ attr_accessor :name
6
+ attr_accessor :cid, :id
7
+
8
+ #Creates a new Aceroute::Customer object. Note this does not
9
+ #persist the Customer to Aceroute, that can be done by calling the
10
+ #create! method on the new object.
11
+ # @param name [String] customer name
12
+ # @param email [String] customer email
13
+ # @param location [Hash] customer Location, optional
14
+ # @param cid [Integer] Aceroute customer id, optional; useful for instantiating Customer objects from Aceroute API response
15
+ # @return [Aceroute::Customer]
16
+ def initialize(name, email, location = {}, cid = nil)
17
+ self.locations = []
18
+ #create getters/setters for each param
19
+ self.email = email
20
+ self.name = name
21
+ self.cid = cid
22
+
23
+ if !location.empty?
24
+ locations << Aceroute::Location.new(location[:address1], location[:address2], location[:description],
25
+ location[:name], location[:phone])
26
+ end
27
+ end
28
+
29
+
30
+ # Persists Customer object to Aceroute API.
31
+ # @return [Aceroute::Customer]
32
+ def create!
33
+ recs = "<data>
34
+ <cst>
35
+ <nm>#{self.name}</nm>
36
+ <locnm>#{self.locations.first.description}</locnm>
37
+ <adr>#{self.locations.first.address1}</adr>
38
+ <adr2>#{self.locations.first.address2}</adr2>
39
+ <cntnm>#{self.locations.first.name}</cntnm>
40
+ <tel>#{self.locations.first.phone}</tel>
41
+ <eml>#{self.email}</eml>
42
+ </cst>
43
+ </data>"
44
+
45
+ #puts recs
46
+ data = Aceroute::call_api("customer.create", recs)
47
+ location = data.locs.loc
48
+ customer = data.cnts.cnt
49
+ update_attrs(customer)
50
+ self.cid = customer.cid
51
+ locations.first.update_attrs(location)
52
+ return self
53
+ end
54
+
55
+ # Deletes this Aceroute::Customer object (self) from Aceroute;
56
+ def destroy!(id = nil)
57
+ Customer.delete(id ? id : self.cid)
58
+ end
59
+
60
+ # Deletes Aceroute::Customer of given id from Aceroute
61
+ # @param id [Integer]
62
+ def self.delete(id)
63
+ recs = "<data><del><id>#{id}</id></del></data>"
64
+ ret = Aceroute::call_api("customer.delete", recs)
65
+ ret.success == "true" ? true : false
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,12 @@
1
+ class Hashit
2
+ def initialize(hash)
3
+ hash.nil? ? return : nil
4
+ hash.each do |k,v|
5
+ self.instance_variable_set("@#{k}", v.is_a?(Hash) ? Hashit.new(v) : v)
6
+ self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
7
+ self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
8
+ end
9
+ end
10
+ end
11
+
12
+
@@ -0,0 +1,62 @@
1
+ module Aceroute
2
+ class Location < Base
3
+ attr_accessor :address1
4
+ attr_accessor :address2
5
+ attr_accessor :description
6
+ attr_accessor :name
7
+ attr_accessor :phone
8
+ attr_accessor :cid, :id
9
+
10
+
11
+ #Creates a new Aceroute::Location object. Note this does not
12
+ #persist the Location to Aceroute, that can be done by calling the
13
+ #create! method on the new object.
14
+ # @param address1 [String] customer address line 1 (street)
15
+ # @param address2 [String] customer address line 2 (eg apartment number)
16
+ # @param description [String] description of address (eg 'Home')
17
+ # @param name [String] name of address (eg 'Home')
18
+ # @param phone [String] phone number associated with this address
19
+ # @param cid [Integer] Aceroute customer id, optional; associates this Location with that Customer
20
+ # @param id [Integer] Aceroute location id, optional; useful for instantiating Location objects from Aceroute API response
21
+ # @return [Aceroute::Location]
22
+ def initialize(address1, address2, description, name, phone, cid = nil, id= nil)
23
+ self.address1 = address1
24
+ self.address2 = address2
25
+ self.description = description
26
+ self.name = name
27
+ self.phone = phone
28
+ self.cid = cid
29
+ self.id = id
30
+ end
31
+
32
+
33
+ # Persists Aceroute::Location object to Aceroute API.
34
+ # @return [Aceroute::Location]
35
+ def create!
36
+ recs = "<data><loc><id>0</id>
37
+ <cid>#{self.cid}</cid>
38
+ <nm>#{self.description}</nm>
39
+ <adr>#{self.address1}</adr>
40
+ <adr2>#{self.address2}</adr2>
41
+ </loc></data>"
42
+ data = Aceroute::call_api("customer.location.save", recs)
43
+ loc = data.loc
44
+ update_attrs(loc)
45
+ return self
46
+ end
47
+
48
+ # Deletes this Aceroute::Location object (self) from Aceroute
49
+ def destroy!(id = nil)
50
+ Location.delete(id ? id : self.id)
51
+ end
52
+
53
+ # Deletes Aceroute::Location of given id from Aceroute
54
+ # @param id [Integer]
55
+ def self.delete(id)
56
+ req = "<data><del><id>#{id}</id></del></data>"
57
+ ret = Aceroute::call_api("customer.location.delete", req)
58
+ ret.success == "true" ? true : false #maybe raise error here instead
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,81 @@
1
+ module Aceroute
2
+ class Order < Base
3
+ attr_accessor :customer, :location, :start_time #in msec (not sec) since epoch
4
+ attr_accessor :description, :duration, :scheduled, :worker, :summary, :purchase_order #any freeform text here
5
+ attr_accessor :cid, :id
6
+
7
+
8
+
9
+
10
+
11
+ #Creates a new Aceroute::Order object. Note this does not
12
+ #persist the Location to Aceroute, that can be done by calling the
13
+ #create! method on the new object.
14
+ # @param customer [Aceroute::Customer] recipient of this Order
15
+ # @param location [Aceroute::Location] location of this Order (delivery destination)
16
+ # @param start_time [Integer] start time of Order, in msec since epoch (note: msec not sec)
17
+ # @param description [String] description of Order (e.g., contents of order)
18
+ # @param duration [Integer] duration of Order in minutes (time to service customer); used to aid in route optimization. Defaults to 10 minutes
19
+ # @param scheduled [Boolean] whether this Order is scheduled or not; defaults to true
20
+ # @param worker [Integer] Worker ID of aceroute worker to assign this Order to; defaults to nil (not implemented)
21
+ # @param summary [String] Summary of order, to be displated in app. Defaults to none.
22
+ # @param purchase_order [String] arbitrary string to indicate PO or other note, if needed
23
+ # @return [Aceroute::Order]
24
+ def initialize(customer, location, start_time, description = nil, duration = 10,
25
+ scheduled = true, worker = nil, summary = nil, purchase_order = nil)
26
+ self.customer = customer
27
+ self.location = location
28
+ self.start_time = start_time
29
+ #FIXME: taking a DateTime argument is friendlier
30
+ #self.start_time = start_time.to_i * 1000 #DateTime object
31
+ self.description = description
32
+ self.duration = duration
33
+ self.scheduled = scheduled
34
+ self.worker = worker
35
+ self.summary = summary
36
+ self.purchase_order = purchase_order
37
+ end
38
+
39
+
40
+
41
+ # Persists Aceroute::Order object to Aceroute API.
42
+ # @return [Aceroute::Order]
43
+ def create!
44
+ recs = "<data>
45
+ <event>
46
+ <cid>#{self.customer.cid}</cid>
47
+ <nm>#{self.description}</nm>
48
+ <dur>#{self.duration}</dur>
49
+ <schd>#{self.scheduled ? 1 : 0}</schd>
50
+ <start_epoch>#{self.start_time}</start_epoch>
51
+ <lid>#{self.location.id}</lid>
52
+ <cntid>#{0}</cntid>
53
+ <rid>#{self.worker}</rid>
54
+ <dtl>#{self.summary}</dtl>
55
+ <po>#{self.purchase_order}</po>
56
+ </event>
57
+ </data>"
58
+ data = Aceroute::call_api("order.create", recs)
59
+ order = data.event
60
+ update_attrs(order)
61
+ return self
62
+ end
63
+
64
+
65
+ # Deletes this Aceroute::Order object (self) from Aceroute
66
+ def destroy!(id = nil)
67
+ Order.delete(id ? id : self.id)
68
+ end
69
+
70
+
71
+ # Deletes Aceroute::Location of given id from Aceroute
72
+ # @param id [Integer]
73
+ def self.delete(id)
74
+ req = "<data><del><id>#{id}</id></del></data>"
75
+ ret = Aceroute::call_api("order.delete", req)
76
+ ret.success == "true" ? true : false #maybe raise error here instead
77
+ end
78
+
79
+
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module Aceroute
2
+ VERSION = "0.2"
3
+ end
data/lib/aceroute.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "aceroute/version"
2
+ require "aceroute/core"
3
+ module Aceroute
4
+ # Your code goes here...
5
+ end