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.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#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.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#ssl_options ⇒ Object
Connection options.
-
#timeout ⇒ Object
Connection options.
-
#url ⇒ Object
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 |
# File 'lib/nestful/request.rb', line 15 def initialize(url, = {}) @url = url.to_s @options = @options.each do |key, val| method = "#{key}=" send(method, val) if respond_to?(method) end self.method ||= :get self.params ||= {} self.headers ||= {} self.body ||= '' self.format ||= :blank 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 |
#extension ⇒ Object
Returns the value of attribute extension.
10 11 12 |
# File 'lib/nestful/request.rb', line 10 def extension @extension 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 |
#raw ⇒ Object
Returns the value of attribute raw.
10 11 12 |
# File 'lib/nestful/request.rb', line 10 def raw @raw 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
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
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/nestful/request.rb', line 36 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/nestful/request.rb', line 87 def execute callback(:before_request, self) result = nil if [:post, :put].include?(method) connection.send(method, path, encoded, headers) do |res| result = decoded(res) result.class_eval { attr_accessor :response } result.response = res end else connection.send(method, query_path, headers) do |res| result = decoded(res) result.class_eval { attr_accessor :response } result.response = res end end callback(:after_request, self, result) result rescue Redirection => error self.url = error.response['Location'] execute 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
74 75 76 |
# File 'lib/nestful/request.rb', line 74 def path uri.path end |
#query_path ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/nestful/request.rb', line 78 def query_path query_path = path if params.any? query_path += "?" query_path += params.to_param end query_path end |
#uri ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/nestful/request.rb', line 53 def uri return @uri if @uri url = @url.match(/^http/) ? @url : "http://#{@url}" @uri = URI.parse(url) @uri.path = "/" if @uri.path.empty? if extension extension = format.extension if extension.is_a?(Boolean) @uri.path += ".#{extension}" end @uri.query.split("&").each do |res| key, value = res.split("=") @params[key] = value end if @uri.query @uri end |