Class: Instamojo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/client/client.rb

Constant Summary collapse

URL =
Instamojo::HOST + "/api/" + Instamojo::API_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, &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

Instance Attribute Details

#app_idObject (readonly)

Returns the value of attribute app_id.



3
4
5
# File 'lib/client/client.rb', line 3

def app_id
  @app_id
end

#authorizedObject (readonly)

Returns the value of attribute authorized.



5
6
7
# File 'lib/client/client.rb', line 5

def authorized
  @authorized
end

#connection_optionsObject (readonly)

Returns the value of attribute connection_options.



3
4
5
# File 'lib/client/client.rb', line 3

def connection_options
  @connection_options
end

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'lib/client/client.rb', line 4

def request
  @request
end

#responseObject (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

DELETE /links/:slug



113
114
115
# File 'lib/client/client.rb', line 113

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, 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

POST /links



92
93
94
95
96
97
98
# File 'lib/client/client.rb', line 92

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) ⇒ Object

POST /refunds



173
174
175
176
177
# 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

PATCH /links/:slug



101
102
103
104
105
106
107
108
109
110
# File 'lib/client/client.rb', line 101

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_objectObject



35
36
37
# File 'lib/client/client.rb', line 35

def get_connection_object
  @conn
end

GET /links/:slug



85
86
87
88
89
# File 'lib/client/client.rb', line 85

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

GET /links



78
79
80
81
# File 'lib/client/client.rb', line 78

def links_list(opts = {})
  get('links', opts)
  @response.success? ? @response.body[:links].map { |link| Instamojo::Link.new link, self } : @response
end

#logoutObject

DELETE /auth/:token - Delete auth token



180
181
182
183
184
185
186
187
188
# 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) ⇒ Object

GET /payments/:payment_id



135
136
137
138
139
# 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) ⇒ Object

POST /payment-requests



142
143
144
145
146
# 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) ⇒ Object



154
155
156
157
158
# 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 = {}) ⇒ Object

GET /payment-requests



149
150
151
152
# 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 = {}) ⇒ Object

GET /payments



129
130
131
132
# 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) ⇒ Object

GET /refunds/:refund_id



167
168
169
170
# 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 = {}) ⇒ Object

GET /refunds



161
162
163
164
# 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_sObject



190
191
192
# File 'lib/client/client.rb', line 190

def to_s
  sprintf("Instamojo Client(URL: %s, Authorized: %s)", @endpoint, @authorized)
end

#upload_file(filepath) ⇒ Object



118
119
120
121
122
123
124
125
# 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