Class: Restfully::HTTP::Request

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/restfully/http/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#sanitize_head, #sanitize_query

Constructor Details

#initialize(session, method, path, 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/restfully/http/request.rb', line 12

def initialize(session, method, path, options = {})
  @session = session
  @attempts = 0

  request = options.symbolize_keys

  @retry_on_error = request[:retry_on_error] || session.config[:retry_on_error]
  @wait_before_retry = request[:wait_before_retry] || session.config[:wait_before_retry]

  request[:method] = method

  request[:head] = sanitize_head(@session.default_headers).merge(
    build_head(request)
  )
  origin_content_type = request[:head].delete('Origin-Content-Type')
  request[:head]['Content-Type'] ||= origin_content_type if origin_content_type

  request[:uri] = @session.uri_to(path)
  if request[:query]
    request[:uri].query_values = sanitize_query(request[:query])
  end

  request[:body] = if [:post, :put].include?(request[:method])
    build_body(request)
  end

  @method, @uri, @head, @body = request.values_at(
    :method, :uri, :head, :body
  )

end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



9
10
11
# File 'lib/restfully/http/request.rb', line 9

def attempts
  @attempts
end

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/restfully/http/request.rb', line 9

def body
  @body
end

#headObject (readonly)

Returns the value of attribute head.



9
10
11
# File 'lib/restfully/http/request.rb', line 9

def head
  @head
end

#methodObject (readonly)

Returns the value of attribute method.



9
10
11
# File 'lib/restfully/http/request.rb', line 9

def method
  @method
end

#retry_on_errorObject

Returns the value of attribute retry_on_error.



10
11
12
# File 'lib/restfully/http/request.rb', line 10

def retry_on_error
  @retry_on_error
end

#sessionObject (readonly)

Returns the value of attribute session.



9
10
11
# File 'lib/restfully/http/request.rb', line 9

def session
  @session
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/restfully/http/request.rb', line 9

def uri
  @uri
end

#wait_before_retryObject

Returns the value of attribute wait_before_retry.



10
11
12
# File 'lib/restfully/http/request.rb', line 10

def wait_before_retry
  @wait_before_retry
end

Instance Method Details

#execute!Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/restfully/http/request.rb', line 78

def execute!
  session.logger.debug self.inspect
  resource = RestClient::Resource.new(
    uri.to_s,
    @session.ssl_options.merge({:headers => head})
  )

  begin
    reqcode, reqhead, reqbody = resource.send(method, body || {})
    response = Response.new(session, reqcode, reqhead, reqbody)
    session.logger.debug response.inspect
    response
  rescue Errno::ECONNREFUSED => e
    retry! || raise(e)
  end
end

#forced_cache?Boolean

Returns:

  • (Boolean)


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

def forced_cache?
  !!@forced_cache
end

#inspectObject



61
62
63
# File 'lib/restfully/http/request.rb', line 61

def inspect
  "#{method.to_s.upcase} #{uri.to_s}, head=#{head.inspect}, body=#{body.inspect}"
end

#no_cache!Object



69
70
71
72
# File 'lib/restfully/http/request.rb', line 69

def no_cache!
  @forced_cache = true
  head['Cache-Control'] = 'no-cache'
end

#no_cache?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/restfully/http/request.rb', line 65

def no_cache?
  head['Cache-Control'] && head['Cache-Control'].include?('no-cache')
end

#remove_no_cache!Object



106
107
108
109
110
111
112
113
# File 'lib/restfully/http/request.rb', line 106

def remove_no_cache!
  @forced_cache = false
  if head['Cache-Control']
    head['Cache-Control'] = head['Cache-Control'].split(/\s+,\s+/).reject{|v|
      v =~ /no-cache/i
    }.join(",")
  end
end

#retry!Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/restfully/http/request.rb', line 95

def retry!
  if @attempts < @retry_on_error
    @attempts+=1
    session.logger.warn "Encountered connection or server error. Retrying in #{@wait_before_retry}s... [#{@attempts}/#{@retry_on_error}]"
    sleep @wait_before_retry if @wait_before_retry > 0
    execute!
  else
    false
  end
end

#update!(options = {}) ⇒ Object

Updates the request header and query parameters. Returns nil if no changes were made, otherwise self.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/restfully/http/request.rb', line 46

def update!(options = {})
  objects_that_may_be_updated = [@uri, @head]
  old_hash = objects_that_may_be_updated.map(&:hash)
  opts = options.symbolize_keys
  @head.merge!(build_head(opts))
  if opts[:query]
    @uri.query_values = sanitize_query(opts[:query])
  end
  if old_hash == objects_that_may_be_updated.map(&:hash)
    nil
  else
    self
  end
end