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 URL and credentials are correct and try again."
Instance Attribute Summary collapse
-
#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) ⇒ 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.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/vend/http_client.rb', line 14 def initialize( = {}) @base_url = [:base_url] @username = [:username] @password = [:password] @verify_ssl = if .has_key?(:verify_ssl) [:verify_ssl] else true end end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/vend/http_client.rb', line 11 def base_url @base_url end |
#password ⇒ Object
Returns the value of attribute password.
11 12 13 |
# File 'lib/vend/http_client.rb', line 11 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
11 12 13 |
# File 'lib/vend/http_client.rb', line 11 def username @username end |
#verify_ssl ⇒ Object Also known as: verify_ssl?
Returns the value of attribute verify_ssl.
11 12 13 |
# File 'lib/vend/http_client.rb', line 11 def verify_ssl @verify_ssl end |
Instance Method Details
#get_http_connection(host, port) ⇒ Object
sets up a http connection
26 27 28 29 30 31 |
# File 'lib/vend/http_client.rb', line 26 def get_http_connection(host, port) http = Net::HTTP.new(host, port) http.use_ssl = true http.verify_mode = verify_mode 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?bar=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\"}'
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/vend/http_client.rb', line 58 def request(path, = {}) = {:method => :get}.merge url = URI.parse(base_url + path) http = get_http_connection(url.host, url.port) # 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 request.body = [:body] if [:body] logger.debug url response = http.request(request) raise Unauthorized.new(UNAUTHORIZED_MESSAGE) if response.kind_of?(Net::HTTPUnauthorized) raise HTTPError.new(response) unless response.kind_of?(Net::HTTPSuccess) logger.debug response JSON.parse response.body unless response.body.nil? or response.body.empty? end |
#verify_mode ⇒ Object
Returns the SSL verification mode, based upon the value of verify_ssl?
78 79 80 81 82 83 84 |
# File 'lib/vend/http_client.rb', line 78 def verify_mode if verify_ssl? OpenSSL::SSL::VERIFY_PEER else OpenSSL::SSL::VERIFY_NONE end end |