Sha256: 6cb3dc64324752d57538a3d1d2c75592d8c5e5d448eff8616672049f199483ff

Contents?: true

Size: 1019 Bytes

Versions: 8

Compression:

Stored size: 1019 Bytes

Contents

module Reviewed
  class Request
    attr_accessor :client, :resource
    attr_reader :path

    def initialize(opts={})
      if opts[:resource].kind_of?(Class)
        @resource = opts[:resource]
        @path = @resource.path
      else
        @path = opts[:resource].to_s
      end

      @client = opts[:client] || Reviewed::Client.new
    end

    # Get request on resource#show
    def find(id, params={})
      object_from_response(:get, "#{path}/#{id}", params)
    end

    # Get request on resource#index with query params
    def where(params={})
      collection_from_response(:get, path, params)
    end

    # Convenience Method
    def all
      where({})
    end

    def object_from_response(method, url, params={})
      response = client.send(method, url, params)
      resource.new(response.body)
    end

    def collection_from_response(method, url, params={})
      response = client.send(method, url, params)
      Reviewed::Collection.new(client, resource, response, params)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reviewed-0.1.32 lib/reviewed/request.rb
reviewed-0.1.31 lib/reviewed/request.rb
reviewed-0.1.30 lib/reviewed/request.rb
reviewed-0.1.29 lib/reviewed/request.rb
reviewed-0.1.28 lib/reviewed/request.rb
reviewed-0.1.27 lib/reviewed/request.rb
reviewed-0.1.26 lib/reviewed/request.rb
reviewed-0.1.25 lib/reviewed/request.rb