-
Notifications
You must be signed in to change notification settings - Fork 1
/
ebay_integration.rb
110 lines (93 loc) · 3.11 KB
/
ebay_integration.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
require 'sinatra'
require 'json'
require 'endpoint_base'
require './lib/ebay'
class EbayIntegration < EndpointBase::Sinatra::Base
enable :logging
post '/get_session_id' do
response = Ebay.new(@payload, @config).get_session_id
if response.success?
result 200, response.payload[:session_id]
else
result 500, 'Something Went Wrong. Please try again.'
end
end
post '/fetch_token' do
response = Ebay.new(@payload, @config).fetch_token
if response.success?
result 200, response.payload[:e_bay_auth_token]
else
result 500, 'Something Went Wrong. Please try again.'
end
end
post '/get_products' do
response = Ebay.new(@payload, @config).get_products
if response.success?
add_parameter 'ebay_start_time_from', Time.now - 30*24*60*60
add_parameter 'ebay_start_time_to', Time.now - 30*24*60*60
add_parameter 'ebay_page_number', (response.payload[:has_more_items] ? @config[:ebay_page_number].to_i + 1 : 1)
[response.payload[:item_array][:item]].flatten.each do |item|
add_object 'product', Product.wombat_product_hash(item)
Inventory.wombat_inventories_hash(item).each { |inventory_hash| add_object 'inventory', inventory_hash }
end if response.payload[:item_array]
result 200
else
result 500, response.errors.first.long_message
end
end
post '/get_orders' do
response = Ebay.new(@payload, @config).get_orders
if response.success?
add_parameter 'ebay_mod_time_from', Time.now - 25*24*60*60
add_parameter 'ebay_page_number', (response.payload[:has_more_orders] ? @config[:ebay_page_number].to_i + 1 : 1)
[response.payload[:order_array][:order]].flatten.each do |item|
add_object 'order', Order.wombat_order_hash(item)
add_object 'shipment', Shipment.wombat_shipment_hash(item)
end if response.payload[:order_array]
result 200
else
result 500, response.errors.first.long_message
end
end
post '/set_inventory' do
response = Ebay.new(@payload, @config).set_inventory
if response.success?
result 200
else
result 500, response.errors.first.long_message
end
end
post '/add_product' do
response = Ebay.new(@payload, @config).add_product
if response.success?
add_object 'product', @payload[:product].merge({ ebay_item_id: response.payload[:item_id] })
result 200, "Product with #{ response.payload[:item_id] } is added to eBay."
else
result 500, response.errors.first.long_message
end
end
post '/update_product' do
response = Ebay.new(@payload, @config).update_product
if response.success?
result 200
else
result 500, response.errors.first.long_message
end
end
post '/add_shipment' do
response = Ebay.new(@payload, @config).add_shipment
if response.success?
result 200
else
result 500, response.errors.first.long_message
end
end
post '/update_shipment' do
response = Ebay.new(@payload, @config).add_shipment
if response.success?
result 200
else
result 500, response.errors.first.long_message
end
end
end