Class: Glomper::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(client, api, *args, &block) ⇒ Request

Returns a new instance of Request.



4
5
6
7
8
9
# File 'lib/glomper/request.rb', line 4

def initialize(client, api, *args, &block)
  @client = client
  @api = api
  @id = args.first
  @method = 'post'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/glomper/request.rb', line 11

def method_missing(meth, *args, &block)
  if @api[:methods][meth] || @api[:actions][meth] || @api[:aspects][meth]
    @method = 'get' if @api[:aspects][meth] 
    send_request(meth, *args, &block)
  else
    super
  end
end

Instance Method Details

#build_url(meth) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/glomper/request.rb', line 27

def build_url meth
  url = '/api/%s/' % @api[:url]
  if @api[:methods][meth]
    url += meth.to_s
  else
    url += '%s/%s' % [@id?@id:'self', meth]
  end
  url
end

#send_request(meth, *args, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/glomper/request.rb', line 20

def send_request(meth, *args, &block)
  response = @client.connection.send(@method) do |req|
    req.url build_url(meth), *args
  end
  @client.return_error_or_body(response, response.body.response)
end