Class: Nestful::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nestful/request.rb', line 10

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.



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

def auth_type
  @auth_type
end

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#follow_redirectionObject

Returns the value of attribute follow_redirection.



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

def follow_redirection
  @follow_redirection
end

#formatObject

Returns the value of attribute format.



3
4
5
# File 'lib/nestful/request.rb', line 3

def format
  @format
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#max_attemptsObject

Returns the value of attribute max_attempts.



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

def max_attempts
  @max_attempts
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/nestful/request.rb', line 3

def options
  @options
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#ssl_optionsObject

Returns the value of attribute ssl_options.



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

def ssl_options
  @ssl_options
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/nestful/request.rb', line 3

def url
  @url
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#encodedObject



74
75
76
# File 'lib/nestful/request.rb', line 74

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

#encoded?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/nestful/request.rb', line 70

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

#executeObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/nestful/request.rb', line 78

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



54
55
56
# File 'lib/nestful/request.rb', line 54

def path
  uri.path
end

#query_pathObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nestful/request.rb', line 58

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/nestful/request.rb', line 39

def uri
  return @uri if @uri

  url = @url.match(/\Ahttps?:\/\//) ? @url : "http://#{@url}"

  @uri = URI.parse(url)
  @uri.path = '/' if @uri.path.empty?

  @uri
end

#uri_paramsObject



50
51
52
# File 'lib/nestful/request.rb', line 50

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