Class: ActiveResource::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/superdupe/active_resource_extensions.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

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



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/superdupe/active_resource_extensions.rb', line 149

def delete(path, headers = {}, external_request = false)
  # External requests are not allowed!      
  Dupe.network.request(:delete, path)

  ActiveResource::HttpMock.respond_to do |mock|
    mock.delete(path, {}, nil, 200)
  end
  response = request(:delete, path, build_request_headers(headers, :delete))
  
  ActiveResource::HttpMock.delete_mock(:delete, path)
  response
end

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

:nodoc:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/superdupe/active_resource_extensions.rb', line 73

def get(path, headers = {}, external_request = false) #:nodoc:
  if external_request
    # Without mocking - standard ActiveResource
    response = request(:get, path, build_request_headers(headers, :get))
  else
    mocked_response = Dupe.network.request(:get, path)
    ActiveResource::HttpMock.respond_to do |mock|
      mock.get(path, {}, mocked_response)
    end
    response = request(:get, path, build_request_headers(headers, :get))
    ActiveResource::HttpMock.delete_mock(:get, path)
  end
  format.decode(response.body)
end

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

:nodoc:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/superdupe/active_resource_extensions.rb', line 89

def post(path, body = '', headers = {}, external_request = false) #:nodoc:
  if external_request
    # external request - standard ActiveResource
    response = request(:post, path, body.to_s, build_request_headers(headers, :post))
  else
    resource_hash = Hash.from_xml(body)
    resource_hash = resource_hash[resource_hash.keys.first]
    resource_hash = {} unless resource_hash.kind_of?(Hash)
    begin
      mocked_response, new_path = Dupe.network.request(:post, path, resource_hash)
      error = false
    rescue Dupe::UnprocessableEntity => e
      mocked_response = {:error => e.message.to_s}.to_xml(:root => 'errors')
      error = true
    end
    ActiveResource::HttpMock.respond_to do |mock|
      if error
        mock.post(path, {}, mocked_response, 422, "Content-Type" => 'application/xml')
      else
        mock.post(path, {}, mocked_response, 201, "Location" => new_path)
      end
    end
    response = request(:post, path, body.to_s, build_request_headers(headers, :post))
    ActiveResource::HttpMock.delete_mock(:post, path)
  end
  response
end

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

:nodoc:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/superdupe/active_resource_extensions.rb', line 118

def put(path, body = '', headers = {}, external_request = false) #:nodoc:
  if external_request
    response = request(:put, path, body.to_s, build_request_headers(headers, :put))        
  else
    resource_hash = Hash.from_xml(body)
    resource_hash = resource_hash[resource_hash.keys.first]
    resource_hash = {} unless resource_hash.kind_of?(Hash)
    resource_hash.symbolize_keys!
  
    begin
      error = false
      mocked_response, path = Dupe.network.request(:put, path, resource_hash)
    rescue Dupe::UnprocessableEntity => e
      mocked_response = {:error => e.message.to_s}.to_xml(:root => 'errors')
      error = true
    end
    ActiveResource::HttpMock.respond_to do |mock|
      if error
        mock.put(path, {}, mocked_response, 422, "Content-Type" => 'application/xml')
      else
        mock.put(path, {}, mocked_response, 204)
      end
    end
    response = request(:put, path, body.to_s, build_request_headers(headers, :put))
    ActiveResource::HttpMock.delete_mock(:put, path)
  end

  response
end