Class: Typekit::Kit

Inherits:
Object
  • Object
show all
Includes:
MassAssignment
Defined in:
lib/typekit/kit.rb

Overview

TODO:

Get information for a specific family in the kit (/kits/:kit/families/:family)

Constant Summary collapse

@@defaults =
TODO:

Allow users to change defaults easily

{ :analytics => false, :badge => false }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MassAssignment

#initialize, #mass_assign

Instance Attribute Details

#analyticsObject

Detailed information about a kit. Lazy loaded when accessed unless the data already exists.

See Also:



9
10
11
# File 'lib/typekit/kit.rb', line 9

def analytics
  @analytics
end

#badgeObject

Detailed information about a kit. Lazy loaded when accessed unless the data already exists.

See Also:



9
10
11
# File 'lib/typekit/kit.rb', line 9

def badge
  @badge
end

#domainsObject

Detailed information about a kit. Lazy loaded when accessed unless the data already exists.

See Also:



9
10
11
# File 'lib/typekit/kit.rb', line 9

def domains
  @domains
end

#familiesObject

Detailed information about a kit. Lazy loaded when accessed unless the data already exists.

See Also:



9
10
11
# File 'lib/typekit/kit.rb', line 9

def families
  @families
end

#idObject

Typekit-defined kit id



12
13
14
# File 'lib/typekit/kit.rb', line 12

def id
  @id
end

#nameObject

Detailed information about a kit. Lazy loaded when accessed unless the data already exists.

See Also:



9
10
11
# File 'lib/typekit/kit.rb', line 9

def name
  @name
end

Class Method Details

.allObject

TODO:

Support pagination

Get a list of all of the kits available for this Typekit account



32
33
34
35
36
# File 'lib/typekit/kit.rb', line 32

def all
  Client.get('/kits').inject([]) do |kits, attributes|
    kits << Kit.new(attributes)
  end
end

.create(params) ⇒ Typekit::Kit

Create a new kit

Parameters:

  • params (Hash)

    Attributes for the newly create kit

Options Hash (params):

  • :name (String)

    Required: The name of the kit

  • :domains (Array)

    Required: An array of the domains that this kit will be used on

  • :analytics (Boolean) — default: false

    Allow Typekit to collect kit-usage data via Google Analytics

  • :badge (Boolean) — default: false

    Show the Typekit colophon badge on websites using this kit

Returns:



45
46
47
48
49
# File 'lib/typekit/kit.rb', line 45

def create(params)
  params = @@defaults.merge(params)
  response = Client.post("/kits", :query => params)
  Kit.new(response)
end

.find(id) ⇒ Object

Find a kit by id (not by name)

Parameters:

  • id (String)

    Typekit Kit ID (can be found via all)



24
25
26
27
28
# File 'lib/typekit/kit.rb', line 24

def find(id)
  kit = Kit.new(:id => id)
  kit.reload
  kit
end

Instance Method Details

#add_family(id, params = {}) ⇒ Boolean

Add a family to this kit (does not publish changes)

Parameters:

  • id (String)

    Typekit Font Family id (e.g. ‘brwr’)

  • params (Hash) (defaults to: {})

    Attributes for the family to be added

Options Hash (params):

  • :variations (Array) — default: []

    Font Variation Descriptions (‘n4’, ‘i7’, etc.) for the variations to be included

  • :subset (String) — default: 'default'

    Character subset to be served (‘all’ or ‘default’)

Returns:

  • (Boolean)

    True on success; error raised on failure



113
114
115
116
# File 'lib/typekit/kit.rb', line 113

def add_family(id, params = {})
  params = { :variations => [], :subset => 'default' }.merge(params)
  !!Client.post("/kits/#{@id}/families/#{id}", :query => params)
end

#deleteObject Also known as: destroy

Delete this kit from Typekit



102
103
104
# File 'lib/typekit/kit.rb', line 102

def delete
  Client.delete("/kits/#{@id}")
end

#delete_family(id) ⇒ Boolean Also known as: remove_family

Delete a family from this kit (does not publish changes)

Parameters:

  • id (String)

    Typekit Font Family id (e.g. ‘brwr’)

Returns:

  • (Boolean)

    True on success; error raised on failure



139
140
141
# File 'lib/typekit/kit.rb', line 139

def delete_family(id)
  !!Client.delete("/kits/#{@id}/families/#{id}")
end

#fetch(attribute = nil) ⇒ Object Also known as: reload

Note:

This is called lazily when you access any non-loaded attribute and doesn’t need to be called manually unless you want to reload the data. This means we can return an array of Kit objects for all without making N+1 requests to the API.

Get detailed information about this kit from Typekit

Parameters:

  • attribute (Symbol) (defaults to: nil)

    Optionally return a single attribute after data is loaded

Returns:

  • Returns @attribute if attribute argument is specified; otherwise returns self



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

def fetch(attribute = nil)
  mass_assign Client.get("/kits/#{@id}")
  attribute ? instance_variable_get("@#{attribute}") : self
end

#publishTime

Typekit maintains the changes you have made to a Kit in a “working” state until you specify that it is ready to be published. After the state has been changed to “published” your kit will be queued to be pushed out to their CDN and served to new requests. This can take up to 5 minutes when they are under heavy load.

Returns:

  • (Time)

    The date & time that the kit was last published



97
98
99
# File 'lib/typekit/kit.rb', line 97

def publish
  Client.post("/kits/#{@id}/publish")
end

#save(publish_after_save = true) ⇒ Boolean

Save kit attributes like name and domains. This does not alter the families added to the kit.

Parameters:

  • publish_after_save (Boolean) (defaults to: true)

    Commit changes saved to the published kit. See #publish.

Returns:

  • (Boolean)

    Status of the operation (including the publishing, if it is called)



81
82
83
84
85
86
87
88
89
# File 'lib/typekit/kit.rb', line 81

def save(publish_after_save = true)
  attributes = [:name, :analytics, :badge, :domains].inject({}) { |attributes, x| attributes[x] = instance_variable_get("@#{x}"); attributes }
  result = mass_assign Client.post("/kits/#{@id}", :query => attributes)
  published = publish if publish_after_save
  
  # For the parenthesized statement, true && true or false && false are acceptable.
  # but xor does the exact opposite, so we negate it.
  result && !(publish_after_save ^ published)
end

#update_family(id) {|family| ... } ⇒ Boolean

Update a family on this kit (does not publish changes)

Examples:

Updating a font family

Typekit::Kit.update_family('abcdef') do |family|
  family['subset'] = 'all'
  family['variations'] << 'i3'
end

Parameters:

  • id (String)

    Typekit Font Family id (e.g. ‘brwr’)

  • A (Block)

    block manipulating the family attributes

Yield Parameters:

  • family (Hash)

    The existing definition for this family

Returns:

  • (Boolean)

    True on success; error raised on failure



128
129
130
131
132
133
134
# File 'lib/typekit/kit.rb', line 128

def update_family(id)
  raise 'Block required' unless block_given?
  family = Client.get("/kits/#{@id}/families/#{id}")
  yield family
  family.keep_if { |k,v| %w{variations subset}.include? k }
  !!Client.post("/kits/#{@id}/families/#{id}", :query => family)
end