Class: Ecertic::Client
Instance Attribute Summary collapse
-
#apikey ⇒ Object
Returns the value of attribute apikey.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Class Method Summary collapse
Instance Method Summary collapse
- #base_url ⇒ Object
- #execute(method, path, data = nil, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #post(path, data = nil, options = {}) ⇒ Object
- #request(method, path, data = nil, options = {}) ⇒ Object
Methods included from API
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 |
# File 'lib/ecertic/client.rb', line 8 def initialize( = {}) defaults = Ecertic::Default. Ecertic::Default.keys.each do |key| instance_variable_set(:"@#{key}", [key] || defaults[key]) end @connection = connection || self.class.default_connection @services = {} end |
Instance Attribute Details
#apikey ⇒ Object
Returns the value of attribute apikey.
6 7 8 |
# File 'lib/ecertic/client.rb', line 6 def apikey @apikey end |
#connection ⇒ Object
Returns the value of attribute connection.
6 7 8 |
# File 'lib/ecertic/client.rb', line 6 def connection @connection end |
#secret ⇒ Object
Returns the value of attribute secret.
6 7 8 |
# File 'lib/ecertic/client.rb', line 6 def secret @secret end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
6 7 8 |
# File 'lib/ecertic/client.rb', line 6 def user_agent @user_agent end |
Class Method Details
.default_connection ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ecertic/client.rb', line 17 def self.default_connection Thread.current[:ecertic_client_default_connection] ||= begin Faraday.new do |builder| builder.use Faraday::Response::RaiseError builder.response :json, content_type: /\bjson$/, preserve_raw: true, parser_options: { symbolize_names: true } builder.adapter :net_http_persistent end end end |
Instance Method Details
#base_url ⇒ Object
29 30 31 |
# File 'lib/ecertic/client.rb', line 29 def base_url @base_url.chomp("/") end |
#execute(method, path, data = nil, options = {}) ⇒ Object
41 42 43 |
# File 'lib/ecertic/client.rb', line 41 def execute(method, path, data = nil, = {}) request(method, path, data, ) end |
#get(path, options = {}) ⇒ Object
33 34 35 |
# File 'lib/ecertic/client.rb', line 33 def get(path, = {}) execute :get, path, nil, .to_h end |
#post(path, data = nil, options = {}) ⇒ Object
37 38 39 |
# File 'lib/ecertic/client.rb', line 37 def post(path, data = nil, = {}) execute :post, path, data, end |
#request(method, path, data = nil, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ecertic/client.rb', line 45 def request(method, path, data = nil, = {}) = (method, , data) uri = "#{base_url}#{path}" begin request_start = Time.now log_request(method, path, [:body], [:headers]) response = connection.run_request(method, uri, [:body], [:headers]) do |req| # req.options.open_timeout = Ecertic.open_timeout # req.options.timeout = Ecertic.read_timeout end log_response(request_start, method, path, response.status, response.body) response rescue StandardError => e log_response_error(request_start, e, method, path) case e when Faraday::ClientError if e.response handle_error_response(e.response) else handle_network_error(e) end else raise end end Ecertic::Response.from_faraday_response(response) end |