module SynapseClient

Constants

VERSION

Attributes

client_id[RW]
client_secret[RW]
dev[RW]
dev?[RW]
merchant_email[RW]
merchant_oauth_key[RW]
merchant_synapse_id[RW]

Public Class Methods

api_url(url='') click to toggle source
# File lib/synapse_client.rb, line 36
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) click to toggle source
# File lib/synapse_client.rb, line 120
def self.creds(client_id, client_secret)
  {
    :client_id     => client_id,
    :client_secret => client_secret
  }
end
ensure_trailing_slash(url='') click to toggle source
# File lib/synapse_client.rb, line 30
def self.ensure_trailing_slash(url='')
  return url if url.empty?
  return url if url[-1] == "/"
  "#{ url }/"
end
execute_request(opts) click to toggle source
# File lib/synapse_client.rb, line 127
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) click to toggle source
# File lib/synapse_client.rb, line 143
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) click to toggle source
# File lib/synapse_client.rb, line 49
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) click to toggle source
# File lib/synapse_client.rb, line 138
def self.uri_encode(params)
  Util.flatten_params(params).
    map { |k,v| "#{k}=#{Util.url_encode(v)}" }.join('&')
end