Class: Zferral::Resource

Inherits:
Hashie::Mash
  • Object
show all
Includes:
HTTParty
Defined in:
lib/zferral/resource.rb

Overview

The “base” resource from which all of the Zferral API resources subclass. This class typically isn’t used directly; instead, you’ll use the subclasses like Campaign.

Direct Known Subclasses

Campaign, Event

Class Method Summary collapse

Class Method Details

.api_tokenString

Returns The API token of the currently connected Client, or nil of no Client is connected.

Returns:

  • (String)

    The API token of the currently connected Client, or nil of no Client is connected.



30
31
32
# File 'lib/zferral/resource.rb', line 30

def self.api_token
  @@client.api_token if @@client
end

.connect(client) ⇒ Class

Used to “connect” the resource to a Client already configured with credentials.

Upon connection, the passed in Client is stored (see .client), the base_uri for HTTParty interaction is calculated, and the class is returned.

Parameters:

  • client (Client)

    instance with credentials (subdomain and api_token attached)

Returns:

  • (Class)


18
19
20
21
22
# File 'lib/zferral/resource.rb', line 18

def self.connect(client)
  @@client = client
  self.base_uri("https://#{subdomain}.zferral.com/api/#{api_token}")
  self
end

.fetch(id) ⇒ Resource

Fetches a single resource by ID

Returns:

  • (Resource)

    A single Resource object, or one of its subclasses

Raises:



61
62
63
64
# File 'lib/zferral/resource.rb', line 61

def self.fetch(id)
  fetch_id(id)
  handle_fetch get("/#{resource_name}/#{id}.json").parsed_response
end

.find(scope_or_id) ⇒ Array, Resource

find can be used with syntax like ActiveRecord to fetch lists or single resources.

To fetch a list, use the :all parameter:

Resource.find(:all)

To fetch a single Resource, pass an Integer ID

Parameters:

  • +:all+ (Symbol, Integer)

    to fetch a list, an Integer to fetch a single Resource

Returns:

  • (Array, Resource)

    An Array of Resources or a single Resource

Raises:



79
80
81
82
83
84
85
86
87
88
# File 'lib/zferral/resource.rb', line 79

def self.find(scope_or_id)
  case scope_or_id
  when Symbol
    if scope_or_id == :all
      list
    end
  when Integer, String
    fetch(scope_or_id.to_i)
  end
end

.listArray Also known as: all

Get an array of all members of the resource list

Returns:

  • (Array)

    An array of the expected resource



47
48
49
# File 'lib/zferral/resource.rb', line 47

def self.list
  handle_list get("/#{resource_names}.json")["#{resource_names}"]
end

.resource_nameString

Returns The singular resource name (computed from the class name) to be used in resource URLs.

Returns:

  • (String)

    The singular resource name (computed from the class name) to be used in resource URLs



35
36
37
# File 'lib/zferral/resource.rb', line 35

def self.resource_name
  name.split("::").last.downcase
end

.resource_namesString

Returns The plural resource name (computed from the class name) to be used in resource URLs.

Returns:

  • (String)

    The plural resource name (computed from the class name) to be used in resource URLs



40
41
42
# File 'lib/zferral/resource.rb', line 40

def self.resource_names
  name.split("::").last.downcase + "s"
end

.subdomainString

Returns The subdomain of the currently connected Client, or nil of no Client is connected.

Returns:

  • (String)

    The subdomain of the currently connected Client, or nil of no Client is connected.



25
26
27
# File 'lib/zferral/resource.rb', line 25

def self.subdomain
  @@client.subdomain if @@client
end