Class: SpeedLightning::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, CheckoutLinkMethods
Defined in:
lib/speed_lightning/client.rb

Constant Summary collapse

API_URL =
"https://api.tryspeed.com/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CheckoutLinkMethods

#create_checkout_link, #retrieve_checkout_link

Methods included from Retryable

#with_retry

Constructor Details

#initialize(secret_key:) ⇒ Client

Returns a new instance of Client.



12
13
14
# File 'lib/speed_lightning/client.rb', line 12

def initialize(secret_key:)
  @secret_key = secret_key
end

Instance Attribute Details

#api_secretObject

Returns the value of attribute api_secret.



9
10
11
# File 'lib/speed_lightning/client.rb', line 9

def api_secret
  @api_secret
end

Instance Method Details

#make_request(endpoint, request_type, body_hash = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/speed_lightning/client.rb', line 16

def make_request(endpoint, request_type, body_hash = nil)
  headers = {
    "Accept" => "application/json",
    "Speed-Version" => "2022-04-15",
    "Authorization" => "Basic " + Base64.strict_encode64(@secret_key + ":")
  }
  headers["Content-Type"] = "application/json" if body_hash
  options = { headers: headers }
  options[:body] = body_hash.to_json if body_hash
  full_url = API_URL + endpoint
  response =
    case request_type
    when :get
      self.class.get(full_url, options)
    when :post
      self.class.post(full_url, options)
    end
  
    parsed_response = JSON.parse(response.body)
  
  if parsed_response['errors'] && !parsed_response['errors'].empty?
    error_messages = parsed_response['errors'].map { |error| error['message'] }.compact
    concatenated_error_message = error_messages.join(', ')
    raise SpeedLightning::Error, concatenated_error_message
  end         
  
  parsed_response
end