Class: Nestful::Request
- Inherits:
-
Object
- Object
- Nestful::Request
- Defined in:
- lib/nestful/request.rb
Constant Summary collapse
- UriParser =
URI.const_defined?(:Parser) ? URI::Parser.new : URI
Instance Attribute Summary collapse
-
#auth_type ⇒ Object
Returns the value of attribute auth_type.
-
#body ⇒ Object
Returns the value of attribute body.
-
#follow_redirection ⇒ Object
Returns the value of attribute follow_redirection.
-
#format ⇒ Object
Returns the value of attribute format.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#max_attempts ⇒ Object
Returns the value of attribute max_attempts.
-
#method ⇒ Object
Returns the value of attribute method.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#password ⇒ Object
Returns the value of attribute password.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#ssl_options ⇒ Object
Returns the value of attribute ssl_options.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#url ⇒ Object
Returns the value of attribute url.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #encoded ⇒ Object
- #encoded? ⇒ Boolean
- #execute ⇒ Object
-
#initialize(url, options = {}) ⇒ Request
constructor
A new instance of Request.
- #path ⇒ Object
- #query_path ⇒ Object
- #uri ⇒ Object
- #uri_params ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/nestful/request.rb', line 12 def initialize(url, = {}) @url = url.to_s @options = { :method => :get, :params => {}, :headers => {}, :format => :form, :max_attempts => 5, :follow_redirection => true }.merge() @options.each do |key, val| method = "#{key}=" send(method, val) if respond_to?(method) end end |
Instance Attribute Details
#auth_type ⇒ Object
Returns the value of attribute auth_type.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def auth_type @auth_type end |
#body ⇒ Object
Returns the value of attribute body.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def body @body end |
#follow_redirection ⇒ Object
Returns the value of attribute follow_redirection.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def follow_redirection @follow_redirection end |
#format ⇒ Object
Returns the value of attribute format.
5 6 7 |
# File 'lib/nestful/request.rb', line 5 def format @format end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def headers @headers end |
#max_attempts ⇒ Object
Returns the value of attribute max_attempts.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def max_attempts @max_attempts end |
#method ⇒ Object
Returns the value of attribute method.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def method @method end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/nestful/request.rb', line 5 def @options end |
#params ⇒ Object
Returns the value of attribute params.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def params @params end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def password @password end |
#proxy ⇒ Object
Returns the value of attribute proxy.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def proxy @proxy end |
#ssl_options ⇒ Object
Returns the value of attribute ssl_options.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def @ssl_options end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def timeout @timeout end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/nestful/request.rb', line 5 def url @url end |
#user ⇒ Object
Returns the value of attribute user.
7 8 9 |
# File 'lib/nestful/request.rb', line 7 def user @user end |
Instance Method Details
#encoded ⇒ Object
76 77 78 |
# File 'lib/nestful/request.rb', line 76 def encoded params.any? ? format.encode(params) : body end |
#encoded? ⇒ Boolean
72 73 74 |
# File 'lib/nestful/request.rb', line 72 def encoded? [:post, :put].include?(method) end |
#execute ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/nestful/request.rb', line 80 def execute with_redirection do if encoded? result = connection.send(method, query_path, encoded, build_headers) else result = connection.send(method, query_path, build_headers) end Response.new(result, uri) end end |
#path ⇒ Object
56 57 58 |
# File 'lib/nestful/request.rb', line 56 def path uri.path end |
#query_path ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/nestful/request.rb', line 60 def query_path query_path = path.dup query_params = uri_params.dup query_params.merge!(params) unless encoded? if query_params.any? query_path += '?' + Helpers.to_url_param(query_params) end query_path end |
#uri ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/nestful/request.rb', line 41 def uri return @uri if defined?(@uri) && @uri url = @url.match(/\Ahttps?:\/\//) ? @url : "http://#{@url}" @uri = UriParser.parse(url) @uri.path = '/' if @uri.path.empty? @uri end |
#uri_params ⇒ Object
52 53 54 |
# File 'lib/nestful/request.rb', line 52 def uri_params uri.query ? Helpers.from_param(uri.query) : {} end |