Class: Celery::API
- Inherits:
-
Object
- Object
- Celery::API
- Defined in:
- lib/celery.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#host ⇒ Object
Returns the value of attribute host.
-
#path ⇒ Object
Returns the value of attribute path.
-
#session ⇒ Object
Returns the value of attribute session.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
-
#call(method, path, opts = {}, &block) ⇒ Object
Make a request to the Celery API.
-
#coupon ⇒ Celery::Coupons
Create an coupons object.
-
#delete(path, opts = {}, &block) ⇒ Object
Make a DELETE request.
-
#get(path, opts = {}, &block) ⇒ Object
Make a GET request.
-
#handle_response(response) ⇒ Object
Handle the response of a request.
-
#initialize(token = nil, debug = false) ⇒ API
constructor
A new instance of API.
-
#order ⇒ Celery::Orders
Create an orders object.
-
#patch(path, opts = {}, &block) ⇒ Object
Make a PATCH request.
-
#post(path, opts = {}, &block) ⇒ Object
Make a POST request.
-
#product ⇒ Celery::Products
Create an products object.
-
#put(path, opts = {}, &block) ⇒ Object
Make a PUT request.
-
#shop ⇒ Celery::Shop
Create an shop object.
-
#user ⇒ Celery::Users
Create an users object.
Constructor Details
#initialize(token = nil, debug = false) ⇒ API
Returns a new instance of API.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/celery.rb', line 18 def initialize(token=nil, debug=false) @host = 'https://api.trycelery.com' @path = '/v1' @session = Excon.new @host @debug = debug if not token token = ENV['CELERY_API_TOKEN'] if ENV['CELERY_API_TOKEN'] # TODO: Could put feedback warning here that some methods # require an API key. end @token = token end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
16 17 18 |
# File 'lib/celery.rb', line 16 def debug @debug end |
#host ⇒ Object
Returns the value of attribute host.
16 17 18 |
# File 'lib/celery.rb', line 16 def host @host end |
#path ⇒ Object
Returns the value of attribute path.
16 17 18 |
# File 'lib/celery.rb', line 16 def path @path end |
#session ⇒ Object
Returns the value of attribute session.
16 17 18 |
# File 'lib/celery.rb', line 16 def session @session end |
#token ⇒ Object
Returns the value of attribute token.
16 17 18 |
# File 'lib/celery.rb', line 16 def token @token end |
Instance Method Details
#call(method, path, opts = {}, &block) ⇒ Object
Make a request to the Celery API
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/celery.rb', line 37 def call(method, path, opts={}, &block) # :nodoc: # Ensure the body is JSON opts[:body] = JSON.generate(opts[:body]) if opts[:body] # Set the headers opts[:headers] ||= {} opts[:headers].merge!(headers) # Set the path opts[:path] = "#{@path}/#{path}" # Set the access token # TODO: Remove and use HTTP Basic Authorization opts[:query] ||= {} opts[:query].merge!(access_token: @token) # Make the request req = @session.send(method, opts) # Handle the response handle_response(req) end |
#coupon ⇒ Celery::Coupons
Create an coupons object
120 121 122 |
# File 'lib/celery.rb', line 120 def coupon Coupon.new self end |
#delete(path, opts = {}, &block) ⇒ Object
Make a DELETE request
86 87 88 |
# File 'lib/celery.rb', line 86 def delete(path, opts={}, &block) # :nodoc: call(:delete, path, opts, &block) end |
#get(path, opts = {}, &block) ⇒ Object
Make a GET request
58 59 60 |
# File 'lib/celery.rb', line 58 def get(path, opts={}, &block) # :nodoc: call(:get, path, opts, &block) end |
#handle_response(response) ⇒ Object
Handle the response of a request
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/celery.rb', line 91 def handle_response(response) # :nodoc: case response.status when 400 raise BadRequest.new JSON.parse(response.body)["error"] when 401 raise Unauthorized.new when 404 raise NotFound.new when 400...500 raise ClientError.new JSON.parse(response.body)["error"] when 500...600 raise ServerError.new else if response.body.is_a?(Integer) response.body else JSON.parse(response.body) end end end |
#order ⇒ Celery::Orders
Create an orders object
132 133 134 |
# File 'lib/celery.rb', line 132 def order Order.new self end |
#patch(path, opts = {}, &block) ⇒ Object
Make a PATCH request
79 80 81 |
# File 'lib/celery.rb', line 79 def patch(path, opts={}, &block) # :nodoc: call(:patch, path, opts, &block) end |
#post(path, opts = {}, &block) ⇒ Object
Make a POST request
65 66 67 |
# File 'lib/celery.rb', line 65 def post(path, opts={}, &block) # :nodoc: call(:post, path, opts, &block) end |
#product ⇒ Celery::Products
Create an products object
144 145 146 |
# File 'lib/celery.rb', line 144 def product Product.new self end |
#put(path, opts = {}, &block) ⇒ Object
Make a PUT request
72 73 74 |
# File 'lib/celery.rb', line 72 def put(path, opts={}, &block) # :nodoc: call(:put, path, opts, &block) end |
#shop ⇒ Celery::Shop
Create an shop object
156 157 158 |
# File 'lib/celery.rb', line 156 def shop Shop.new self end |