class Slipcover::HttpAdapter

Public Instance Methods

delete(url, data={}) click to toggle source
# File lib/slipcover/http_adapter.rb, line 27
def delete(url, data={})
  try {
    parse( RestClient.delete(url + query_string(data), headers) )
  }
end
error_class(e) click to toggle source
# 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={}) click to toggle source
# File lib/slipcover/http_adapter.rb, line 9
def get(url, data={})
  try {
    parse( RestClient.get(url + query_string(data), headers) )
  }
end
head(url, data={}) click to toggle source
# File lib/slipcover/http_adapter.rb, line 3
def head(url, data={})
  try {
    parse( RestClient.head(url + query_string(data), headers) )
  }
end
headers() click to toggle source
# File lib/slipcover/http_adapter.rb, line 95
def headers
  {:content_type => :json, :accept => :json}
end
parse(response) click to toggle source
# 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={}) click to toggle source
# File lib/slipcover/http_adapter.rb, line 15
def post(url, data={})
  try {
    parse( RestClient.post(url, data.to_json, headers) )
  }
end
put(url, data={}) click to toggle source
# File lib/slipcover/http_adapter.rb, line 21
def put(url, data={})
  try {
    parse( RestClient.put(url, data.to_json, headers) )
  }
end
query_string(data) click to toggle source
# 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) click to toggle source
# File lib/slipcover/http_adapter.rb, line 54
def reraise(e)
  # As we keep doing more stuff with couchdb we might want to update this list
  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() { || ... } click to toggle source
# File lib/slipcover/http_adapter.rb, line 48
def try
  yield
rescue Exception => e
  reraise(e)
end