Class: Fontist::Import::Google::DataSources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/import/google/data_sources/base.rb

Overview

Base class for Google Fonts API data source clients

Provides common functionality for fetching data from the API, caching responses, and parsing JSON into FontFamily models.

Direct Known Subclasses

Ttf, Vf, Woff2

Constant Summary collapse

BASE_URL =
"https://www.googleapis.com/webfonts/v1/webfonts".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, capability: nil) ⇒ Base

Initialize a new API data source client

Parameters:

  • api_key (String)

    Google Fonts API key

  • capability (String, nil) (defaults to: nil)

    Optional capability parameter



22
23
24
25
26
# File 'lib/fontist/import/google/data_sources/base.rb', line 22

def initialize(api_key:, capability: nil)
  @api_key = api_key
  @capability = capability
  @cache = nil
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



16
17
18
# File 'lib/fontist/import/google/data_sources/base.rb', line 16

def api_key
  @api_key
end

#capabilityObject (readonly)

Returns the value of attribute capability.



16
17
18
# File 'lib/fontist/import/google/data_sources/base.rb', line 16

def capability
  @capability
end

Instance Method Details

#clear_cachenil

Clear the internal cache

Returns:

  • (nil)


82
83
84
# File 'lib/fontist/import/google/data_sources/base.rb', line 82

def clear_cache
  @cache = nil
end

#fetchArray<FontFamily>

Fetch and parse font families from the API

Returns:

  • (Array<FontFamily>)

    array of parsed font family models



31
32
33
34
35
36
# File 'lib/fontist/import/google/data_sources/base.rb', line 31

def fetch
  return @cache if @cache

  raw_data = fetch_raw
  @cache = parse_response(raw_data)
end

#fetch_rawHash

Fetch raw JSON data from the API

Returns:

  • (Hash)

    parsed JSON response

Raises:

  • (RuntimeError)

    if the request fails



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fontist/import/google/data_sources/base.rb', line 53

def fetch_raw
  uri = URI(url)
  response = Net::HTTP.get_response(uri)

  unless response.is_a?(Net::HTTPSuccess)
    raise "API request failed: #{response.code} #{response.message}"
  end

  JSON.parse(response.body)
rescue JSON::ParserError => e
  raise "Failed to parse API response: #{e.message}"
rescue StandardError => e
  raise "Failed to fetch from API: #{e.message}"
end

#parse_response(raw_data) ⇒ Array<FontFamily>

Parse API response into FontFamily models

Subclasses should override this method to customize parsing

Parameters:

  • raw_data (Hash)

    the raw API response

Returns:

  • (Array<FontFamily>)

    array of font family models



74
75
76
77
# File 'lib/fontist/import/google/data_sources/base.rb', line 74

def parse_response(raw_data)
  items = raw_data["items"] || []
  items.map { |item| parse_item(item) }
end

#urlString

Build the API URL with parameters

Returns:

  • (String)

    the complete API URL



41
42
43
44
45
46
47
# File 'lib/fontist/import/google/data_sources/base.rb', line 41

def url
  uri = URI(BASE_URL)
  params = { key: api_key }
  params[:capability] = capability if capability
  uri.query = URI.encode_www_form(params)
  uri.to_s
end