Class: Mollie::Client
- Inherits:
-
Object
- Object
- Mollie::Client
- Defined in:
- lib/mollie/client.rb
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- API_ENDPOINT =
'https://api.mollie.com'.freeze
- API_VERSION =
'v2'.freeze
- MODE_TEST =
'test'.freeze
- MODE_LIVE =
'live'.freeze
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Class Method Summary collapse
- .configure {|configuration| ... } ⇒ Object
- .instance ⇒ Mollie::Client
- .with_api_key(api_key) ⇒ Object
Instance Method Summary collapse
- #add_version_string(version_string) ⇒ Object
-
#initialize(api_key = nil) ⇒ Client
constructor
A new instance of Client.
- #perform_http_call(http_method, api_method, id = nil, http_body = {}, query = {}) ⇒ Object
Constructor Details
#initialize(api_key = nil) ⇒ Client
Returns a new instance of Client.
48 49 50 51 52 53 54 55 56 |
# File 'lib/mollie/client.rb', line 48 def initialize(api_key = nil) @api_endpoint = API_ENDPOINT @api_key = api_key @version_strings = [] add_version_string 'Mollie/' << VERSION add_version_string 'Ruby/' << RUBY_VERSION add_version_string OpenSSL::OPENSSL_VERSION.split(' ').slice(0, 2).join '/' end |
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
10 11 12 |
# File 'lib/mollie/client.rb', line 10 def configuration @configuration end |
Instance Attribute Details
#api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
45 46 47 |
# File 'lib/mollie/client.rb', line 45 def api_endpoint @api_endpoint end |
#api_key ⇒ Object
Returns the value of attribute api_key.
45 46 47 |
# File 'lib/mollie/client.rb', line 45 def api_key @api_key end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
23 24 25 26 |
# File 'lib/mollie/client.rb', line 23 def self.configure self.configuration ||= Configuration.new yield(configuration) end |
.instance ⇒ Mollie::Client
29 30 31 32 33 34 |
# File 'lib/mollie/client.rb', line 29 def self.instance Thread.current['MOLLIE_CLIENT'] ||= begin self.configuration ||= Configuration.new new(configuration.api_key) end end |
.with_api_key(api_key) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/mollie/client.rb', line 36 def self.with_api_key(api_key) Thread.current['MOLLIE_API_KEY'] = instance.api_key instance.api_key = api_key yield ensure instance.api_key = Thread.current['MOLLIE_API_KEY'] Thread.current['MOLLIE_API_KEY'] = nil end |
Instance Method Details
#add_version_string(version_string) ⇒ Object
62 63 64 |
# File 'lib/mollie/client.rb', line 62 def add_version_string(version_string) @version_strings << version_string.gsub(/\s+/, '-') end |
#perform_http_call(http_method, api_method, id = nil, http_body = {}, query = {}) ⇒ Object
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mollie/client.rb', line 66 def perform_http_call(http_method, api_method, id = nil, http_body = {}, query = {}) path = if api_method.start_with?(API_ENDPOINT) URI.parse(api_method).path else "/#{API_VERSION}/#{api_method}/#{id}".chomp('/') end api_key = http_body.delete(:api_key) || query.delete(:api_key) || @api_key api_endpoint = http_body.delete(:api_endpoint) || query.delete(:api_endpoint) || @api_endpoint idempotency_key = http_body.delete(:idempotency_key) || query.delete(:idempotency_key) unless query.empty? camelized_query = Util.camelize_keys(query) path += "?#{build_nested_query(camelized_query)}" end uri = URI.parse(api_endpoint) client = Net::HTTP.new(uri.host, uri.port) client.use_ssl = true client.verify_mode = OpenSSL::SSL::VERIFY_PEER client.ca_file = (File. '../cacert.pem', File.dirname(__FILE__)) client.read_timeout = self.class.configuration.read_timeout client.open_timeout = self.class.configuration.open_timeout case http_method when 'GET' request = Net::HTTP::Get.new(path) when 'POST' http_body.delete_if { |_k, v| v.nil? } request = Net::HTTP::Post.new(path) request.body = Util.camelize_keys(http_body).to_json when 'PATCH' http_body.delete_if { |_k, v| v.nil? } request = Net::HTTP::Patch.new(path) request.body = Util.camelize_keys(http_body).to_json when 'DELETE' http_body.delete_if { |_k, v| v.nil? } request = Net::HTTP::Delete.new(path) request.body = Util.camelize_keys(http_body).to_json else raise Mollie::Exception, "Invalid HTTP Method: #{http_method}" end request['Accept'] = 'application/json' request['Content-Type'] = 'application/json' request['Authorization'] = "Bearer #{api_key}" request['User-Agent'] = @version_strings.join(' ') if http_method == 'POST' && idempotency_key request['Idempotency-Key'] = idempotency_key end begin response = client.request(request) rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e raise Mollie::Exception, e. end http_code = response.code.to_i case http_code when 200, 201 Util.nested_underscore_keys(JSON.parse(response.body)) when 204 {} # No Content when 404 json = JSON.parse(response.body) exception = ResourceNotFoundError.new(json, response) raise exception else json = JSON.parse(response.body) exception = Mollie::RequestError.new(json, response) raise exception end end |