Class: Zferral::Resource
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- Zferral::Resource
- 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.
Class Method Summary collapse
-
.api_token ⇒ String
The API token of the currently connected Client, or nil of no Client is connected.
-
.connect(client) ⇒ Class
Used to “connect” the resource to a Client already configured with credentials.
-
.fetch(id) ⇒ Resource
Fetches a single resource by ID.
-
.find(scope_or_id) ⇒ Array, Resource
Resource.find can be used with syntax like ActiveRecord to fetch lists or single resources.
-
.list ⇒ Array
(also: all)
Get an array of all members of the resource list.
-
.resource_name ⇒ String
The singular resource name (computed from the class name) to be used in resource URLs.
-
.resource_names ⇒ String
The plural resource name (computed from the class name) to be used in resource URLs.
-
.subdomain ⇒ String
The subdomain of the currently connected Client, or nil of no Client is connected.
Class Method Details
.api_token ⇒ String
Returns 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.
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
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
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 |
.list ⇒ Array Also known as: all
Get an array of all members of the resource list
47 48 49 |
# File 'lib/zferral/resource.rb', line 47 def self.list handle_list get("/#{resource_names}.json")["#{resource_names}"] end |
.resource_name ⇒ String
Returns 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_names ⇒ String
Returns 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 |
.subdomain ⇒ String
Returns 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 |