Class: Viddler::Request

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

Overview

Class used to send requests over http to Viddler API.

Constant Summary collapse

API_URL =

:nodoc:

'http://api.viddler.com/rest/v1/'
DEFAULT_HEADERS =
{:accept => 'application/xml', :content_type => 'application/x-www-form-urlencoded'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, method) ⇒ Request

:nodoc:



25
26
27
28
29
30
# File 'lib/viddler/request.rb', line 25

def initialize(http_method, method) #:nodoc:
  @http_method = http_method.to_s
  @url = API_URL
  self.params = {:method => viddlerize(method)}
  self.headers = DEFAULT_HEADERS
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



22
23
24
# File 'lib/viddler/request.rb', line 22

def body
  @body
end

#headersObject

Returns the value of attribute headers.



23
24
25
# File 'lib/viddler/request.rb', line 23

def headers
  @headers
end

#http_methodObject

Returns the value of attribute http_method.



22
23
24
# File 'lib/viddler/request.rb', line 22

def http_method
  @http_method
end

#paramsObject

Returns the value of attribute params.



23
24
25
# File 'lib/viddler/request.rb', line 23

def params
  @params
end

#responseObject

Returns the value of attribute response.



22
23
24
# File 'lib/viddler/request.rb', line 22

def response
  @response
end

#urlObject

Returns the value of attribute url.



22
23
24
# File 'lib/viddler/request.rb', line 22

def url
  @url
end

Instance Method Details

#run(&block) ⇒ Object

Send http request to Viddler API.



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

def run(&block)
  if block_given?
    set(:params, &block)
  end
  
  if post? and multipart?
    put_multipart_params_into_body
  else
    put_params_into_url
  end
  request = RestClient::Request.execute(
     :method => http_method,
     :url => url,
     :headers => headers,
     :payload => body
   )
   self.response = parse_response(request)
end

#set(container, &declarations) ⇒ Object

Use this method to setup your request’s payload and headers.

Example:

request.set :headers do |h|
  h.content_type = 'application/ufo'
end

request.set :params do |p|
  p.sessionid = '12323'
  p.api_key   = '13123
end


45
46
47
48
49
# File 'lib/viddler/request.rb', line 45

def set(container, &declarations)
  struct = OpenStruct.new
  declarations.call(struct)
  send("#{container}=", struct.table)
end