Class: MultiSafePay::Client
- Inherits:
-
Object
- Object
- MultiSafePay::Client
- Defined in:
- lib/multisafepay/client.rb
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- LIVE_API_ENDPOINT =
'https://api.multisafepay.com/v1/json'.freeze
- TEST_API_ENDPOINT =
'https://testapi.multisafepay.com/v1/json'.freeze
- MODE_TEST =
:test
- MODE_LIVE =
:live
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#environment ⇒ Object
Returns the value of attribute environment.
Class Method Summary collapse
- .configure {|configuration| ... } ⇒ Object
- .instance ⇒ MultiSafePay::Client
- .with_api_key(api_key, environment = MODE_LIVE) ⇒ Object
Instance Method Summary collapse
- #add_version_string(version_string) ⇒ Object
- #api_endpoint ⇒ Object
- #api_endpoint=(api_endpoint) ⇒ Object
- #delete(api_method, id = nil, http_body = {}, query = {}) ⇒ Object
- #get(api_method, id = nil, query = {}) ⇒ Object
-
#initialize(api_key = nil, environment = MODE_LIVE) ⇒ Client
constructor
A new instance of Client.
- #live? ⇒ Boolean
- #patch(api_method, id = nil, http_body = {}, query = {}) ⇒ Object
- #perform_http_call(http_method, api_method, id = nil, http_body = {}, query = {}) ⇒ Object
- #post(api_method, id = nil, http_body = {}, query = {}) ⇒ Object
- #test? ⇒ Boolean
Constructor Details
#initialize(api_key = nil, environment = MODE_LIVE) ⇒ Client
Returns a new instance of Client.
54 55 56 57 58 59 60 61 62 |
# File 'lib/multisafepay/client.rb', line 54 def initialize(api_key = nil, environment = MODE_LIVE) @api_key = api_key @environment = environment @version_strings = [] add_version_string 'MultiSafePay/' << 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/multisafepay/client.rb', line 10 def configuration @configuration end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
51 52 53 |
# File 'lib/multisafepay/client.rb', line 51 def api_key @api_key end |
#environment ⇒ Object
Returns the value of attribute environment.
51 52 53 |
# File 'lib/multisafepay/client.rb', line 51 def environment @environment end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
25 26 27 28 |
# File 'lib/multisafepay/client.rb', line 25 def self.configure self.configuration ||= Configuration.new yield(configuration) end |
.instance ⇒ MultiSafePay::Client
31 32 33 34 35 36 |
# File 'lib/multisafepay/client.rb', line 31 def self.instance Thread.current['MULTISAFEPAY_CLIENT'] ||= begin self.configuration ||= Configuration.new new(configuration.api_key, configuration.environment) end end |
.with_api_key(api_key, environment = MODE_LIVE) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/multisafepay/client.rb', line 38 def self.with_api_key(api_key, environment = MODE_LIVE) Thread.current['MULTISAFEPAY_API_KEY'] = instance.api_key Thread.current['MULTISAFEPAY_ENVIRONMENT'] = instance.environment instance.api_key = api_key instance.environment = environment yield ensure instance.api_key = Thread.current['MULTISAFEPAY_API_KEY'] instance.environment = Thread.current['MULTISAFEPAY_ENVIRONMENT'] Thread.current['MULTISAFEPAY_API_KEY'] = nil Thread.current['MULTISAFEPAY_ENVIRONMENT'] = nil end |
Instance Method Details
#add_version_string(version_string) ⇒ Object
80 81 82 |
# File 'lib/multisafepay/client.rb', line 80 def add_version_string(version_string) @version_strings << version_string.gsub(/\s+/, '-') end |
#api_endpoint ⇒ Object
72 73 74 |
# File 'lib/multisafepay/client.rb', line 72 def api_endpoint @api_endpoint || (live? ? LIVE_API_ENDPOINT : TEST_API_ENDPOINT) end |
#api_endpoint=(api_endpoint) ⇒ Object
76 77 78 |
# File 'lib/multisafepay/client.rb', line 76 def api_endpoint=(api_endpoint) @api_endpoint = api_endpoint.chomp('/') end |
#delete(api_method, id = nil, http_body = {}, query = {}) ⇒ Object
96 97 98 |
# File 'lib/multisafepay/client.rb', line 96 def delete(api_method, id = nil, http_body = {}, query = {}) perform_http_call('DELETE', api_method, id, http_body, query) end |
#get(api_method, id = nil, query = {}) ⇒ Object
84 85 86 |
# File 'lib/multisafepay/client.rb', line 84 def get(api_method, id = nil, query = {}) perform_http_call('GET', api_method, id, {}, query) end |
#live? ⇒ Boolean
64 65 66 |
# File 'lib/multisafepay/client.rb', line 64 def live? @environment == MODE_LIVE end |
#patch(api_method, id = nil, http_body = {}, query = {}) ⇒ Object
92 93 94 |
# File 'lib/multisafepay/client.rb', line 92 def patch(api_method, id = nil, http_body = {}, query = {}) perform_http_call('PATCH', api_method, id, http_body, query) end |
#perform_http_call(http_method, api_method, id = nil, http_body = {}, query = {}) ⇒ Object
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/multisafepay/client.rb', line 100 def perform_http_call(http_method, api_method, id = nil, http_body = {}, query = {}) path = if api_method.start_with?(self.api_endpoint) URI.parse(api_method).path else "#{URI.parse(self.api_endpoint).path}/#{api_method}/#{id}".chomp('/') end environment = http_body.delete(:environment) || query.delete(:environment) || @environment api_endpoint = http_body.delete(:api_endpoint) || query.delete(:api_endpoint) || (environment == MODE_LIVE ? LIVE_API_ENDPOINT : TEST_API_ENDPOINT) api_key = http_body.delete(:api_key) || query.delete(:api_key) || @api_key unless query.empty? path += "?#{build_nested_query(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 if self.class.configuration.debug puts " -> api_token: #{api_key}" puts " -> api_endpoint: #{api_endpoint}" puts " -> path: #{path}" puts " -> http_body: #{http_body}" end 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 = http_body.to_json when 'PATCH' http_body.delete_if { |_k, v| v.nil? } request = Net::HTTP::Patch.new(path) request.body = http_body.to_json when 'DELETE' http_body.delete_if { |_k, v| v.nil? } request = Net::HTTP::Delete.new(path) request.body = http_body.to_json else raise MultiSafePay::Exception, "Invalid HTTP Method: #{http_method}" end request['Accept'] = 'application/json' request['Content-Type'] = 'application/json' request['User-Agent'] = @version_strings.join(' ') request['api-key'] = api_key begin response = client.request(request) rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e raise MultiSafePay::Exception, e. end if self.class.configuration.debug puts " => response code: #{response.code}" puts " => response body: #{response.body}" end http_code = response.code.to_i case http_code when 200, 201 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 = MultiSafePay::RequestError.new(json, response) raise exception end end |
#post(api_method, id = nil, http_body = {}, query = {}) ⇒ Object
88 89 90 |
# File 'lib/multisafepay/client.rb', line 88 def post(api_method, id = nil, http_body = {}, query = {}) perform_http_call('POST', api_method, id, http_body, query) end |
#test? ⇒ Boolean
68 69 70 |
# File 'lib/multisafepay/client.rb', line 68 def test? !live? end |