Class: Celery::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#debugObject

Returns the value of attribute debug.



16
17
18
# File 'lib/celery.rb', line 16

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



16
17
18
# File 'lib/celery.rb', line 16

def host
  @host
end

#pathObject

Returns the value of attribute path.



16
17
18
# File 'lib/celery.rb', line 16

def path
  @path
end

#sessionObject

Returns the value of attribute session.



16
17
18
# File 'lib/celery.rb', line 16

def session
  @session
end

#tokenObject

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

Parameters:

  • verb (Symbol)

    the HTTP request method

  • path (String)

    the HTTP URL path of the request

  • opts (Hash) (defaults to: {})

    the options to make the request with



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

#couponCelery::Coupons

Create an coupons object

Examples:


coupons = celery.coupons

Returns:

  • (Celery::Coupons)


120
121
122
# File 'lib/celery.rb', line 120

def coupon
  Coupon.new self
end

#delete(path, opts = {}, &block) ⇒ Object

Make a DELETE request

See Also:



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

See Also:



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

#orderCelery::Orders

Create an orders object

Examples:


orders = celery.orders

Returns:

  • (Celery::Orders)


132
133
134
# File 'lib/celery.rb', line 132

def order
  Order.new self
end

#patch(path, opts = {}, &block) ⇒ Object

Make a PATCH request

See Also:



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

See Also:



65
66
67
# File 'lib/celery.rb', line 65

def post(path, opts={}, &block) # :nodoc:
  call(:post, path, opts, &block)
end

#productCelery::Products

Create an products object

Examples:


products = celery.products

Returns:

  • (Celery::Products)


144
145
146
# File 'lib/celery.rb', line 144

def product
  Product.new self
end

#put(path, opts = {}, &block) ⇒ Object

Make a PUT request

See Also:



72
73
74
# File 'lib/celery.rb', line 72

def put(path, opts={}, &block) # :nodoc:
  call(:put, path, opts, &block)
end

#shopCelery::Shop

Create an shop object

Examples:


shop = celery.shop

Returns:



156
157
158
# File 'lib/celery.rb', line 156

def shop
  Shop.new self
end

#userCelery::Users

Create an users object

Examples:


users = celery.users

Returns:

  • (Celery::Users)


168
169
170
# File 'lib/celery.rb', line 168

def user
  User.new self
end