Class: Nestful::Request
- Inherits:
-
Object
- Object
- Nestful::Request
- Includes:
- Callbacks
- Defined in:
- lib/nestful/oauth.rb,
lib/nestful/request.rb,
lib/nestful/request.rb,
lib/nestful/request/callbacks.rb
Defined Under Namespace
Modules: Callbacks
Constant Summary
Constants included from Callbacks
Instance Attribute Summary collapse
-
#auth_type ⇒ Object
Connection options.
-
#body ⇒ Object
Returns the value of attribute body.
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#callbacks ⇒ Object
writeonly
Sets the attribute callbacks.
-
#format ⇒ Object
Returns the value of attribute format.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#oauth ⇒ Object
Returns the value of attribute oauth.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#password ⇒ Object
Connection options.
-
#proxy ⇒ Object
Connection options.
-
#ssl_options ⇒ Object
Connection options.
-
#timeout ⇒ Object
Connection options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user ⇒ Object
Connection options.
Class Method Summary collapse
-
.callbacks(type = nil) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #connection ⇒ Object
- #execute ⇒ Object
-
#initialize(url, options = {}) ⇒ Request
constructor
A new instance of Request.
- #oauth_sign ⇒ Object
- #path ⇒ Object
- #query_path ⇒ Object
- #uri ⇒ Object
Methods included from Callbacks
Constructor Details
#initialize(url, options = {}) ⇒ Request
Returns a new instance of Request.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nestful/request.rb', line 15 def initialize(url, = {}) @url = url.to_s @options = @options.each {|key, val| method = "#{key}=" send(method, val) if respond_to?(method) } self.method ||= :get self.format ||= :blank self.headers ||= {} self.params ||= {} self.body ||= '' if self.uri.query populate_query_params end end |
Instance Attribute Details
#auth_type ⇒ Object
Connection options
13 14 15 |
# File 'lib/nestful/request.rb', line 13 def auth_type @auth_type end |
#body ⇒ Object
Returns the value of attribute body.
10 11 12 |
# File 'lib/nestful/request.rb', line 10 def body @body end |
#buffer ⇒ Object
Returns the value of attribute buffer.
10 11 12 |
# File 'lib/nestful/request.rb', line 10 def buffer @buffer end |
#callbacks=(value) ⇒ Object
Sets the attribute callbacks
10 11 12 |
# File 'lib/nestful/request.rb', line 10 def callbacks=(value) @callbacks = value end |
#format ⇒ Object
Returns the value of attribute format.
9 10 11 |
# File 'lib/nestful/request.rb', line 9 def format @format end |
#headers ⇒ Object
Returns the value of attribute headers.
10 11 12 |
# File 'lib/nestful/request.rb', line 10 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
10 11 12 |
# File 'lib/nestful/request.rb', line 10 def method @method end |
#oauth ⇒ Object
Returns the value of attribute oauth.
5 6 7 |
# File 'lib/nestful/oauth.rb', line 5 def oauth @oauth end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/nestful/request.rb', line 9 def @options end |
#params ⇒ Object
Returns the value of attribute params.
10 11 12 |
# File 'lib/nestful/request.rb', line 10 def params @params end |
#password ⇒ Object
Connection options
13 14 15 |
# File 'lib/nestful/request.rb', line 13 def password @password end |
#proxy ⇒ Object
Connection options
13 14 15 |
# File 'lib/nestful/request.rb', line 13 def proxy @proxy end |
#ssl_options ⇒ Object
Connection options
13 14 15 |
# File 'lib/nestful/request.rb', line 13 def @ssl_options end |
#timeout ⇒ Object
Connection options
13 14 15 |
# File 'lib/nestful/request.rb', line 13 def timeout @timeout end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
9 10 11 |
# File 'lib/nestful/request.rb', line 9 def url @url end |
#user ⇒ Object
Connection options
13 14 15 |
# File 'lib/nestful/request.rb', line 13 def user @user end |
Class Method Details
.callbacks(type = nil) ⇒ Object
:nodoc:
3 4 5 6 7 |
# File 'lib/nestful/request.rb', line 3 def self.callbacks(type = nil) #:nodoc: @callbacks ||= {} return @callbacks unless type @callbacks[type] ||= [] end |
Instance Method Details
#connection ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nestful/request.rb', line 38 def connection conn = Connection.new(uri, format) conn.proxy = proxy if proxy conn.user = user if user conn.password = password if password conn.auth_type = auth_type if auth_type conn.timeout = timeout if timeout conn. = if conn end |
#execute ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/nestful/request.rb', line 72 def execute callback(:before_request, self) result = nil if [:post, :put].include?(method) connection.send(method, path, encoded, headers) {|res| result = decoded(res) } else connection.send(method, query_path, headers) {|res| result = decoded(res) } end callback(:after_request, self, result) result end |
#oauth_sign ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/nestful/oauth.rb', line 7 def oauth_sign return unless oauth params = method == :get ? self.params : {} signature = ROAuth.header( oauth, uri.to_s, params, method ) self.headers ||= {} self.headers["Authorization"] = signature end |
#path ⇒ Object
59 60 61 |
# File 'lib/nestful/request.rb', line 59 def path uri.path end |
#query_path ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/nestful/request.rb', line 63 def query_path query_path = path if params.any? query_path += "?" query_path += params.to_param end query_path end |
#uri ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/nestful/request.rb', line 49 def uri http_url = url.match(/^http/) ? url : "http://#{url}" uri = URI.parse(http_url) uri.path = "/" if uri.path.empty? if format && format.extension && !uri.path.match(/\..+/) uri.path += ".#{format.extension}" end uri end |