Sha256: ff77d97b4f8f6059ed99dbcb239859003dcf6286015c2a489e7dedfde21ec993

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

class Plaza::RestfulAdapter
  include Plaza::BaseAdapter
  attr_reader :request, :logger

  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

  def index(query_params = nil)
    handle_response(request.get(base_url(query_params)))
  end

  def show(id)
    hash = handle_response(request.get(resource_url(id)))
    hash.fetch(@singular_name){hash}
  end

  def update(id, data)
    hash = handle_response(request.put(resource_url(id), data))
    hash.fetch(@singular_name){hash}
  end

  def create(data)
    hash = handle_response(request.post(base_url, data))
    hash.fetch(@singular_name){hash}
  end

  def delete(id)
    hash = handle_response(request.delete(resource_url(id)))
    hash.fetch(@singular_name){hash}
  end

  def has_many(id, relation)
    hash = handle_response(request.get(has_many_url(id,relation)))
    hash.fetch(@singular_name){hash}
  end

  private
  def base_url(query_params = nil)
    url = "#{@plural_name}.json"
    url << "?#{URI.encode_www_form(query_params)}" if query_params
    url
  end

  def resource_url(id)
    "#{base_url.chomp('.json')}/#{id}.json"
  end

  def has_many_url(id, relation)
    "#{resource_url(id).chomp('.json')}/#{relation}.json"
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
plaza-0.1.1 lib/plaza/adapters/restful_adapter.rb
plaza-0.1.0 lib/plaza/adapters/restful_adapter.rb
plaza-0.0.4 lib/plaza/adapters/restful_adapter.rb