Module: Sova::HTTP

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



22
23
24
# File 'lib/sova/http.rb', line 22

def adapter
  @adapter
end

Instance Method Details

#copy(uri, destination) ⇒ Object



63
64
65
66
67
# File 'lib/sova/http.rb', line 63

def copy(uri, destination) 
  headers = {'X-HTTP-Method-Override' => 'COPY', 'Destination' => destination}
  response = request(:post, uri, nil, headers)
  JSON.parse(response.body, :max_nesting => false)
end

#delete(uri) ⇒ Object



58
59
60
61
# File 'lib/sova/http.rb', line 58

def delete(uri)
  response = request(:delete, uri)
  JSON.parse(response.body, :max_nesting => false)
end

#get(uri) ⇒ Object



47
48
49
50
# File 'lib/sova/http.rb', line 47

def get(uri)
  response = request(:get, uri)
  JSON.parse(response.body, :max_nesting => false)
end

#post(uri, doc = nil) ⇒ Object



52
53
54
55
56
# File 'lib/sova/http.rb', line 52

def post(uri, doc=nil)
  doc = doc.to_json if doc
  response = request(:post, uri, doc)
  JSON.parse(response.body, :max_nesting => false)
end

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



41
42
43
44
45
# File 'lib/sova/http.rb', line 41

def put(uri, doc=nil, headers={})
  doc = doc.to_json if doc
  response = request(:put, uri, doc, headers)
  JSON.parse(response.body, :max_nesting => false)
end

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



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

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 = Sova.proxy if Sova.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