Class: Vend::HttpClient
- Inherits:
-
Object
- Object
- Vend::HttpClient
- Includes:
- Logable
- Defined in:
- lib/vend/http_client.rb
Constant Summary collapse
- UNAUTHORIZED_MESSAGE =
"Client not authorized. Check your store credentials are correct and try again."- READ_TIMEOUT =
Read timeout in seconds
240
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
-
#verify_ssl ⇒ Object
(also: #verify_ssl?)
Returns the value of attribute verify_ssl.
Attributes included from Logable
Instance Method Summary collapse
-
#get_http_connection(host, port, use_ssl = true) ⇒ Object
sets up a http connection.
-
#initialize(options = {}) ⇒ HttpClient
constructor
A new instance of HttpClient.
-
#request(path, options = {}) ⇒ Object
Makes a request to the specified path within the Vend API E.g.
-
#verify_mode ⇒ Object
Returns the SSL verification mode, based upon the value of verify_ssl?.
Constructor Details
#initialize(options = {}) ⇒ HttpClient
Returns a new instance of HttpClient.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/vend/http_client.rb', line 15 def initialize( = {}) @base_url = [:base_url] @username = [:username] @password = [:password] @auth_token = [:auth_token] @verify_ssl = if .key?(:verify_ssl) [:verify_ssl] else true end end |
Instance Attribute Details
#auth_token ⇒ Object
Returns the value of attribute auth_token.
12 13 14 |
# File 'lib/vend/http_client.rb', line 12 def auth_token @auth_token end |
#base_url ⇒ Object
Returns the value of attribute base_url.
12 13 14 |
# File 'lib/vend/http_client.rb', line 12 def base_url @base_url end |
#password ⇒ Object
Returns the value of attribute password.
12 13 14 |
# File 'lib/vend/http_client.rb', line 12 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
12 13 14 |
# File 'lib/vend/http_client.rb', line 12 def username @username end |
#verify_ssl ⇒ Object Also known as: verify_ssl?
Returns the value of attribute verify_ssl.
12 13 14 |
# File 'lib/vend/http_client.rb', line 12 def verify_ssl @verify_ssl end |
Instance Method Details
#get_http_connection(host, port, use_ssl = true) ⇒ Object
sets up a http connection
28 29 30 31 32 33 34 35 36 |
# File 'lib/vend/http_client.rb', line 28 def get_http_connection(host, port, use_ssl = true) http = Net::HTTP.new(host, port) http.use_ssl = use_ssl http.verify_mode = verify_mode # Default read_tiemout is 60 seconds, some of the Vend API calls are # taking longer than this. http.read_timeout = READ_TIMEOUT http end |
#request(path, options = {}) ⇒ Object
Makes a request to the specified path within the Vend API E.g. request(‘foo’) will make a GET request to
http://storeurl.vendhq.com/api/foo
The HTTP method may be specified, by default it is GET.
An optional hash of arguments may be specified. Possible options include:
:method - The HTTP method
E.g. request('foo', method: :post) will perform a POST request for
http://storeurl.vendhq.com/api/foo
:url_params - The URL parameters for GET requests.
E.g. request('foo', url_params: {bar: "baz"}) will request
http://storeurl.vendhq.com/api/foo?=baz
:id - The ID required for performing actions on specific resources
(e.g. delete).
E.g. request('foos', method: :delete, id: 1) will request
DELETE http://storeurl.vendhq.com/api/foos/1
:body - The request body
E.g. For submitting a POST to http://storeurl.vendhq.com/api/foo
with the JSON data {"baz":"baloo"} we would call
request('foo', method: :post, body: '{\"baz\":\"baloo\"}'
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 |
# File 'lib/vend/http_client.rb', line 63 def request(path, = {}) = {method: :get, redirect_count: 0}.merge raise RedirectionLimitExceeded if [:redirect_count] > 10 url = if path.is_a?(URI) path else URI.parse(base_url + path) end ssl = (url.scheme == 'https') http = get_http_connection(url.host, url.port, ssl) # FIXME: extract method method = ("Net::HTTP::" + [:method].to_s.classify).constantize request = method.new(url.path + url_params_for([:url_params])) request.basic_auth username, password if username && password request['Authorization'] = "Bearer #{auth_token}" if auth_token request.body = [:body] if [:body] logger.debug url response = http.request(request) if response.is_a?(Net::HTTPRedirection) location = URI.parse(response['location']) logger.debug "Following redirect to %s" % [location] [:redirect_count] = [:redirect_count] + 1 request(location, ) else raise .new(UNAUTHORIZED_MESSAGE) if response.is_a?(Net::) raise HTTPError.new(response) unless response.is_a?(Net::HTTPSuccess) logger.debug response JSON.parse response.body unless response.body.nil? || response.body.empty? end end |
#verify_mode ⇒ Object
Returns the SSL verification mode, based upon the value of verify_ssl?
97 98 99 100 101 102 103 |
# File 'lib/vend/http_client.rb', line 97 def verify_mode if verify_ssl? OpenSSL::SSL::VERIFY_PEER else OpenSSL::SSL::VERIFY_NONE end end |