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 credentials are correct and try again."
READ_TIMEOUT =

Read timeout in seconds

240

Instance Attribute Summary collapse

Attributes included from Logable

#logger

Instance Method Summary collapse

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

Instance Attribute Details

#auth_tokenObject

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_urlObject

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

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


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, options = {})
  options = {method: :get, redirect_count: 0}.merge options
  raise RedirectionLimitExceeded if options[: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::" + options[:method].to_s.classify).constantize
  request = method.new(url.path + url_params_for(options[:url_params]))
  request.basic_auth username, password if username && password
  request['Authorization'] = "Bearer #{auth_token}" if auth_token

  request.body = options[:body] if options[: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]
    options[:redirect_count] = options[:redirect_count] + 1
    request(location, options)
  else
    raise Unauthorized.new(UNAUTHORIZED_MESSAGE) if response.is_a?(Net::HTTPUnauthorized)
    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_modeObject

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