Class: Viddler::Request
- Inherits:
-
Object
- Object
- Viddler::Request
- 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
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#response ⇒ Object
Returns the value of attribute response.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(http_method, method) ⇒ Request
constructor
:nodoc:.
-
#run(&block) ⇒ Object
Send http request to Viddler API.
-
#set(container, &declarations) ⇒ Object
Use this method to setup your request’s payload and headers.
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
#body ⇒ Object
Returns the value of attribute body.
22 23 24 |
# File 'lib/viddler/request.rb', line 22 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
23 24 25 |
# File 'lib/viddler/request.rb', line 23 def headers @headers end |
#http_method ⇒ Object
Returns the value of attribute http_method.
22 23 24 |
# File 'lib/viddler/request.rb', line 22 def http_method @http_method end |
#params ⇒ Object
Returns the value of attribute params.
23 24 25 |
# File 'lib/viddler/request.rb', line 23 def params @params end |
#response ⇒ Object
Returns the value of attribute response.
22 23 24 |
# File 'lib/viddler/request.rb', line 22 def response @response end |
#url ⇒ Object
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 |