Module: Protip::Resource::SearchMethods

Defined in:
lib/protip/resource/search_methods.rb

Overview

Internal handlers for index/show actions. Never use these directly; instead, use ‘.all` and `.find` on the resource you’re working with, since those methods will adjust their signatures to correctly parse a set of query parameters if supported.

Class Method Summary collapse

Class Method Details

.index(resource_class, query) ⇒ Array

Fetch a list from the server at the collection’s base endpoint. Expects the server response to be an array containing encoded messages that can be used to instantiate our resource.

Parameters:

  • resource_class (Class)

    The resource type that we’re fetching.

  • query (::Protobuf::Message|NilClass)

    An optional query to send along with the request.

Returns:

  • (Array)

    The array of resources (each is an instance of the resource class we were initialized with).



14
15
16
17
18
19
20
21
22
# File 'lib/protip/resource/search_methods.rb', line 14

def self.index(resource_class, query)
  response = resource_class.client.request path: resource_class.base_path,
    method: Net::HTTP::Get,
    message: query,
    response_type: Protip::Messages::Array
  response.messages.map do |message|
    resource_class.new resource_class.message.decode(message)
  end
end

.show(resource_class, id, query) ⇒ Protip::Resource

Fetch a single resource from the server.

Parameters:

  • resource_class (Class)

    The resource type that we’re fetching.

  • id (String)

    The ID to be used in the URL to fetch the resource.

  • query (::Protobuf::Message|NilClass)

    An optional query to send along with the request.

Returns:

  • (Protip::Resource)

    An instance of our resource class, created from the server response.



31
32
33
34
35
36
37
# File 'lib/protip/resource/search_methods.rb', line 31

def self.show(resource_class, id, query)
  response = resource_class.client.request path: "#{resource_class.base_path}/#{id}",
    method: Net::HTTP::Get,
    message: query,
    response_type: resource_class.message
  resource_class.new response
end