Class: Slipcover::HttpAdapter
- Inherits:
-
Object
- Object
- Slipcover::HttpAdapter
show all
- Defined in:
- lib/slipcover/http_adapter.rb
Defined Under Namespace
Classes: ConflictError, DBNotFound, DocumentNotFound, NamedViewNotFound, NotFound, ParseError
Instance Method Summary
collapse
Instance Method Details
#delete(url, data = {}) ⇒ Object
27
28
29
30
31
|
# File 'lib/slipcover/http_adapter.rb', line 27
def delete(url, data={})
try {
parse( RestClient.delete(url + query_string(data), ) )
}
end
|
#error_class(e) ⇒ Object
67
68
69
70
71
72
73
74
75
|
# File 'lib/slipcover/http_adapter.rb', line 67
def error_class(e)
{
JSON::ParserError => ParseError,
RestClient::ResourceNotFound => NotFound,
RestClient::PreconditionFailed => ConflictError,
RestClient::Conflict => ConflictError
}[e.class] || e.class
end
|
#get(url, data = {}) ⇒ Object
9
10
11
12
13
|
# File 'lib/slipcover/http_adapter.rb', line 9
def get(url, data={})
try {
parse( RestClient.get(url + query_string(data), ) )
}
end
|
#head(url, data = {}) ⇒ Object
3
4
5
6
7
|
# File 'lib/slipcover/http_adapter.rb', line 3
def head(url, data={})
try {
parse( RestClient.head(url + query_string(data), ) )
}
end
|
95
96
97
|
# File 'lib/slipcover/http_adapter.rb', line 95
def
{:content_type => :json, :accept => :json}
end
|
#parse(response) ⇒ Object
43
44
45
46
|
# File 'lib/slipcover/http_adapter.rb', line 43
def parse(response)
parsed = JSON.parse(response)
parsed.is_a?(Hash) ? parsed.symbolize_keys : parsed
end
|
#post(url, data = {}) ⇒ Object
15
16
17
18
19
|
# File 'lib/slipcover/http_adapter.rb', line 15
def post(url, data={})
try {
parse( RestClient.post(url, data.to_json, ) )
}
end
|
#put(url, data = {}) ⇒ Object
21
22
23
24
25
|
# File 'lib/slipcover/http_adapter.rb', line 21
def put(url, data={})
try {
parse( RestClient.put(url, data.to_json, ) )
}
end
|
#query_string(data) ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/slipcover/http_adapter.rb', line 33
def query_string(data)
return "" if data.empty?
query = data.map do |key, value|
"#{key}=#{value}"
end.join("&")
"?#{query}"
end
|
#reraise(e) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/slipcover/http_adapter.rb', line 54
def reraise(e)
response = JSON.parse(e.response) rescue Hash.new
case response["reason"]
when "no_db_file" then raise DBNotFound, e.response
when "Database does not exist." then raise DBNotFound, e.response
when "missing_named_view" then raise NamedViewNotFound, e.response
when "missing" then raise DocumentNotFound, e.response
else
raise error_class(e).new(response.empty? ? e.message : e.response)
end
end
|
#try ⇒ Object
48
49
50
51
52
|
# File 'lib/slipcover/http_adapter.rb', line 48
def try
yield
rescue Exception => e
reraise(e)
end
|