Class: Fulfillment::Client
- Inherits:
-
Object
- Object
- Fulfillment::Client
- Defined in:
- lib/fulfillment/client.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
10
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #add_query_parameter(curl, key, value) ⇒ Object
- #build_auth_url(resource_path) ⇒ Object
- #configure_http(http) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #set_request_page(curl, page_num) ⇒ Object
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( = {}) = HashWithIndifferentAccess.new() @api_key = [:api_key].nil? ? (raise ArgumentError.new(":api_key is a required argument")) : [:api_key] @host = [:host].nil? ? (raise ArgumentError.new(":host is a required argument")) : [:host] @scheme = [:scheme] || "https" @base_uri = @scheme + "://" + @host @verbose = [:verbose] || false @logger = [:logger] || nil @timeout = [:timeout] || DEFAULT_TIMEOUT end |
Instance Attribute Details
#api_key ⇒ Object (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_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
7 8 9 |
# File 'lib/fulfillment/client.rb', line 7 def base_uri @base_uri end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/fulfillment/client.rb', line 7 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
6 7 8 |
# File 'lib/fulfillment/client.rb', line 6 def logger @logger end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
7 8 9 |
# File 'lib/fulfillment/client.rb', line 7 def scheme @scheme end |
#verbose ⇒ Object
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 |