Class: Instamojo::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
# File 'lib/client/client.rb', line 15

def initialize(api)
  @conn = Faraday.new(Instamojo::URL, &connection_options)

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

  @app_id = api.app_id
  add_header("X-App-Id", @app_id)
  @authentication_flag = "Not authenticated"
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

#authentication_flagObject (readonly)

Returns the value of attribute authentication_flag.



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

def authentication_flag
  @authentication_flag
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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/client/client.rb', line 31

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(Instamojo::PREFIX + @request, params)
    return sanitize_response
  end
end

Instance Method Details

#authenticate(username = nil, password = nil, options = {}, &block) ⇒ Object

POST /auth/ Authenticate, generate token and add header



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/client/client.rb', line 52

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_offer(options = {}, &block) ⇒ Object

POST /offer/ - Create an offer



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

def create_offer(options = {}, &block)
  options = set_options(options, &block)
  @response = post('offer', options)
end

#delete_offer(slug) ⇒ Object

DELETE /offer/:slug - Archives an offer



97
98
99
100
101
102
# File 'lib/client/client.rb', line 97

def delete_offer(slug)
  unless get_connection_object.headers.has_key?("X-Auth-Token")
    raise "Please authenticate() to see your offers"
  end
  delete("/offer/#{slug}")
end

#edit_offer(slug, options = {}, &block) ⇒ Object

PATCH /offer/



91
92
93
94
# File 'lib/client/client.rb', line 91

def edit_offer(slug, options = {}, &block)
  options = set_options(options, &block)
  patch("/offer/#{slug}", options)
end

#get_connection_objectObject



26
27
28
# File 'lib/client/client.rb', line 26

def get_connection_object
  @conn #Faraday::Connection object
end

#get_offer(slug) ⇒ Object

GET /offer/:slug



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

def get_offer(slug)
  get("offer/#{slug}")
end

#get_offersObject

GET /offer - List all offers



71
72
73
74
75
76
# File 'lib/client/client.rb', line 71

def get_offers
  unless get_connection_object.headers.has_key?("X-Auth-Token")
    raise "Please authenticate() to see your offers"
  end
  get('offer')
end

#logoutObject

DELETE /auth/:token - Delete auth token



111
112
113
114
115
116
117
118
119
# File 'lib/client/client.rb', line 111

def logout
  auth_token = get_connection_object.headers['X-Auth-Token']
  raise "Can't find any authorization token to logout." unless auth_token
  delete("/auth/#{auth_token}")
  if @response.has_key?("success") and @response['success']
    get_connection_object.headers.delete("X-Auth-Token")
  end
  @response
end

#to_sObject



122
123
124
125
126
127
# File 'lib/client/client.rb', line 122

def to_s
  sprintf("Instamojo Client(URL: %s, Status: %s)",
          Instamojo::URL + Instamojo::PREFIX,
          @authentication_flag
  )
end

#upload_fileObject

Uploading file and cover images



105
106
107
# File 'lib/client/client.rb', line 105

def upload_file
  #TODO
end