-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_client.rb
155 lines (136 loc) · 4.47 KB
/
order_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'httparty'
class OrderClient
include HTTParty
format :json
base_uri 'http://localhost:8080'
def OrderClient.create(newOrder)
post '/orders',
body: newOrder.to_json,
headers: { 'Content-Type' => 'application/json',
'ACCEPT' => 'application/json' }
end
def OrderClient.custOrdersByEmail(email)
get "/orders?email=#{email}"
end
def OrderClient.custOrderById(id)
get "/orders?id=#{id}"
end
def OrderClient.id(id)
get "orders/#{id}"
end
end
class CustomerClient
include HTTParty
format :json
base_uri 'http://localhost:8081'
def CustomerClient.register(customer)
post '/customers',
body: customer.to_json,
headers: { 'Content-Type' => 'application/json',
'ACCEPT' => 'application/json' }
end
def CustomerClient.email(email)
get "/customers?email=#{email}"
end
def CustomerClient.id(id)
get "/customers?id=#{id}"
end
end
class ItemClient
include HTTParty
format :json
base_uri 'http://localhost:8082'
def ItemClient.add(item)
post '/items',
body: item.to_json,
headers: { 'Content-Type' => 'application/json',
'ACCEPT' => 'application/json'}
end
def ItemClient.updateItem(item)
put '/items',
body: item.to_json,
headers: { 'Content-Type' => 'application/json',
'ACCEPT' => 'application/json'}
end
def ItemClient.id(id)
get "/items?id=#{id}"
end
end
quit = false
while !quit
puts "What do you want to do: CreateOrder, RetrieveOrder, RegisterCustomer, LookupCustomer, CreateItem, LookupItem, or Quit"
input = gets.chomp!
if input == 'CreateOrder'
arr = Array.new
puts "Enter Item id"
arr[0] = gets.chomp!
puts "Enter Customer Email"
arr[1] = gets.chomp!
response = OrderClient.create itemId: arr[0], email: arr[1]
puts "status code #{response.code}"
puts response.body
elsif input == 'RetrieveOrder'
puts "Would you like to retrieve the order by OrderID, CustomerID, or CustomerEmail"
input = gets.chomp!
if input == "OrderID"
puts "Enter OrderID"
input = gets.chomp!
response = OrderClient.id(input)
puts "status code #{response.code}"
puts response.body
elsif input == "CustomerID"
puts "Enter CustomerID"
input = gets.chomp!
response = OrderClient.custOrderById(input)
puts "status code #{response.code}"
puts response.body
elsif input == "CustomerEmail"
puts "Enter CustomerEmail"
input = gets.chomp!
response = OrderClient.custOrdersByEmail(input)
puts "status code #{response.code}"
puts response.body
end
elsif input == 'RegisterCustomer'
puts 'enter lastName, firstName and email for new customer'
register = gets.chomp!
check = register.split(' ')
response = CustomerClient.register lastName: check[0], firstName: check[1], email: check[2]
puts "status code #{response.code}"
puts response.body
elsif input == 'LookupCustomer'
puts "lookup by ID or email?"
input = gets.chomp!
if input == 'ID'
puts "enter customer ID"
input = gets.chomp!
response = CustomerClient.id(id)
puts "status code #{response.code}"
puts response.body
elsif input == 'email'
puts "enter customer Email"
response = CustomerClient.email(email)
puts "status code #{response.code}"
puts response.body
end
elsif input == 'CreateItem'
arr = Array.new
puts 'enter item description'
arr[1] = gets.chomp!
puts 'enter item price'
arr[2] = gets.chomp!
puts 'enter item stockQty'
arr[3] = gets.chomp!
response = ItemClient.add id: arr[0], description: arr[1], price: arr[2], stockQty: arr[3]
puts "status code #{response.code}"
puts response.body
elsif input == 'LookupItem'
puts 'enter id of item to lookup'
id = gets.chomp!
response = ItemClient.id(id)
puts "status code #{response.code}"
puts response.body
elsif input == 'Quit'
quit = true
end
end