class Fontist::Import::Google::DataSources::Base
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.
Constants
- BASE_URL
Attributes
Public Class Methods
Source
# File lib/fontist/import/google/data_sources/base.rb, line 23 def initialize(api_key:, capability: nil) @api_key = api_key @capability = capability @cache = nil end
Public Instance Methods
Source
# File lib/fontist/import/google/data_sources/base.rb, line 83 def clear_cache @cache = nil end
Clear the internal cache
@return [nil]
Source
# File lib/fontist/import/google/data_sources/base.rb, line 32 def fetch return @cache if @cache raw_data = fetch_raw @cache = parse_response(raw_data) end
Fetch and parse font families from the API
@return [Array<FontFamily>] array of parsed font family models
Source
# File lib/fontist/import/google/data_sources/base.rb, line 54 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
Fetch raw JSON data from the API
@return [Hash] parsed JSON response @raise [RuntimeError] if the request fails
Source
# File lib/fontist/import/google/data_sources/base.rb, line 75 def parse_response(raw_data) items = raw_data["items"] || [] items.map { |item| parse_item(item) } end
Parse API response into FontFamily models
Subclasses should override this method to customize parsing
@param raw_data [Hash] the raw API response @return [Array<FontFamily>] array of font family models
Source
# File lib/fontist/import/google/data_sources/base.rb, line 42 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
Build the API URL with parameters
@return [String] the complete API URL
Protected Instance Methods
Source
# File lib/fontist/import/google/data_sources/base.rb, line 93 def parse_item(item) Models::FontFamily.from_json(item.to_json) end
Parse a single font item into a FontFamily model
@param item [Hash] the raw item data @return [FontFamily] the parsed font family