class Plaza::RestfulAdapter

Attributes

logger[R]
request[R]

Public Class Methods

new(resource) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 5
def initialize(resource)
  @singular_name = resource.singular_name
  @plural_name   = resource.plural_name
  @request = Plaza::Request.new(resource.plaza_config)
  @logger  = Plaza.configuration(resource.plaza_config).logger
end

Public Instance Methods

create(data) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 26
def create(data)
  hash = handle_response(request.post(base_url, data))
  hash.fetch(@singular_name){hash}
end
delete(id) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 31
def delete(id)
  hash = handle_response(request.delete(resource_url(id)))
  hash.fetch(@singular_name){hash}
end
has_many(id, relation) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 36
def has_many(id, relation)
  hash = handle_response(request.get(has_many_url(id,relation)))
  hash.fetch(@singular_name){hash}
end
index(query_params = nil) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 12
def index(query_params = nil)
  handle_response(request.get(base_url(query_params)))
end
show(id) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 16
def show(id)
  hash = handle_response(request.get(resource_url(id)))
  hash.fetch(@singular_name){hash}
end
update(id, data) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 21
def update(id, data)
  hash = handle_response(request.put(resource_url(id), data))
  hash.fetch(@singular_name){hash}
end

Private Instance Methods

base_url(query_params = nil) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 42
def base_url(query_params = nil)
  url = "#{@plural_name}.json"
  url << "?#{URI.encode_www_form(query_params)}" if query_params
  url
end
has_many_url(id, relation) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 52
def has_many_url(id, relation)
  "#{resource_url(id).chomp('.json')}/#{relation}.json"
end
resource_url(id) click to toggle source
# File lib/plaza/adapters/restful_adapter.rb, line 48
def resource_url(id)
  "#{base_url.chomp('.json')}/#{id}.json"
end