Module: Sovaa::HTTP

Extended by:
HTTP
Included in:
HTTP
Defined in:
lib/sovaa/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



23
24
25
# File 'lib/sovaa/http.rb', line 23

def adapter
  @adapter
end

Instance Method Details

#copy(uri, destination) ⇒ Object



64
65
66
67
68
# File 'lib/sovaa/http.rb', line 64

def copy(uri, destination)
  headers = {'X-HTTP-Method-Override' => 'COPY', 'Destination' => destination}
  response = request(:post, uri, nil, headers)
  parsed_response(response)
end

#delete(uri) ⇒ Object



59
60
61
62
# File 'lib/sovaa/http.rb', line 59

def delete(uri)
  response = request(:delete, uri)
  parsed_response(response)
end

#get(uri) ⇒ Object



48
49
50
51
# File 'lib/sovaa/http.rb', line 48

def get(uri)
  response = request(:get, uri)
  parsed_response(response)
end

#parsed_response(response) ⇒ Object



70
71
72
73
# File 'lib/sovaa/http.rb', line 70

def parsed_response(response)
  parsed = Yajl::Parser.parse(response.body)
  parsed.is_a?(Hash) ? JsonResponse.new(parsed, response.headers['ETag']) : parsed
end

#post(uri, doc = nil) ⇒ Object



53
54
55
56
57
# File 'lib/sovaa/http.rb', line 53

def post(uri, doc=nil)
  doc = Yajl::Encoder.encode(doc) if doc
  response = request(:post, uri, doc)
  parsed_response(response)
end

#put(uri, doc = nil, headers = {}) ⇒ Object



42
43
44
45
46
# File 'lib/sovaa/http.rb', line 42

def put(uri, doc=nil, headers={})
  doc = Yajl::Encoder.encode(doc) if doc
  response = request(:put, uri, doc, headers)
  parsed_response(response)
end

#request(method, uri, doc = nil, headers = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sovaa/http.rb', line 25

def request method, uri, doc=nil, headers={}
  uri = URI.parse(uri)
  request = HTTPI::Request.new
  request.url = uri
  request.auth.basic uri.user, uri.password if uri.user && uri.password
  request.proxy = Sovaa.proxy if Sovaa.proxy
  request.body = doc if doc
  request.headers = {
    "Content-Type" => "application/json",
    "Accept" => "application/json"
  }.merge(headers)

  response = HTTPI.request(method, request, adapter)
  raise http_error(response) if response.error?
  response
end