Class: Instamojo::Client
- Inherits:
-
Object
- Object
- Instamojo::Client
- Defined in:
- lib/client/client.rb
Constant Summary collapse
- URL =
Instamojo::HOST + "/api/" + Instamojo::API_VERSION
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#authorized ⇒ Object
readonly
Returns the value of attribute authorized.
-
#connection_options ⇒ Object
readonly
Returns the value of attribute connection_options.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
-
#archive_link(slug) ⇒ Object
DELETE /links/:slug.
-
#authenticate(username = nil, password = nil, options = {}, &block) ⇒ Object
POST /auth/ Authenticate, generate token and add header.
-
#create_link(options = {}, &block) ⇒ Object
POST /links.
-
#create_refund(options = {}, &block) ⇒ Object
POST /refunds.
-
#edit_link(link = nil, options = {}, &block) ⇒ Object
PATCH /links/:slug.
- #get_connection_object ⇒ Object
-
#initialize(api, endpoint) ⇒ Client
constructor
A new instance of Client.
-
#link_detail(slug) ⇒ Object
GET /links/:slug.
-
#links_list ⇒ Object
GET /links.
-
#logout ⇒ Object
DELETE /auth/:token - Delete auth token.
-
#payment_detail(payment_id) ⇒ Object
GET /payments/:payment_id.
-
#payment_request(options, &block) ⇒ Object
POST /payment-requests.
- #payment_request_status(payment_request_id) ⇒ Object
-
#payment_requests_list ⇒ Object
GET /payment-requests.
-
#payments_list ⇒ Object
GET /payments.
-
#refund_detail(refund_id) ⇒ Object
GET /refunds/:refund_id.
-
#refunds_list ⇒ Object
GET /refunds.
- #to_s ⇒ Object
-
#upload_file(filepath) ⇒ Object
POST ‘filepicker.io/api/store/S3’.
Constructor Details
#initialize(api, endpoint) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/client/client.rb', line 17 def initialize(api, endpoint) @endpoint = endpoint || URL @conn = Faraday.new(@endpoint, &) #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 |
Instance Attribute Details
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
3 4 5 |
# File 'lib/client/client.rb', line 3 def app_id @app_id end |
#authorized ⇒ Object (readonly)
Returns the value of attribute authorized.
5 6 7 |
# File 'lib/client/client.rb', line 5 def @authorized end |
#connection_options ⇒ Object (readonly)
Returns the value of attribute connection_options.
3 4 5 |
# File 'lib/client/client.rb', line 3 def @connection_options end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
4 5 6 |
# File 'lib/client/client.rb', line 4 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
4 5 6 |
# File 'lib/client/client.rb', line 4 def response @response end |
Class Method Details
.define_http_verb(http_verb) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 |
Instance Method Details
#archive_link(slug) ⇒ Object
DELETE /links/:slug
112 113 114 |
# File 'lib/client/client.rb', line 112 def archive_link(slug) delete("links/#{slug}") end |
#authenticate(username = nil, password = nil, options = {}, &block) ⇒ Object
POST /auth/ Authenticate, generate token and add header
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/client/client.rb', line 60 def authenticate(username = nil, password = nil, = {}, &block) if username.is_a?(Hash) or password.is_a?(Hash) = username.is_a?(Hash) ? username : password end ["username"] = username if username and !username.is_a?Hash ["password"] = password if password and !password.is_a?Hash = (, &block) #TODO: Raise error if doesn't find username and password key in options @response = post('auth', ) if @response.has_key?("success") and @response['success'] add_header("X-Auth-Token", @response['token']) end @response end |
#create_link(options = {}, &block) ⇒ Object
POST /links
91 92 93 94 95 96 97 |
# File 'lib/client/client.rb', line 91 def create_link( = {}, &block) = (, &block) [:file_upload_json] = [:file_upload] && upload_file(.delete(:file_upload)) [:cover_image_json] = [:cover_image] && upload_file(.delete(:cover_image)) post('links', ) @response.success? ? Instamojo::Link.new(@response.body[:link], self) : @response end |
#create_refund(options = {}, &block) ⇒ Object
POST /refunds
172 173 174 175 176 |
# File 'lib/client/client.rb', line 172 def create_refund( = {}, &block) = (, &block) post('refunds', ) @response.success? ? Instamojo::Refund.new(@response.body[:refund], self) : @response end |
#edit_link(link = nil, options = {}, &block) ⇒ Object
PATCH /links/:slug
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/client/client.rb', line 100 def edit_link(link = nil, = {}, &block) if link && link.is_a?(Instamojo::Link) yield(link) if block_given? else = (, &block) link = Instamojo::Link.new(, self) end patch("links/#{link.slug}", link.to_h) @response.success? ? Instamojo::Link.new(@response.body[:link], self) : @response end |
#get_connection_object ⇒ Object
35 36 37 |
# File 'lib/client/client.rb', line 35 def get_connection_object @conn end |
#link_detail(slug) ⇒ Object
GET /links/:slug
84 85 86 87 88 |
# 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 ⇒ Object
GET /links
78 79 80 81 |
# 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 ⇒ Object
DELETE /auth/:token - Delete auth token
179 180 181 182 183 184 185 186 187 |
# 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) ⇒ Object
GET /payments/:payment_id
134 135 136 137 138 |
# 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) ⇒ Object
POST /payment-requests
141 142 143 144 145 |
# File 'lib/client/client.rb', line 141 def payment_request(, &block) (, &block) post('payment-requests', ) @response.success? ? Instamojo::PaymentRequest.new(@response.body[:payment_request], self) : @response end |
#payment_request_status(payment_request_id) ⇒ Object
153 154 155 156 157 |
# 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 ⇒ Object
GET /payment-requests
148 149 150 151 |
# 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 ⇒ Object
GET /payments
128 129 130 131 |
# 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) ⇒ Object
GET /refunds/:refund_id
166 167 168 169 |
# 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 ⇒ Object
GET /refunds
160 161 162 163 |
# 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 ⇒ Object
189 190 191 |
# File 'lib/client/client.rb', line 189 def to_s sprintf("Instamojo Client(URL: %s, Authorized: %s)", @endpoint, @authorized) end |
#upload_file(filepath) ⇒ Object
POST ‘filepicker.io/api/store/S3’
117 118 119 120 121 122 123 124 |
# File 'lib/client/client.rb', line 117 def upload_file(filepath) if filepath && (file=File.open(File.(filepath), 'rb')) if (url=get_file_upload_url).is_a? String resource = RestClient::Resource.new(url) resource.post fileUpload: file end end end |