Class: Vend::HttpClient

Inherits:
Object
  • Object
show all
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

Attributes included from Logable

#logger

Instance Method Summary collapse

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(options = {})
  @base_url = options[:base_url]
  @username = options[:username]
  @password = options[:password]
  @verify_ssl = if options.has_key?(:verify_ssl)
                  options[:verify_ssl]
                else
                  true
                end
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



11
12
13
# File 'lib/vend/http_client.rb', line 11

def base_url
  @base_url
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/vend/http_client.rb', line 11

def password
  @password
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/vend/http_client.rb', line 11

def username
  @username
end

#verify_sslObject 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\"}'

Raises:



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, options = {})
  options = {:method => :get}.merge options
  url = URI.parse(base_url + path) 
  http = get_http_connection(url.host, url.port)

  # FIXME extract method
  method = ("Net::HTTP::" + options[:method].to_s.classify).constantize
  request = method.new(url.path + url_params_for(options[:url_params]))
  request.basic_auth username, password

  request.body = options[:body] if options[: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_modeObject

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