/
api.rb
127 lines (95 loc) · 2.63 KB
/
api.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
module Bigcommerce
class Api
def initialize(configuration={})
@connection = Connection.new(configuration)
end
# Added getter to ensure configuration is correct
def connection
@connection
end
def store_url=(store_url)
@connection.store_url = store_url
end
def username=(username)
@connection.username = username
end
def api_key=(api_key)
@connection.api_key = api_key
end
def verify_peer=(verify)
@connection.verify_peer = verify
end
def ca_file=(path)
@connection.ca_file = path
end
# Returns the date formatted as
# RFC 2822 string
def to_rfc2822(datetime)
datetime.strftime("%a, %d %b %Y %H:%M:%S %z")
end
def get_time
@connection.get '/time'
end
def get_products(params={})
@connection.get '/products'
end
def get_brands
@connection.get '/brands'
end
def get_product(id)
@connection.get '/products/' + id.to_s
end
def get_categories
@connection.get '/categories'
end
def get_category(id)
@connection.get '/categories/' + id.to_s
end
def get_orders(params={})
@connection.get('/orders', :params => params)
end
def get_orders_by_date(date, params={})
if date.is_a?(String)
date = DateTime.parse(date)
end
date = to_rfc2822(date)
@connection.get('/orders', :params => params.merge!(:min_date_created => CGI::escape(date)))
end
def get_orders_count
get_count @connection.get '/orders/count'
end
def get_order(id)
@connection.get '/orders/' + id.to_s
end
def get_order_products(id)
@connection.get '/orders/' + id.to_s + '/products'
end
def get_orders_modified_since(datetime)
@connection.get('/orders', :headers => {'If-Modified-Since' => CGI::escape(to_rfc2822(datetime))})
end
def get_customers(_filters = {})
url_filters = _filters.map{|k,v| "#{k}=#{v}"}.join("&")
@connection.get "/customers#{url_filters.present? ? ("?" + url_filters) : ""}"
end
def get_customer(id)
@connection.get '/customers/' + id.to_s
end
def get_customer_count
@connection.get '/customers/count'
end
def create_product(attributes)
@connection.post('/products', :body => attributes.to_xml(:root => 'product'))
end
def update_product(id, attributes)
@connection.put("/products/#{id}", :body => attributes.to_xml(:root => 'product'))
end
private
def get_count(result)
result["count"]
end
def get_resource(result)
end
def get_collection(result)
end
end
end