Class: ActiveResource::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/dupe/active_resource.rb

Overview

:nodoc:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.request_logObject (readonly)

Returns the value of attribute request_log.



32
33
34
# File 'lib/dupe/active_resource.rb', line 32

def request_log
  @request_log
end

Class Method Details

.flush_request_logObject



44
45
46
# File 'lib/dupe/active_resource.rb', line 44

def flush_request_log
  @request_log = []
end

.log_request(method, path, headers, response) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/dupe/active_resource.rb', line 34

def log_request(method, path, headers, response)
  @request_log ||= []
  @request_log << {
    :method   => method,
    :path     => path,
    :headers  => headers,
    :response => response
  }
end


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dupe/active_resource.rb', line 48

def print_request_log
  @request_log ||= []
  if @request_log.empty?
    puts("\n  -----No request attempts logged for this scenario")  
    return
  end
  puts "\n    Request attempts logged for this scenario:\n    --------------------------------------------\n\n"
  @request_log.each do |request|
    puts "    Request: #{request[:method].to_s.upcase} #{request[:path]}"
    puts "    Headers: #{request[:headers].inspect}"
    puts "    Response Body:\n#{request[:response].body.split("\n").map {|s| (" "*6) + s}.join("\n")}"
    puts "    Response Code: #{request[:response].code}"
    puts "    Response Headers: #{request[:response].headers}"
    puts "    Response Message: #{request[:response].message}"
    puts "\n\n"
  end
end

Instance Method Details

#delete(path, headers = {}) ⇒ Object

Execute a DELETE request (see HTTP protocol documentation if unfamiliar). Used to delete resources.



77
78
79
80
81
# File 'lib/dupe/active_resource.rb', line 77

def delete(path, headers = {})
  response = request(:delete, path, build_request_headers(headers, :delete))
  ActiveResource::Connection.log_request(:delete, path, build_request_headers(headers, :delete), response)
  response
end

#get(path, headers = {}) ⇒ Object

Execute a GET request. Used to get (find) resources.



69
70
71
72
73
# File 'lib/dupe/active_resource.rb', line 69

def get(path, headers = {})
  response = request(:get, path, build_request_headers(headers, :get))
  ActiveResource::Connection.log_request(:get, path, build_request_headers(headers, :get), response)
  format.decode(response.body)
end

#head(path, headers = {}) ⇒ Object

Execute a HEAD request. Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).



101
102
103
104
105
# File 'lib/dupe/active_resource.rb', line 101

def head(path, headers = {})
  response = request(:head, path, build_request_headers(headers))
  ActiveResource::Connection.log_request(:head, path, build_request_headers(headers), response)
  response
end

#post(path, body = '', headers = {}) ⇒ Object

Execute a POST request. Used to create new resources.



93
94
95
96
97
# File 'lib/dupe/active_resource.rb', line 93

def post(path, body = '', headers = {})
  response = request(:post, path, body.to_s, build_request_headers(headers, :post))
  ActiveResource::Connection.log_request(:post, path, build_request_headers(headers, :post), response) 
  response
end

#put(path, body = '', headers = {}) ⇒ Object

Execute a PUT request (see HTTP protocol documentation if unfamiliar). Used to update resources.



85
86
87
88
89
# File 'lib/dupe/active_resource.rb', line 85

def put(path, body = '', headers = {})
  response = request(:put, path, body.to_s, build_request_headers(headers, :put))
  ActiveResource::Connection.log_request(:put, path, build_request_headers(headers, :put), response)
  response
end