Class: Typekit::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/typekit/client.rb

Defined Under Namespace

Classes: APIError, ResourceDoesNotExistError, ServiceError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Client

Returns a new instance of Client.

Parameters:

  • token (String)

    Your Typekit API token



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.

Examples:

typekit = Typekit::Client.new('your token here')
typekit.kits    #=> [...]


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

See Also:



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

Parameters:

  • id (String)

    The Typekit Family id (e.g. ‘brwr’ or ‘gkmg’)

See Also:



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

Parameters:

  • name (String)

    The name of the font family without variation (e.g. ‘FF Meta Web Pro’ or ‘Droid Sans’)

See Also:



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

Parameters:

  • slug (String)

    The Typekit Family slug for the font family (e.g. ‘ff-meta-web-pro’ or ‘droid-sans’)

See Also:



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

TODO:

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

See Also:



71
72
73
# File 'lib/typekit/client.rb', line 71

def kit(id)
  Kit.find id
end

.kitsObject

List kits available for this account

See Also:



65
66
67
# File 'lib/typekit/client.rb', line 65

def kits
  Kit.all
end

.librariesObject

Lists available libraries

See Also:



83
84
85
# File 'lib/typekit/client.rb', line 83

def libraries
  Library.all
end

.library(id, params = {}) ⇒ Object

Retrieve a specific library

See Also:



89
90
91
# File 'lib/typekit/client.rb', line 89

def library(id, params = {})
  Library.find(id, params)
end

.set_token(token) ⇒ Object

TODO:

Work around the class variable limitation of HTTParty to allow use of the API with multiple tokens.

Note:

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.

Parameters:

  • token (String)

    Your Typekit API token



59
60
61
# File 'lib/typekit/client.rb', line 59

def set_token(token)
  headers 'X-Typekit-Token' => token
end