Module: TypekitClient::TypekitAPI

Defined in:
lib/typekit_client/typekit_api.rb

Class Method Summary collapse

Class Method Details

.add_family(kit_id, family_id, options) ⇒ Object

Public: This helper method makes http request to add new font family to kit

kit_id- The kit id to add the font family family_id- The font family id to be added options- The options hash containing user inputs

Example:

add_family('bsz4xuy', 'myvg', {format: 'json', subset: 'default'})

If it is a success, it prints the newly added font family details based on the specified format If it is a failure, it prints out error status code and message



123
124
125
126
127
128
129
130
131
# File 'lib/typekit_client/typekit_api.rb', line 123

def self.add_family(kit_id, family_id, options)

  body = { subset: options[:subset] }
  body[:variations] = options[:variations] if options[:variations]

  uri_string = "kits/#{kit_id}/families/#{family_id}"
  response = TypekitClient::HttpRequest.request(uri_string, :POST, options[:format], body)
  print_response(response)
end

.create_kit(options) ⇒ Object

Public: This helper method makes http request to create a new kit

options- The options hash containing user inputs

Example:

create_kit({format: 'json', name: 'test'})

If it is a success, it prints the newly created kit details based on the specified format If it is a failure, it prints out error status code and message



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/typekit_client/typekit_api.rb', line 55

def self.create_kit(options)

  body = {
    name: options[:name],
    domains: options[:domains],
    segmented_css_names: options[:segmented_css_names]
  }

  body[:families] = parse_families(options[:families]) if options[:families]

  uri_string = 'kits'
  response = TypekitClient::HttpRequest.request(uri_string, :POST, options[:format], body)
  print_response(response)
end

.delete_family(kit_id, family_id, format) ⇒ Object

Public: This helper method makes http request to delete font family from kit

kit_id- The kit id to delete the family from family_id- The font family id in the kit format- The user requested format, set to JSON by default

Example:

delete_family('bsz4xuy', 'myvg', 'json')

If it is a success, it prints success message If it is a failure, it prints out error status code and message



186
187
188
189
190
191
# File 'lib/typekit_client/typekit_api.rb', line 186

def self.delete_family(kit_id, family_id, format)
  uri_string = "kits/#{kit_id}/families/#{family_id}"
  response = TypekitClient::HttpRequest.request(uri_string, :DELETE, format)

  print_response(response)
end

.delete_kit(kit_id, format) ⇒ Object

Public: This helper method makes http request and deletes the kit for the given kit id

kit_id- The id of the kit to delete format- The user requested format, set to JSON by default

Example:

delete_kit('bsz4xuy', 'json')

If it is a success, it prints the success message If it is a failure, it prints out error status code and message



82
83
84
85
86
87
# File 'lib/typekit_client/typekit_api.rb', line 82

def self.delete_kit(kit_id, format)
  uri_string = "kits/#{kit_id}"
  response = TypekitClient::HttpRequest.request(uri_string, :DELETE, format)

  print_response(response)
end

.family(kit_id, family_id, format) ⇒ Object

Public: This helper method makes http request and prints out the font family details for the given font id and the family id

kit_id- The kit id to find the details for family_id- The font family id for the kit format- The user requested format, set to JSON by default

Example:

family('bsz4xuy', 'myvg', 'json')

If it is a success, it prints the font family details based on the specified format If it is a failure, it prints out error status code and message



103
104
105
106
107
108
# File 'lib/typekit_client/typekit_api.rb', line 103

def self.family(kit_id, family_id, format)
  uri_string = "kits/#{kit_id}/families/#{family_id}"
  response = TypekitClient::HttpRequest.request(uri_string, :GET, format)

  print_response(response)
end

.kit(kit_id, format) ⇒ Object

Public: This helper method makes http request and prints out the draft version of the kit for the given kit id

kit_id- The kit id to find the details for format- The user requested format, set to JSON by default

Example:

kit('bsz4xuy', 'json')

If it is a success, it prints the kit details based on the specified format If it is a failure, it prints out error status code and message



37
38
39
40
41
42
# File 'lib/typekit_client/typekit_api.rb', line 37

def self.kit(kit_id, format)
  uri_string = "kits/#{kit_id}"
  response = TypekitClient::HttpRequest.request(uri_string, :GET, format)

  print_response(response)
end

.kits(format) ⇒ Object

Public: This helper method makes http request and prints out the list of kits

format- The user requested format, set to JSON by default

Example:

kits('json')

If it is a success, it prints the response body based on the specified format If it is a failure, it prints out error status code and message



18
19
20
21
22
23
# File 'lib/typekit_client/typekit_api.rb', line 18

def self.kits(format)
  uri_string = 'kits'
  response = TypekitClient::HttpRequest.request(uri_string, :GET, format)

  print_response(response)
end

.parse_families(families) ⇒ Object

Private: This helper method parses families input string and converts it to a hash

families- The user input families string in the format ‘id:abcd,variations:n2;id:2,variations:i7’

Example:

parse_families('id:abcd,variations:n2;id:2,variations:i7')
=> {"0"=>{"id"=>"1", "variations"=>"n2"}, "1"=>{"id"=>"2", "variations"=>"i7"}}

Returns equivalent hash for the string



233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/typekit_client/typekit_api.rb', line 233

def self.parse_families(families)

  family_list = families.split(';')
  family_hash = {}

  family_list.each_with_index do |family, index|
    family_hash[index.to_s] = family.split(',').each_with_object({}) { |str, attribute_hash|
      tmp = str.split(':')
      attribute_hash[tmp[0]] = tmp[1] if %w(id variations subset).include? tmp[0]
    }
  end

  family_hash
end

Private: This helper method prints out the response body based on the response code

response- The http response object from the http request call

Example:

print_response(Net::HTTPResponse)

If the status code is 200, it prints out the response body based on the content-type If it is a failure, it prints out error status code and message



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/typekit_client/typekit_api.rb', line 207

def self.print_response(response)
  if response.code == '200'
    if response.body.empty?
      puts 'The command ran successfully'
    end
    if response['content-type'] == 'application/json; charset=utf-8'
      puts JSON.pretty_generate(JSON.parse(response.body))
    else
      puts response.body
    end
  else
    puts "The following error occurred while running the command:#{response.code}, #{JSON.parse(response.body)['errors']}".red
  end
end

.publish_kit(kit_id, format) ⇒ Object

Public: This helper method makes http request to publish the draft version of the kit

kit_id- The id of the kit to publish format- The user requested format, set to JSON by default

Example:

publish_kit('bsz4xuy', 'json')

If it is a success, it prints the published date in specified format If it is a failure, it prints out error status code and message



145
146
147
148
149
150
# File 'lib/typekit_client/typekit_api.rb', line 145

def self.publish_kit(kit_id, format)
  uri_string = "kits/#{kit_id}/publish"
  response = TypekitClient::HttpRequest.request(uri_string, :POST, format)

  print_response(response)
end

.published_kit(kit_id, format) ⇒ Object

Public: This helper method makes http request and prints out the published version of kit for the given kit id If token is not provided, it will display only public info pass-in token in the options hash to get all info with proper authentication

kit_id- The kit id to find the details for format- The user requested format, set to JSON by default

Example:

published_kit('bsz4xuy', 'json')

If it is a success, it prints the kit details based on the specified format If it is a failure, it prints out error status code and message



166
167
168
169
170
171
# File 'lib/typekit_client/typekit_api.rb', line 166

def self.published_kit(kit_id, format)
  uri_string = "kits/#{kit_id}/published"
  response = TypekitClient::HttpRequest.request(uri_string, :GET, format)

  print_response(response)
end