Module: SynapseClient

Defined in:
lib/synapse_client.rb,
lib/synapse_client/mfa.rb,
lib/synapse_client/util.rb,
lib/synapse_client/error.rb,
lib/synapse_client/order.rb,
lib/synapse_client/version.rb,
lib/synapse_client/customer.rb,
lib/synapse_client/mass_pay.rb,
lib/synapse_client/merchant.rb,
lib/synapse_client/api_resource.rb,
lib/synapse_client/bank_account.rb,
lib/synapse_client/refreshed_tokens.rb,
lib/synapse_client/security_question.rb,
lib/synapse_client/api_operations/list.rb,
lib/synapse_client/api_operations/response.rb

Defined Under Namespace

Modules: APIOperations, Util Classes: APIResource, BankAccount, Customer, Error, MFA, MassPay, Merchant, Order, RefreshedTokens, SecurityQuestion

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.client_idObject

Returns the value of attribute client_id.



23
24
25
# File 'lib/synapse_client.rb', line 23

def client_id
  @client_id
end

.client_secretObject

Returns the value of attribute client_secret.



23
24
25
# File 'lib/synapse_client.rb', line 23

def client_secret
  @client_secret
end

.devObject Also known as: dev?

Returns the value of attribute dev.



25
26
27
# File 'lib/synapse_client.rb', line 25

def dev
  @dev
end

.merchant_emailObject

Returns the value of attribute merchant_email.



24
25
26
# File 'lib/synapse_client.rb', line 24

def merchant_email
  @merchant_email
end

.merchant_oauth_keyObject

Returns the value of attribute merchant_oauth_key.



24
25
26
# File 'lib/synapse_client.rb', line 24

def merchant_oauth_key
  @merchant_oauth_key
end

.merchant_synapse_idObject

Returns the value of attribute merchant_synapse_id.



24
25
26
# File 'lib/synapse_client.rb', line 24

def merchant_synapse_id
  @merchant_synapse_id
end

Class Method Details

.api_url(url = '') ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/synapse_client.rb', line 35

def self.api_url(url='')
  base_api_url = "https://synapsepay.com"
  base_api_url = "https://sandbox.synapsepay.com" if dev?

  ret = base_api_url + ensure_trailing_slash(url)

  if dev?
    ret + "?is_dev=true"
  else
    ret
  end
end

.creds(client_id, client_secret) ⇒ Object



119
120
121
122
123
124
# File 'lib/synapse_client.rb', line 119

def self.creds(client_id, client_secret)
  {
    :client_id     => client_id,
    :client_secret => client_secret
  }
end

.ensure_trailing_slash(url = '') ⇒ Object



29
30
31
32
33
# File 'lib/synapse_client.rb', line 29

def self.ensure_trailing_slash(url='')
  return url if url.empty?
  return url if url[-1] == "/"
  "#{ url }/"
end

.execute_request(opts) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/synapse_client.rb', line 126

def self.execute_request(opts)
  if dev?
    puts "\n"
    puts "SynapseClient: About to send a request with the following opts:"
    puts opts
    puts "\n"
  end

  RestClient::Request.execute(opts)
end

.parse(response) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/synapse_client.rb', line 142

def self.parse(response)
  #begin
    body = JSON.parse(response.body)
  #rescue JSON::ParserError
  #   TODO
  #  raise general_api_error(response.code, response.body)
  #end

  APIOperations::Response.new(body, response.cookies)
end

.request(method, path, params = {}, headers = {}, client_id = nil, client_secret = nil, api_base_url = nil) ⇒ Object



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
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
# File 'lib/synapse_client.rb', line 48

def self.request(method, path, params={}, headers={}, client_id=nil, client_secret=nil, api_base_url=nil)

  #
    unless (client_id ||= @client_id) && (client_secret ||= @client_secret)
      # TODO - use a custom Error class here
      raise StandardError.new("You need to enter API credentials first.")
    end

    if client_id =~ /\s/ || client_secret =~ /\s/
      # TODO - use a custom Error class here
      raise StandardError.new("Your API credentials are invalid, as they contain whitespace.")
    end

  #
    url     = api_url(path)
    cookies = params.delete("cookies")

    case method.to_s.downcase.to_sym
    when :get, :head, :delete
      # Make params into GET parameters
      url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
      payload = nil
    else
      if path.include?("oauth2")
        payload = params.to_query
        headers[:content_type] = "application/x-www-form-urlencoded"
      else
        payload = creds(client_id, client_secret).update(params)

        # dealing with some naming inconsistencies in the api
        payload[:oauth_consumer_key] = payload.delete(:access_token) if payload[:access_token]
        payload[:oauth_consumer_key] = payload.delete("access_token") if payload["access_token"]
        payload[:access_token] = payload.delete("bank_account_token") if payload["bank_account_token"]
        payload[:access_token] = payload.delete(:bank_account_token) if payload[:bank_account_token]

        payload = payload.to_json
        headers[:content_type] = :json
      end
    end

  #
    request_opts = {
      :headers => headers,
      :method => method, :open_timeout => 30,
      :payload => payload,
      :url => url, :timeout => 80, :cookies => cookies
    }

  #
    begin

      response = execute_request(request_opts)

    rescue RestClient::Exception, Errno::ECONNREFUSED => e

      begin
       reason = JSON.parse(e.http_body)["reason"]
      rescue => p
        reason = e.http_body
      end

      return SynapseClient::APIOperations::Response.new({
        :success     => false,
        :status_code => e.http_code,
        :reason      => reason
      })
    end

    parse(response)
end

.uri_encode(params) ⇒ Object



137
138
139
140
# File 'lib/synapse_client.rb', line 137

def self.uri_encode(params)
  Util.flatten_params(params).
    map { |k,v| "#{k}=#{Util.url_encode(v)}" }.join('&')
end