Class: Typekit::Client
- Inherits:
-
Object
- Object
- Typekit::Client
- Includes:
- HTTParty
- Defined in:
- lib/typekit/client.rb
Defined Under Namespace
Classes: APIError, ResourceDoesNotExistError, ServiceError
Class Method Summary collapse
-
.create_kit(params) ⇒ Object
Create a new kit.
-
.family(id) ⇒ Object
Retrieve a specific Family.
-
.family_by_name(name) ⇒ Object
Retrieve a Family by font family name.
-
.family_by_slug(slug) ⇒ Object
Retrieve a Family by Typekit slug.
-
.handle_response(response) ⇒ Object
Handle responses from HTTParty calls to the Typekit API with some generic response interpretation and manipulation.
-
.kit(id) ⇒ Object
Retrieve a specific kit.
-
.kits ⇒ Object
List kits available for this account.
-
.libraries ⇒ Object
Lists available libraries.
-
.library(id, params = {}) ⇒ Object
Retrieve a specific library.
-
.set_token(token) ⇒ Object
Set the Typekit API token to be used for subsequent calls.
Instance Method Summary collapse
-
#initialize(token) ⇒ Client
constructor
A new instance of Client.
-
#method_missing(method, *args, &block) ⇒ Object
Rather than making laborious calls to ‘Typekit::Client.kits` you can create an instance of the Client and call methods on it.
Constructor Details
#initialize(token) ⇒ Client
Returns a new instance of Client.
7 8 9 |
# File 'lib/typekit/client.rb', line 7 def initialize(token) set_token token end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Rather than making laborious calls to ‘Typekit::Client.kits` you can create an instance of the Typekit::Client and call methods on it. Instance methods will be defined for class methods on their first utilization.
17 18 19 20 21 22 23 24 25 |
# File 'lib/typekit/client.rb', line 17 def method_missing(method, *args, &block) super unless self.class.respond_to? method self.class.class_eval do define_method method do |*args, &block| self.class.send(method, *args, &block) end end self.class.send(method, *args, &block) end |
Class Method Details
.create_kit(params) ⇒ Object
Create a new kit
77 78 79 |
# File 'lib/typekit/client.rb', line 77 def create_kit(params) Kit.create(params) end |
.family(id) ⇒ Object
Retrieve a specific Family
96 97 98 |
# File 'lib/typekit/client.rb', line 96 def family(id) Family.find(id) end |
.family_by_name(name) ⇒ Object
Retrieve a Family by font family name
110 111 112 |
# File 'lib/typekit/client.rb', line 110 def family_by_name(name) Family.find_by_name(name) end |
.family_by_slug(slug) ⇒ Object
Retrieve a Family by Typekit slug
103 104 105 |
# File 'lib/typekit/client.rb', line 103 def family_by_slug(slug) Family.find_by_slug(slug) end |
.handle_response(response) ⇒ Object
Add individual errors for various HTTP Status codes
Handle responses from HTTParty calls to the Typekit API with some generic response interpretation and manipulation.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/typekit/client.rb', line 31 def handle_response(response) status = response.headers['status'].to_i case status when 404 then raise ResourceDoesNotExistError, response when 400..499 then raise APIError, response when 500..599 then raise ServiceError, response end response.values.first if response.values.any? end |
.kit(id) ⇒ Object
Retrieve a specific kit
71 72 73 |
# File 'lib/typekit/client.rb', line 71 def kit(id) Kit.find id end |
.kits ⇒ Object
List kits available for this account
65 66 67 |
# File 'lib/typekit/client.rb', line 65 def kits Kit.all end |
.libraries ⇒ Object
Lists available libraries
83 84 85 |
# File 'lib/typekit/client.rb', line 83 def libraries Library.all end |
.library(id, params = {}) ⇒ Object
Retrieve a specific library
89 90 91 |
# File 'lib/typekit/client.rb', line 89 def library(id, params = {}) Library.find(id, params) end |
.set_token(token) ⇒ Object
Work around the class variable limitation of HTTParty to allow use of the API with multiple tokens.
This is set to a class variable, so all instances of Typekit::Client will use the same API token. This is due to the way HTTParty works.
Set the Typekit API token to be used for subsequent calls.
59 60 61 |
# File 'lib/typekit/client.rb', line 59 def set_token(token) headers 'X-Typekit-Token' => token end |