Class: Fulfillment::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fulfillment/client.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
# File 'lib/fulfillment/client.rb', line 11

def initialize(options = {})
  client_options = HashWithIndifferentAccess.new(options)
  @api_key = client_options[:api_key].nil? ? (raise ArgumentError.new(":api_key is a required argument")) : client_options[:api_key]
  @host = client_options[:host].nil? ? (raise ArgumentError.new(":host is a required argument")) : client_options[:host]
  @scheme = client_options[:scheme] || "https"
  @base_uri = @scheme + "://" + @host
  @verbose = client_options[:verbose] || false
  @logger = client_options[:logger] || nil
  @timeout = client_options[:timeout] || DEFAULT_TIMEOUT
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



7
8
9
# File 'lib/fulfillment/client.rb', line 7

def api_key
  @api_key
end

#base_uriObject (readonly)

Returns the value of attribute base_uri.



7
8
9
# File 'lib/fulfillment/client.rb', line 7

def base_uri
  @base_uri
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/fulfillment/client.rb', line 7

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/fulfillment/client.rb', line 6

def logger
  @logger
end

#schemeObject (readonly)

Returns the value of attribute scheme.



7
8
9
# File 'lib/fulfillment/client.rb', line 7

def scheme
  @scheme
end

#verboseObject

Returns the value of attribute verbose.



6
7
8
# File 'lib/fulfillment/client.rb', line 6

def verbose
  @verbose
end

Instance Method Details

#add_query_parameter(curl, key, value) ⇒ Object



41
42
43
44
# File 'lib/fulfillment/client.rb', line 41

def add_query_parameter(curl, key, value)
  current_url = curl.url
  curl.url = current_url + (current_url.match(/\?/) ? "&" : "?") + "#{CGI::escape key.to_s}=#{CGI::escape value.to_s}"
end

#build_auth_url(resource_path) ⇒ Object



37
38
39
# File 'lib/fulfillment/client.rb', line 37

def build_auth_url(resource_path)
  @base_uri + resource_path
end

#configure_http(http) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fulfillment/client.rb', line 22

def configure_http(http)
  http.headers["X-API-KEY"] = @api_key
  http.headers["Accept"] = Fulfillment::API_VERSION
  http.headers["Content-Type"] = "application/json"
  if scheme == "https"
    http.use_ssl = Curl::CURL_USESSL_ALL
    http.ssl_verify_peer = false
  end
  http.verbose = @verbose
  unless @logger.nil?
    http.on_debug { |type,data| @logger.info "Fulfillment Client #{data}" }
  end
  http.timeout = @timeout
end

#set_request_page(curl, page_num) ⇒ Object



46
47
48
# File 'lib/fulfillment/client.rb', line 46

def set_request_page(curl, page_num)
  add_query_parameter(curl, "page", page_num)
end