class Instamojo::Client
Constants
- URL
Attributes
app_id[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
archive_link(slug)
click to toggle source
DELETE /links/:slug
# File lib/client/client.rb, line 112 def archive_link(slug) delete("links/#{slug}") end
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_link(options = {}, &block)
click to toggle source
POST /links
# File lib/client/client.rb, line 91 def create_link(options = {}, &block) options = set_options(options, &block) options[:file_upload_json] = options[:file_upload] && upload_file(options.delete(:file_upload)) options[:cover_image_json] = options[:cover_image] && upload_file(options.delete(:cover_image)) post('links', options) @response.success? ? Instamojo::Link.new(@response.body[:link], self) : @response end
create_refund(options = {}, &block)
click to toggle source
POST /refunds
# File lib/client/client.rb, line 172 def create_refund(options = {}, &block) options = set_options(options, &block) post('refunds', options) @response.success? ? Instamojo::Refund.new(@response.body[:refund], self) : @response end
edit_link(link = nil, options = {}) { |link| ... }
click to toggle source
PATCH /links/:slug
# File lib/client/client.rb, line 100 def edit_link(link = nil, options = {}, &block) if link && link.is_a?(Instamojo::Link) yield(link) if block_given? else options = set_options(options, &block) link = Instamojo::Link.new(options, self) end patch("links/#{link.slug}", link.to_h) @response.success? ? Instamojo::Link.new(@response.body[:link], self) : @response end
get_connection_object()
click to toggle source
# File lib/client/client.rb, line 35 def get_connection_object @conn end
link_detail(slug)
click to toggle source
GET /links/:slug
# File lib/client/client.rb, line 84 def link_detail(slug) slug = slug.slug if slug.instance_of? Instamojo::Link get("links/#{slug}") @response.success? ? Instamojo::Link.new(@response.body[:link], self) : @response end
links_list()
click to toggle source
GET /links
# File lib/client/client.rb, line 78 def links_list get('links') @response.success? ? @response.body[:links].map { |link| Instamojo::Link.new link, self } : @response end
logout()
click to toggle source
DELETE /auth/:token - Delete auth token
# File lib/client/client.rb, line 179 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 134 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 141 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 153 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()
click to toggle source
GET /payment-requests
# File lib/client/client.rb, line 148 def payment_requests_list get('payment-requests') @response.success? ? @response.body[:payment_requests].map { |payment_request| Instamojo::PaymentRequest.new payment_request, self } : @response end
payments_list()
click to toggle source
GET /payments
# File lib/client/client.rb, line 128 def payments_list get('payments') @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 166 def refund_detail(refund_id) get("refunds/#{refund_id}") @response.success? ? Instamojo::Refund.new(@response.body[:refund], self) : @response end
refunds_list()
click to toggle source
GET /refunds
# File lib/client/client.rb, line 160 def refunds_list get('refunds') @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 189 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 117 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 215 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 221 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 194 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 199 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 205 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