Class: Ecertic::Client

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/ecertic/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#documents, #otps, #tokens

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(options = {})
  defaults = Ecertic::Default.options
  Ecertic::Default.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || defaults[key])
  end
  @connection = connection || self.class.default_connection
  @services = {}
end

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



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

def apikey
  @apikey
end

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#secretObject

Returns the value of attribute secret.



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

def secret
  @secret
end

#user_agentObject

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_connectionObject



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_urlObject



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, options = {})
  request(method, path, data, options)
end

#get(path, options = {}) ⇒ Object



33
34
35
# File 'lib/ecertic/client.rb', line 33

def get(path, options = {})
  execute :get, path, nil, options.to_h
end

#post(path, data = nil, options = {}) ⇒ Object



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

def post(path, data = nil, options = {})
  execute :post, path, data, options
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, options = {})
  request_options = request_options(method, options, data)
  uri = "#{base_url}#{path}"

  begin
    request_start = Time.now
    log_request(method, path, request_options[:body], request_options[:headers])
    response = connection.run_request(method, uri, request_options[:body], request_options[: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