class Instamojo::Client

Constants

URL

Attributes

app_id[R]
authorized[R]
connection_options[R]
request[R]
response[R]

Public Class Methods

define_http_verb(http_verb) click to toggle source
# File lib/client/client.rb, line 39
def self.define_http_verb(http_verb)
  define_method http_verb do |*args|
    request = args[0] || "/"
    params = args[1] || {}

    @request = request
    sanitize_request
    method = @conn.method(http_verb)
    @response = method.call(@request, params)
    sanitize_response
  end
end
new(api, endpoint) click to toggle source
# File lib/client/client.rb, line 17
def initialize(api, endpoint)
  @endpoint = endpoint || URL
  @conn = Faraday.new(@endpoint, &connection_options)

  #TODO: To abstract in /errors.rb
  raise "Supply API with api_key before generating client" unless api.api_key

  @api_key = api.api_key
  @auth_token = api.auth_token
  add_header "X-Api-Key", @api_key
  if @auth_token
    add_header "X-Auth-Token", @auth_token
    get 'debug' # dummy request to verify supplied auth_token
  else
    @authorized = "Not authorized"
  end
end

Public Instance Methods

authenticate(username = nil, password = nil, options = {}, &block) click to toggle source

POST /auth/ Authenticate, generate token and add header

# File lib/client/client.rb, line 60
def authenticate(username = nil, password = nil, options = {}, &block)
  if username.is_a?(Hash) or password.is_a?(Hash)
    options = username.is_a?(Hash) ? username : password
  end
  options["username"] = username if username and !username.is_a?Hash
  options["password"] = password if password and !password.is_a?Hash

  options = set_options(options, &block)

  #TODO: Raise error if doesn't find username and password key in options
  @response = post('auth', options)
  if @response.has_key?("success") and @response['success']
    add_header("X-Auth-Token", @response['token'])
  end
  @response
end
create_refund(options = {}, &block) click to toggle source

POST /refunds

# File lib/client/client.rb, line 173
def create_refund(options = {}, &block)
  options = set_options(options, &block)
  post('refunds', options)
  @response.success? ? Instamojo::Refund.new(@response.body[:refund], self) : @response
end
get_connection_object() click to toggle source
# File lib/client/client.rb, line 35
def get_connection_object
  @conn
end
logout() click to toggle source

DELETE /auth/:token - Delete auth token

# File lib/client/client.rb, line 180
def logout
  auth_token = get_connection_object.headers['X-Auth-Token']
  raise "Can't find any authorization token to logout." unless auth_token
  @response = delete("/auth/#{auth_token}")
  if @response.has_key?("success") and @response['success']
    get_connection_object.headers.delete("X-Auth-Token")
  end
  @response
end
payment_detail(payment_id) click to toggle source

GET /payments/:payment_id

# File lib/client/client.rb, line 135
def payment_detail(payment_id)
  payment_id = payment_id.payment_id if payment_id.instance_of? Instamojo::Payment
  get("payments/#{payment_id}")
  @response.success? ? Instamojo::Payment.new(@response.body[:payment], self) : @response
end
payment_request(options, &block) click to toggle source

POST /payment-requests

# File lib/client/client.rb, line 142
def payment_request(options, &block)
  set_options(options, &block)
  post('payment-requests', options)
  @response.success? ? Instamojo::PaymentRequest.new(@response.body[:payment_request], self) : @response
end
payment_request_status(payment_request_id) click to toggle source
# File lib/client/client.rb, line 154
def payment_request_status(payment_request_id)
  payment_request_id = payment_request_id.id if payment_request_id.instance_of? Instamojo::PaymentRequest
  get("payment-requests/#{payment_request_id}") if payment_request_id
  @response.success? ? Instamojo::PaymentRequest.new(@response.body[:payment_request], self) : @response
end
payment_requests_list(opts = {}) click to toggle source

GET /payment-requests

# File lib/client/client.rb, line 149
def payment_requests_list(opts = {})
  get('payment-requests', opts)
  @response.success? ? @response.body[:payment_requests].map { |payment_request| Instamojo::PaymentRequest.new payment_request, self } : @response
end
payments_list(opts = {}) click to toggle source

GET /payments

# File lib/client/client.rb, line 129
def payments_list(opts = {})
  get('payments', opts)
  @response.success? ? @response.body[:payments].map { |payment| Instamojo::Payment.new payment, self } : @response
end
refund_detail(refund_id) click to toggle source

GET /refunds/:refund_id

# File lib/client/client.rb, line 167
def refund_detail(refund_id)
  get("refunds/#{refund_id}")
  @response.success? ? Instamojo::Refund.new(@response.body[:refund], self) : @response
end
refunds_list(opts = {}) click to toggle source

GET /refunds

# File lib/client/client.rb, line 161
def refunds_list(opts = {})
  get('refunds', opts)
  @response.success? ? @response.body[:refunds].map { |refund| Instamojo::Refund.new refund, self } : @response
end
to_s() click to toggle source
# File lib/client/client.rb, line 190
def to_s
  sprintf("Instamojo Client(URL: %s, Authorized: %s)", @endpoint, @authorized)
end
upload_file(filepath) click to toggle source

POST 'filepicker.io/api/store/S3'

# File lib/client/client.rb, line 118
def upload_file(filepath)
  if filepath && (file=File.open(File.expand_path(filepath), 'rb'))
    if (url=get_file_upload_url).is_a? String
      resource = RestClient::Resource.new(url)
      resource.post fileUpload: file
    end
  end
end

Private Instance Methods

add_header(key, value) click to toggle source
# File lib/client/client.rb, line 216
def add_header(key, value)
  previous_headers = get_connection_object.headers
  get_connection_object.headers = previous_headers.merge({key => value})
end
get_file_upload_url() click to toggle source

GET /links/get_file_upload_url

# File lib/client/client.rb, line 222
def get_file_upload_url
  get('links/get_file_upload_url')
  @response.success? ? @response.body[:upload_url] : @response
end
sanitize_request() click to toggle source
# File lib/client/client.rb, line 195
def sanitize_request
  @request.concat('/') unless request.end_with? "/"
  @request[0] = '' if request.start_with? "/"
end
sanitize_response() click to toggle source
# File lib/client/client.rb, line 200
def sanitize_response
  @response = Instamojo::Response.new(@response)
  @authorized = @response.success?
  @response
end
set_options(options, &block) click to toggle source
# File lib/client/client.rb, line 206
def set_options(options, &block)
  if block_given?
    block_params = OpenStruct.new
    block.call(block_params)
    options = options.merge(block_params.marshal_dump)
  end

  options.symbolize_keys!
end