acenda-client 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/acenda-client.rb +253 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6323bffab1ce23b5fcd8195814b6a450f6ec6a97
|
4
|
+
data.tar.gz: 8ca9a125d8cec204bfe552d9459d96301499f309
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e81604031e2ebfcf91b5f7a637b1eb7da8e1795ba04dcb9691f2f041cdba702ebbb0e759843d29e6f0cc7290e6c8b9afea19924c868494725436a50c04dc72f5
|
7
|
+
data.tar.gz: 44fbdca6ca2a1f537fcf3e1b6703c432bbd44056b093f14473d4edc0b9f6601e5aa8c1abe4ecd5adf93654433cf3567da061fc447137e5123ca5e68cd9fa7c7c
|
@@ -0,0 +1,253 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module URI
|
5
|
+
remove_const :DEFAULT_PARSER
|
6
|
+
unreserved = REGEXP::PATTERN::UNRESERVED
|
7
|
+
DEFAULT_PARSER = Parser.new(:UNRESERVED => unreserved + "\{\}")
|
8
|
+
end
|
9
|
+
|
10
|
+
module Acenda
|
11
|
+
class APIErrorHTTP < StandardError; end
|
12
|
+
class APIErrorClient < StandardError; end
|
13
|
+
class Response
|
14
|
+
require 'json'
|
15
|
+
|
16
|
+
def initialize(response, url="", params=[], debug = false)
|
17
|
+
@logger = Logger.new(STDOUT)
|
18
|
+
@url = url
|
19
|
+
@params = params
|
20
|
+
@response = treat_response(response)
|
21
|
+
|
22
|
+
@logger.info "Acenda:: response body:" if debug == true
|
23
|
+
@logger.info response.body if debug == true
|
24
|
+
@logger.info "Acenda:: response code:" if debug == true
|
25
|
+
@logger.info response.code if debug == true
|
26
|
+
puts "\r\n" if debug == true
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_url()
|
30
|
+
return @url
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_params()
|
34
|
+
return @params
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_result()
|
38
|
+
return @response[:result]
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_number()
|
42
|
+
return @response[:num_total]
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_code()
|
46
|
+
raise Acenda::APIErrorHTTP, "Request didn't go through and error cannot be parsed." if @response[:code].to_i == 0
|
47
|
+
return @response[:code].to_i
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_status()
|
51
|
+
return @response[:status]
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def treat_response(http_response)
|
57
|
+
begin
|
58
|
+
if http_response.class == String
|
59
|
+
return symbolize_result(JSON.parse(http_response))
|
60
|
+
else
|
61
|
+
return {
|
62
|
+
:result => symbolize_result(JSON.parse(http_response.body)),
|
63
|
+
:code => http_response.code().to_i,
|
64
|
+
:num_total => 1,
|
65
|
+
:status => http_response.message()
|
66
|
+
}
|
67
|
+
end
|
68
|
+
rescue JSON::ParserError => e
|
69
|
+
raise Acenda::APIErrorHTTP, "JSON response is not a good format for query #{@url.to_s} and params #{@params.to_json}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def symbolize_result(source)
|
74
|
+
dest = source.is_a?(Array) ? [] : {}
|
75
|
+
|
76
|
+
source.each_with_index { |(key,value), index|
|
77
|
+
k = key.is_a?(String) ? key.to_sym : key
|
78
|
+
if value.is_a? Hash or value.is_a? Array
|
79
|
+
dest[k] = symbolize_result(value)
|
80
|
+
else
|
81
|
+
dest[k] = value
|
82
|
+
end
|
83
|
+
} if source.is_a? Hash
|
84
|
+
|
85
|
+
source.each_with_index { |value, index|
|
86
|
+
dest[index] = symbolize_result(value)
|
87
|
+
} if source.class == Array
|
88
|
+
|
89
|
+
return dest
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class API
|
94
|
+
require 'uri'
|
95
|
+
require 'openssl'
|
96
|
+
require 'net/https'
|
97
|
+
|
98
|
+
def initialize(client_id, client_secret, store_url, verify_ssl = true, debug = false)
|
99
|
+
if (client_id.is_a? String and client_secret.is_a? String and store_url.is_a? String)
|
100
|
+
raise Acenda::APIErrorClient, "store_url MUST be a valid URL" unless store_url =~ URI::regexp
|
101
|
+
@logger = Logger.new(STDOUT)
|
102
|
+
@config = {
|
103
|
+
:client_id => client_id,
|
104
|
+
:client_secret => client_secret,
|
105
|
+
:store_url => store_url + (store_url.split('').last == '/' ? 'api' : '/api'),
|
106
|
+
:access_token => nil,
|
107
|
+
:acenda_api_url => store_url,
|
108
|
+
:verify_ssl => verify_ssl,
|
109
|
+
:debug => debug
|
110
|
+
}
|
111
|
+
|
112
|
+
@logger.info "Acenda:: Configuration:" if @config[:debug] == true
|
113
|
+
@logger.info @config.to_json
|
114
|
+
puts "\r\n" if @config[:debug] == true
|
115
|
+
|
116
|
+
else
|
117
|
+
raise Acenda::APIErrorClient, "Wrong parameters type provided to Acenda::API"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def query(verb, uri, params={})
|
122
|
+
if (verb.is_a? String and uri.is_a? String and params.is_a? Hash)
|
123
|
+
generate_token() if (!@config[:access_token])
|
124
|
+
|
125
|
+
json_headers = {"Content-Type" => "application/json",
|
126
|
+
"Accept" => "application/json"}
|
127
|
+
begin
|
128
|
+
case verb.downcase
|
129
|
+
when "get"
|
130
|
+
query = generate_query(uri, params)
|
131
|
+
|
132
|
+
http = Net::HTTP.new(query.host, query.port)
|
133
|
+
|
134
|
+
http.use_ssl = true if query.scheme == "https"
|
135
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if query.scheme == "https" and !@config[:verify_ssl]
|
136
|
+
|
137
|
+
@logger.info "Acenda:: URI:" if @config[:debug] == true
|
138
|
+
@logger.info uri.to_s if @config[:debug] == true
|
139
|
+
@logger.info "Acenda:: PARAMS:" if @config[:debug] == true
|
140
|
+
@logger.info params if @config[:debug] == true
|
141
|
+
puts "\r\n" if @config[:debug] == true
|
142
|
+
|
143
|
+
return Acenda::Response.new(http.get(query), query, params, @config[:debug])
|
144
|
+
when "post"
|
145
|
+
query = generate_query(uri)
|
146
|
+
|
147
|
+
http = Net::HTTP.new(query.host, query.port)
|
148
|
+
|
149
|
+
http.use_ssl = true if query.scheme == "https"
|
150
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if query.scheme == "https" and !@config[:verify_ssl]
|
151
|
+
|
152
|
+
@logger.info "Acenda:: URI:" if @config[:debug] == true
|
153
|
+
@logger.info uri.to_s if @config[:debug] == true
|
154
|
+
@logger.info "Acenda:: PARAMS:" if @config[:debug] == true
|
155
|
+
@logger.info params if @config[:debug] == true
|
156
|
+
puts "\r\n" if @config[:debug] == true
|
157
|
+
|
158
|
+
return Acenda::Response.new http.post(query, params.to_json, json_headers), query, params, @config[:debug]
|
159
|
+
when "put"
|
160
|
+
query = generate_query(uri)
|
161
|
+
|
162
|
+
http = Net::HTTP.new(query.host, query.port)
|
163
|
+
|
164
|
+
http.use_ssl = true if query.scheme == "https"
|
165
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if query.scheme == "https" and !@config[:verify_ssl]
|
166
|
+
|
167
|
+
@logger.info "Acenda:: URI:" if @config[:debug] == true
|
168
|
+
@logger.info uri.to_s if @config[:debug] == true
|
169
|
+
@logger.info "Acenda:: PARAMS:" if @config[:debug] == true
|
170
|
+
@logger.info params if @config[:debug] == true
|
171
|
+
puts "\r\n" if @config[:debug] == true
|
172
|
+
|
173
|
+
return Acenda::Response.new(http.put(query, params.to_json, json_headers), query, params, @config[:debug])
|
174
|
+
when "delete"
|
175
|
+
query = generate_query(uri, params)
|
176
|
+
|
177
|
+
http = Net::HTTP.new(query.host, query.port)
|
178
|
+
|
179
|
+
http.use_ssl = true if query.scheme == "https"
|
180
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if query.scheme == "https" and !@config[:verify_ssl]
|
181
|
+
|
182
|
+
@logger.info "Acenda:: URI:" if @config[:debug] == true
|
183
|
+
@logger.info uri.to_s if @config[:debug] == true
|
184
|
+
@logger.info "Acenda:: PARAMS:" if @config[:debug] == true
|
185
|
+
@logger.info params if @config[:debug] == true
|
186
|
+
puts "\r\n" if @config[:debug] == true
|
187
|
+
|
188
|
+
return Acenda::Response.new(http.delete(query), query, params, @config[:debug])
|
189
|
+
else
|
190
|
+
raise Acenda::APIErrorClient, "Verb not recognized yet"
|
191
|
+
end
|
192
|
+
rescue => e
|
193
|
+
e.backtrace.push("\r\nError custom trace:\r\nQuery: #{query}\r\n\r\nParams: #{params}\r\n")
|
194
|
+
end
|
195
|
+
else
|
196
|
+
raise Acenda::APIErrorClient, "Wrong parameters type provided to Acenda::API.query"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
@config = {}
|
201
|
+
|
202
|
+
def generate_query(uri, params={})
|
203
|
+
params = params.merge :access_token => @config[:access_token]
|
204
|
+
|
205
|
+
parameters = ""
|
206
|
+
params.each_with_index do |(k,v), i|
|
207
|
+
parameters += "&" unless i < 1
|
208
|
+
parameters += k.to_s+"="+URI.encode(v.to_s)
|
209
|
+
end if params != ""
|
210
|
+
|
211
|
+
@route = @config[:store_url]
|
212
|
+
@route += (uri.split('').first == '/') ? uri : '/'+uri
|
213
|
+
@route += (uri.count('?') > 0 ? '&' : '?')+parameters
|
214
|
+
|
215
|
+
@route = URI(@route)
|
216
|
+
|
217
|
+
return @route
|
218
|
+
end
|
219
|
+
|
220
|
+
def generate_token()
|
221
|
+
json_headers = {"Content-Type" => "application/json",
|
222
|
+
"Accept" => "application/json"}
|
223
|
+
|
224
|
+
uri = URI.parse(@config[:acenda_api_url]+"/oauth/token")
|
225
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
226
|
+
|
227
|
+
http.use_ssl = true if uri.scheme == "https"
|
228
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme == "https" and !@config[:verify_ssl]
|
229
|
+
|
230
|
+
params = {
|
231
|
+
"client_id" => @config[:client_id],
|
232
|
+
"client_secret" => @config[:client_secret],
|
233
|
+
"grant_type" => "client_credentials"
|
234
|
+
}
|
235
|
+
|
236
|
+
@logger.info "Acenda:: URI:" if @config[:debug] == true
|
237
|
+
@logger.info uri.to_s if @config[:debug] == true
|
238
|
+
@logger.info "Acenda:: PARAMS:" if @config[:debug] == true
|
239
|
+
@logger.info params if @config[:debug] == true
|
240
|
+
puts "\r\n" if @config[:debug] == true
|
241
|
+
|
242
|
+
begin
|
243
|
+
response = Acenda::Response.new http.post(uri, params.to_json, json_headers), uri, params, @config[:debug]
|
244
|
+
rescue => e
|
245
|
+
e.backtrace.push("\r\nError custom trace:\r\nQuery: #{query}\r\n\r\nParams: #{params}\r\n")
|
246
|
+
end
|
247
|
+
@config[:access_token] = response.get_result()[:access_token] unless response.get_code() != 200
|
248
|
+
raise Acenda::APIErrorHTTP, "Token generation failed #{response.get_code()}" if response.get_code() != 200
|
249
|
+
end
|
250
|
+
|
251
|
+
private :generate_query, :generate_token
|
252
|
+
end
|
253
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acenda-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.11
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yoann Jaspar
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
- - '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.8.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.8'
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.8.2
|
33
|
+
description: HTTP dialog for the Acenda API with the oAuth2 authentication.
|
34
|
+
email: yoannj@acenda.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- lib/acenda-client.rb
|
40
|
+
homepage: https://github.com/torreycommerce/acenda-ruby-sdk
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.4.6
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Acenda client to access the API in Ruby.
|
64
|
+
test_files: []
|