Class: Instamojo::Client
- Inherits:
-
Object
- Object
- Instamojo::Client
- Defined in:
- lib/client/client.rb
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#authentication_flag ⇒ Object
readonly
Returns the value of attribute authentication_flag.
-
#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
-
#authenticate(username = nil, password = nil, options = {}, &block) ⇒ Object
POST /auth/ Authenticate, generate token and add header.
-
#create_offer(options = {}, &block) ⇒ Object
POST /offer/ - Create an offer.
-
#delete_offer(slug) ⇒ Object
DELETE /offer/:slug - Archives an offer.
-
#edit_offer(slug, options = {}, &block) ⇒ Object
PATCH /offer/.
- #get_connection_object ⇒ Object
-
#get_offer(slug) ⇒ Object
GET /offer/:slug.
-
#get_offers ⇒ Object
GET /offer - List all offers.
-
#initialize(api) ⇒ Client
constructor
A new instance of Client.
-
#logout ⇒ Object
DELETE /auth/:token - Delete auth token.
- #to_s ⇒ Object
-
#upload_file ⇒ Object
Uploading file and cover images.
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, &) #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_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 |
#authentication_flag ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute connection_options.
3 4 5 |
# File 'lib/client/client.rb', line 3 def 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
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, = {}, &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_offer(options = {}, &block) ⇒ Object
POST /offer/ - Create an offer
85 86 87 88 |
# File 'lib/client/client.rb', line 85 def create_offer( = {}, &block) = (, &block) @response = post('offer', ) 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, = {}, &block) = (, &block) patch("/offer/#{slug}", ) end |
#get_connection_object ⇒ Object
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_offers ⇒ Object
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 |
#logout ⇒ Object
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_s ⇒ Object
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_file ⇒ Object
Uploading file and cover images
105 106 107 |
# File 'lib/client/client.rb', line 105 def upload_file #TODO end |