ordrin 0.1.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/ordrindemo.rb DELETED
@@ -1,323 +0,0 @@
1
- #! /usr/bin/env ruby
2
- require 'date'
3
- require 'securerandom'
4
- require 'pp'
5
-
6
- begin
7
- require 'ordrin'
8
- rescue LoadError
9
- require 'rubygems'
10
- begin
11
- require 'ordrin'
12
- rescue LoadError
13
- require_relative '../lib/ordrin'
14
- end
15
- end
16
-
17
- module OrdrinDemo
18
- #
19
- # Global Variables
20
- #
21
- print "Please input your API key: "
22
- STDOUT.flush
23
- api_key = gets.chomp
24
-
25
- @@api = Ordrin::APIs.new(api_key, :test)
26
-
27
- # Create an Address object
28
- @@address = Ordrin::Data::Address.new('1 Main Street', 'College Station', 'TX', '77840', '(555) 555-5555')
29
- @@address_nick = 'addr1'
30
-
31
- # Create a CreditCard object
32
- @@first_name = 'Test'
33
- @@last_name = 'User'
34
- @@credit_card = Ordrin::Data::CreditCard.new("#{@@first_name} #{@@last_name}", '01', (Date.today.year+2).to_s, @@address, '4111111111111111', '123')
35
- @@credit_card_nick = 'cc1'
36
-
37
- unique_id = SecureRandom.uuid.to_s.gsub(/-/, '')
38
- @@email = "demo+#{unique_id}ruby@ordr.in"
39
- @@password = 'password'
40
- @@login = Ordrin::Data::UserLogin.new(@@email, @@password)
41
- @@alt_first_name = 'Example'
42
- @@alt_email = "demo+#{unique_id}rubyalt@ordr.in"
43
- @@alt_login = Ordrin::Data::UserLogin.new(@@alt_email, @@password)
44
- @@new_password = 'password1'
45
-
46
- #
47
- # Restaurant demo functions
48
- #
49
-
50
- def OrdrinDemo.delivery_list_immediate_demo()
51
- puts "Get a list of restaurants that will deliver if you order now"
52
- print "Press enter to execute and see the response"
53
- gets
54
- delivery_list_immediate = @@api.restaurant.get_delivery_list('ASAP', @@address)
55
- PP.pp(delivery_list_immediate)
56
- return delivery_list_immediate
57
- end
58
-
59
- def OrdrinDemo.delivery_list_future_demo()
60
- puts "Get a list of restaurants that will deliver if you order for 12 hours from now"
61
- print "Press enter to execute and see the response"
62
- gets
63
- future_datetime = DateTime.now + 0.5 #A timestamp twelve hours in the future
64
- delivery_list_later = @@api.restaurant.get_delivery_list(future_datetime, @@address)
65
- PP.pp(delivery_list_later)
66
- end
67
-
68
- def OrdrinDemo.delivery_check_demo(restaurant_id)
69
- puts "Get whether a particular restaurant will deliver if you order now"
70
- print "Press enter to execute and see the response"
71
- gets
72
- delivery_check = @@api.restaurant.get_delivery_check(restaurant_id, 'ASAP', @@address)
73
- PP.pp(delivery_check)
74
- end
75
-
76
- def OrdrinDemo.fee_demo(restaurant_id)
77
- puts "Get fee and other info for ordering a given amount with a given tip"
78
- print "Press enter to execute and see the response"
79
- gets
80
- subtotal = "$30.00"
81
- tip = "$5.00"
82
- fee_info = @@api.restaurant.get_fee(restaurant_id, subtotal, tip, 'ASAP', @@address)
83
- PP.pp(fee_info)
84
- end
85
-
86
- def OrdrinDemo.detail_demo(restaurant_id)
87
- puts "Get detailed information about a single restaurant"
88
- print "Press enter to execute and see the response"
89
- gets
90
- restaurant_detail = @@api.restaurant.get_details(restaurant_id)
91
- PP.pp(restaurant_detail)
92
- return restaurant_detail
93
- end
94
-
95
- def OrdrinDemo.find_deliverable_time(restaurant_id)
96
- puts "Find a time when this restaurant will deliver"
97
- print "Press enter to execute and see the response"
98
- gets
99
- delivery_check = @@api.restaurant.get_delivery_check(restaurant_id, 'ASAP', @@address)
100
- delivery = delivery_check['delivery']
101
- if delivery
102
- return 'ASAP'
103
- end
104
- dt = DateTime.now + 1/24.0
105
- while not delivery
106
- delivery_check = @@api.restaurant.get_delivery_check(restaurant_id, dt, @@address)
107
- delivery = delivery_check['delivery']
108
- dt += 1/24.0
109
- end
110
- return dt
111
- end
112
- #
113
- # User demo functions
114
- #
115
-
116
- def OrdrinDemo.get_user_demo()
117
- puts "Get information about a user"
118
- print "Press enter to execute and see the response"
119
- gets
120
- user_info = @@api.user.get(@@login)
121
- PP.pp(user_info)
122
- end
123
-
124
- def OrdrinDemo.create_user_demo()
125
- puts "Create a user"
126
- print "Press enter to execute and see the response"
127
- gets
128
- response = @@api.user.create(@@login, @@first_name, @@last_name)
129
- PP.pp(response)
130
- end
131
-
132
- def OrdrinDemo.update_user_demo()
133
- puts "Update a user"
134
- print "Press enter to execute and see the response"
135
- gets
136
- response = @@api.user.update(@@login, @@alt_first_name, @@last_name)
137
- PP.pp(response)
138
- end
139
-
140
- def OrdrinDemo.get_all_addresses_demo()
141
- puts "Get a list of all saved addresses"
142
- print "Press enter to execute and see the response"
143
- gets
144
- address_list = @@api.user.get_all_addresses(@@login)
145
- PP.pp(address_list)
146
- end
147
-
148
- def OrdrinDemo.get_address_demo()
149
- puts "Get an address by nickname"
150
- print "Press enter to execute and see the response"
151
- gets
152
- addr = @@api.user.get_address(@@login, @@address_nick)
153
- PP.pp(addr)
154
- end
155
-
156
- def OrdrinDemo.set_address_demo()
157
- puts "Save an address with a nickname"
158
- response = @@api.user.set_address(@@login, @@address_nick, @@address)
159
- PP.pp(response)
160
- end
161
-
162
- def OrdrinDemo.remove_address_demo()
163
- puts "Remove a saved address by nickname"
164
- print "Press enter to execute and see the response"
165
- gets
166
- response = @@api.user.remove_address(@@login, @@address_nick)
167
- PP.pp(response)
168
- end
169
-
170
- def OrdrinDemo.get_all_credit_cards_demo()
171
- puts "Get a list of all saved credit cards"
172
- credit_card_list = @@api.user.get_all_credit_cards(@@login)
173
- PP.pp(credit_card_list)
174
- end
175
-
176
- def OrdrinDemo.get_credit_card_demo()
177
- puts "Get a saved credit card by nickname"
178
- print "Press enter to execute and see the response"
179
- gets
180
- credit_card = @@api.user.get_credit_card(@@login, @@credit_card_nick)
181
- PP.pp(credit_card)
182
- end
183
-
184
- def OrdrinDemo.set_credit_card_demo()
185
- puts "Save a credit card with a nickname"
186
- print "Press enter to execute and see the response"
187
- gets
188
- response = @@api.user.set_credit_card(@@login, @@credit_card_nick, @@credit_card)
189
- PP.pp(response)
190
- end
191
-
192
- def OrdrinDemo.remove_credit_card_demo()
193
- puts "Remove a saved credit card by nickname"
194
- print "Press enter to execute and see the response"
195
- gets
196
- response = @@api.user.remove_credit_card(@@login, @@credit_card_nick)
197
- PP.pp(response)
198
- end
199
-
200
- def OrdrinDemo.get_order_history_demo(login)
201
- puts "Get a list of all orders made by this user"
202
- print "Press enter to execute and see the response"
203
- gets
204
- order_list = @@api.user.get_order_history(@@login)
205
- PP.pp(order_list)
206
- end
207
-
208
- def OrdrinDemo.get_order_detail_demo(oid)
209
- puts "Get the details of a particular order made by this user"
210
- print "Press enter to execute and see the response"
211
- gets
212
- order_detail = @@api.user.get_order_detail(@@login, oid)
213
- PP.pp(order_detail)
214
- end
215
-
216
- def OrdrinDemo.set_password_demo()
217
- puts "Set a new password for a user"
218
- print "Press enter to execute and see the response"
219
- gets
220
- response = @@api.user.set_password(@@login, @@new_password)
221
- PP.pp(response)
222
- end
223
-
224
- #
225
- # Order demo functions
226
- #
227
-
228
- def OrdrinDemo.anonymous_order_demo(restaurant_id, tray, date_time)
229
- puts "Order food as someone without a user account"
230
- print "Press enter to execute and see the response"
231
- gets
232
- tip = Random.rand(500)/100.0
233
- response = @@api.order.order(restaurant_id, tray, tip, date_time, @@first_name, @@last_name, @@address, @@credit_card, @@email)
234
- PP.pp(response)
235
- end
236
-
237
- def OrdrinDemo.create_user_and_order_demo(restaurant_id, tray, date_time)
238
- puts "Order food and create an account"
239
- print "Press enter to execute and see the response"
240
- gets
241
- tip = Random.rand(500)/100.0
242
- response = @@api.order.order_create_user(restaurant_id, tray, tip, date_time, @@first_name, @@last_name, @@address, @@credit_card, @@alt_email, @@password)
243
- PP.pp(response)
244
- end
245
-
246
- def OrdrinDemo.order_with_nicks_demo(restaurant_id, tray, date_time)
247
- puts "Order food as a logged in user using previously stored address and credit card"
248
- print "Press enter to execute and see the response"
249
- gets
250
- tip = Random.rand(500)/100.0
251
- response = @@api.order.order(restaurant_id, tray, tip, date_time, @@first_name, @@last_name, @@address_nick, @@credit_card_nick, nil, @@login)
252
- PP.pp(response)
253
- return response
254
- end
255
-
256
- def OrdrinDemo.find_item_to_order(item_list)
257
- for item in item_list
258
- if item['is_orderable']=='1'
259
- if item['price'].to_f>=5.00
260
- return item['id']
261
- end
262
- else
263
- if item.has_key?('children')
264
- item_id = find_item_to_order(item['children'])
265
- unless item_id.nil?
266
- return item_id
267
- end
268
- end
269
- end
270
- end
271
- nil
272
- end
273
-
274
-
275
- #
276
- # Main
277
- #
278
- def OrdrinDemo.run_demo()
279
- puts "Run through the entire demo sequence"
280
- # Restaurant functions
281
- delivery_list = delivery_list_immediate_demo()
282
- delivery_list_future_demo()
283
- restaurant_id = delivery_list[0]['id']
284
- delivery_check_demo(restaurant_id)
285
- fee_demo(restaurant_id)
286
- detail = detail_demo(restaurant_id)
287
-
288
- # User functions
289
- create_user_demo()
290
- get_user_demo()
291
- update_user_demo()
292
- get_user_demo()
293
- set_address_demo()
294
- get_address_demo()
295
- set_credit_card_demo()
296
- get_credit_card_demo()
297
-
298
- # Order functions
299
- order_date_time = find_deliverable_time(restaurant_id)
300
- puts "Ordering food at #{order_date_time}"
301
- item_id = find_item_to_order(detail['menu'])
302
- item = Ordrin::Data::TrayItem.new(item_id, 10)
303
- tray = Ordrin::Data::Tray.new(item)
304
- anonymous_order_demo(restaurant_id, tray, order_date_time)
305
- order = order_with_nicks_demo(restaurant_id, tray, order_date_time)
306
- unless order.nil?
307
- get_order_detail_demo(order['refnum'])
308
- end
309
-
310
- create_user_and_order_demo(restaurant_id, tray, order_date_time)
311
- get_order_history_demo(@@alt_login)
312
-
313
- # Clean up/removing stuff
314
- remove_address_demo()
315
- get_all_addresses_demo()
316
- remove_credit_card_demo()
317
- get_all_credit_cards_demo()
318
- set_password_demo()
319
- #After changing the password I must change the login object to continue to access user info
320
- end
321
-
322
- run_demo()
323
- end
data/lib/ordrin/data.rb DELETED
@@ -1,148 +0,0 @@
1
- require 'digest'
2
- require_relative 'normalize'
3
- module Ordrin
4
- module Data
5
- # Base class for objects that can save any data with the constructor and then
6
- # extract it as a dictionary
7
- class OrdrinData
8
- # Return a dictionary of particular fields to values, determined per subclass
9
- def make_dict
10
- dict = {}
11
- fields.map {|f| dict[f.to_s]=self.send(f) unless self.send(f).nil?}
12
- return dict
13
- end
14
- end
15
-
16
- # Represents a street address
17
- class Address < OrdrinData
18
-
19
- attr_reader :addr, :city, :state, :zip, :phone, :addr2, :fields
20
-
21
- # Store the parts of the address as fields in this object.
22
- # Arguments:
23
- # addr -- Street address
24
- # city -- City
25
- # state -- State
26
- # zip -- Zip code
27
- # phone -- Phone number
28
- # addr2 -- Optional second street address line
29
- def initialize(addr, city, state, zip, phone, addr2='')
30
- @fields = [:addr, :city, :state, :zip, :phone, :addr2]
31
- @addr = addr
32
- @city = city
33
- @state = Normalize.normalize(state, :state)
34
- @zip = Normalize.normalize(zip, :zip)
35
- @phone = Normalize.normalize(phone, :phone)
36
- end
37
- end
38
-
39
- # Represents information about a credit card
40
- class CreditCard < OrdrinData
41
-
42
- attr_reader :expiry_month, :expiry_year, :number, :cvc, :type, :name, :bill_address, :fields
43
-
44
- # Store the credit card info as fields in this object.
45
- # Arguments:
46
- # name -- The name (first and last) on the credit card
47
- # expiry_month -- The month that the card expires (two digits)
48
- # expiry_year -- The year that the card expires (four digits)
49
- # bill_address -- The billing address. Should be an Ordrin::Data::Address object
50
- # number -- The credit card number
51
- # cvc -- The card verification number
52
- def initialize(name, expiry_month, expiry_year, bill_address, number, cvc)
53
- @fields = [:number, :cvc, :expiry_month, :expiry_year, :expiry,
54
- :bill_addr, :bill_addr2, :bill_city, :bill_state, :bill_zip,
55
- :phone, :name]
56
- @expiry_month = Normalize.normalize(expiry_month, :month)
57
- @expiry_year = Normalize.normalize(expiry_year, :year)
58
- @number, @cvc, @type = Normalize.normalize([number, cvc], :credit_card)
59
- @name = name
60
- @bill_address = bill_address
61
- end
62
-
63
- def bill_addr
64
- @bill_address.addr
65
- end
66
-
67
- def bill_addr2
68
- @bill_address.addr2
69
- end
70
-
71
- def bill_city
72
- @bill_address.city
73
- end
74
-
75
- def bill_state
76
- @bill_address.state
77
- end
78
-
79
- def bill_zip
80
- @bill_address.zip
81
- end
82
-
83
- def phone
84
- @bill_address.phone
85
- end
86
-
87
- # A combination of the expiry_month and expiry_date
88
- def expiry
89
- "#{expiry_month}/#{expiry_year}"
90
- end
91
- end
92
-
93
- #Represents a user's login information
94
- class UserLogin < OrdrinData
95
-
96
- attr_reader :email, :password, :fields
97
-
98
- # Store the email and password in this object. Saves only the hash of the
99
- # password, not the password itself
100
- # Arguments:
101
- # email -- The user's email address
102
- # password -- The user's password (in plain text)
103
- def initialize(email, password)
104
- @fields = [:email, :password]
105
- @email = email
106
- @password = UserLogin.hash_password(password)
107
- end
108
-
109
- def UserLogin.hash_password(password)
110
- return Digest::SHA256.new.hexdigest(password)
111
- end
112
- end
113
-
114
- # Represents a single item in an order
115
- class TrayItem
116
-
117
- # Store the descriptors of an order item in this object.
118
- # Arguments:
119
- # item_id -- the restaurants's numerial ID for the item
120
- # quantity -- the quantity
121
- # options -- any number of options to apply to the item
122
- def initialize(item_id, quantity, *options)
123
- @item_id = Normalize.normalize(item_id, :number)
124
- @quantity = Normalize.normalize(quantity, :number)
125
- @options = options.map {|opt| Normalize.normalize(opt, :number)}
126
- end
127
-
128
- def to_s
129
- "#{@item_id}/#{@quantity},#{@options*','}"
130
- end
131
- end
132
-
133
- # Represents a list of items in an order
134
- class Tray
135
-
136
- # Store the list of items in this object. Each argument should be of type Item
137
- # Arguments:
138
- # items -- A list of items to be ordered in this tray
139
- def initialize(*items)
140
- @items = items
141
- end
142
-
143
- def to_s
144
- return @items*'+'
145
- end
146
- end
147
- end
148
- end
data/lib/ordrin/errors.rb DELETED
@@ -1,147 +0,0 @@
1
- module Ordrin
2
- module Errors
3
- # This is the base class for errors specific to this ordrin package
4
- class OrdrinError < Exception
5
- attr_reader :msg
6
-
7
- def initialize(msg=nil)
8
- @msg = msg
9
- end
10
- end
11
-
12
- # This error encapsulates an API error returned by the server.
13
- class ApiError < OrdrinError
14
- attr_reader :text
15
- def initialize(msg=nil, text=nil)
16
- super(msg)
17
- @text = text
18
- end
19
-
20
- def to_s
21
- "ApiError(msg='#{msg}', text='#{text}')"
22
- end
23
- end
24
-
25
- # This error indicates that the server returned a response that could not be
26
- # parsed into JSON
27
- class ApiInvalidResponseError < OrdrinError
28
- def to_s
29
- "ApiInvalidResponseError(msg='#{msg}')"
30
- end
31
- end
32
-
33
- def Errors.state
34
- lambda do |value|
35
- ArgumentError.new("State must be a two letter postal code abbreviation: #{value}")
36
- end
37
- end
38
-
39
- def Errors.money
40
- lambda do |value|
41
- ArgumentError.new("Money must be dollars.cents: #{value}")
42
- end
43
- end
44
-
45
- def Errors.zip
46
- lambda do |value|
47
- ArgumentError.new("Zip code must be exactly 5 digits: #{value}")
48
- end
49
- end
50
-
51
- def Errors.phone
52
- lambda do |value|
53
- ArgumentError.new("Phone numbers must have exactly 10 digits: #{value}")
54
- end
55
- end
56
-
57
- def Errors.number
58
- lambda do |value|
59
- ArgumentError.new("This value must be only digits: #{value}")
60
- end
61
- end
62
-
63
- def Errors.month
64
- lambda do |value|
65
- ArgumentError.new("Months must be two digits: #{value}")
66
- end
67
- end
68
-
69
- def Errors.year
70
- lambda do |value|
71
- ArgumentError.new("Years must be four digits: #{value}")
72
- end
73
- end
74
-
75
- def Errors.cvc
76
- lambda do |value|
77
- ArgumentError.new("Credit card CVC must be 3 or 4 digits, depending on the card type: #{value}")
78
- end
79
- end
80
-
81
- def Errors.credit_card
82
- lambda do |value|
83
- ArgumentError.new("Credit card number must be a valid AmEx, Discover, Mastercard, or Visa card number: #{value}")
84
- end
85
- end
86
-
87
- def Errors.email
88
- lambda do |value|
89
- ArgumentError.new("Bad email format: #{value}")
90
- end
91
- end
92
-
93
- def Errors.normalizer
94
- lambda do |value|
95
- ArgumentError.new("Unknown validator name: #{value}")
96
- end
97
- end
98
-
99
- def Errors.nick
100
- lambda do |value|
101
- ArgumentError.new("Nick names can only have letters, nubmers, dashes, and underscores: #{value}")
102
- end
103
- end
104
-
105
- def Errors.date_time
106
- lambda do |value|
107
- ArgumentError.new("date_time must be a datetime.datetime object or the string 'ASAP': #{value}")
108
- end
109
- end
110
-
111
- def Errors.date
112
- lambda do |value|
113
- ArgumentError.new("date must be a datetime.datetime or datetime.date object or the string 'ASAP': #{value}")
114
- end
115
- end
116
-
117
- def Errors.time
118
- lambda do |value|
119
- ArgumentError.new("time must be a datetime.datetime or datetime.time object: #{value}")
120
- end
121
- end
122
-
123
- def Errors.url
124
- lambda do |value|
125
- ArgumentError.new("url must be a proper url: #{value}")
126
- end
127
- end
128
-
129
- def Errors.method
130
- lambda do |value|
131
- ArgumentError.new("method must be a word: #{value}")
132
- end
133
- end
134
-
135
- def Errors.alphanum
136
- lambda do |value|
137
- ArgumentError.new("This value must be alphanumeric: #{value}")
138
- end
139
- end
140
-
141
- def Errors.request_method
142
- lambda do |value|
143
- ApiError.new("Method not a valid HTTP request method: #{value}")
144
- end
145
- end
146
- end
147
- end