Class: Nestful::Request

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

Constant Summary collapse

UriParser =
URI.const_defined?(:Parser) ? URI::Parser.new : URI

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @url     = url.to_s

  @options = {
    :method           => :get,
    :params           => {},
    :headers          => {},
    :format           => :form,
    :max_attempts     => 5,
    :follow_redirection => true
  }.merge(options)

  @options.each do |key, val|
    method = "#{key}="
    send(method, val) if respond_to?(method)
  end
end

Instance Attribute Details

#auth_typeObject

Returns the value of attribute auth_type.



7
8
9
# File 'lib/nestful/request.rb', line 7

def auth_type
  @auth_type
end

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/nestful/request.rb', line 7

def body
  @body
end

#follow_redirectionObject

Returns the value of attribute follow_redirection.



7
8
9
# File 'lib/nestful/request.rb', line 7

def follow_redirection
  @follow_redirection
end

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/nestful/request.rb', line 5

def format
  @format
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/nestful/request.rb', line 7

def headers
  @headers
end

#max_attemptsObject

Returns the value of attribute max_attempts.



7
8
9
# File 'lib/nestful/request.rb', line 7

def max_attempts
  @max_attempts
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'lib/nestful/request.rb', line 7

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/nestful/request.rb', line 5

def options
  @options
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/nestful/request.rb', line 7

def params
  @params
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/nestful/request.rb', line 7

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



7
8
9
# File 'lib/nestful/request.rb', line 7

def proxy
  @proxy
end

#ssl_optionsObject

Returns the value of attribute ssl_options.



7
8
9
# File 'lib/nestful/request.rb', line 7

def ssl_options
  @ssl_options
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/nestful/request.rb', line 7

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/nestful/request.rb', line 5

def url
  @url
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/nestful/request.rb', line 7

def user
  @user
end

Instance Method Details

#encodedObject



76
77
78
# File 'lib/nestful/request.rb', line 76

def encoded
  params.any? ? format.encode(params) : body
end

#encoded?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/nestful/request.rb', line 72

def encoded?
  [:post, :put].include?(method)
end

#executeObject



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

#pathObject



56
57
58
# File 'lib/nestful/request.rb', line 56

def path
  uri.path
end

#query_pathObject



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

#uriObject



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_paramsObject



52
53
54
# File 'lib/nestful/request.rb', line 52

def uri_params
  uri.query ? Helpers.from_param(uri.query) : {}
end