Class: Fontist::Import::Google::DataSources::Base
- Inherits:
-
Object
- Object
- Fontist::Import::Google::DataSources::Base
- 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.
Constant Summary collapse
- BASE_URL =
"https://www.googleapis.com/webfonts/v1/webfonts".freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#capability ⇒ Object
readonly
Returns the value of attribute capability.
Instance Method Summary collapse
-
#clear_cache ⇒ nil
Clear the internal cache.
-
#fetch ⇒ Array<FontFamily>
Fetch and parse font families from the API.
-
#fetch_raw ⇒ Hash
Fetch raw JSON data from the API.
-
#initialize(api_key:, capability: nil) ⇒ Base
constructor
Initialize a new API data source client.
-
#parse_response(raw_data) ⇒ Array<FontFamily>
Parse API response into FontFamily models.
-
#url ⇒ String
Build the API URL with parameters.
Constructor Details
#initialize(api_key:, capability: nil) ⇒ Base
Initialize a new API data source client
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_key ⇒ Object (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 |
#capability ⇒ Object (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_cache ⇒ nil
Clear the internal cache
82 83 84 |
# File 'lib/fontist/import/google/data_sources/base.rb', line 82 def clear_cache @cache = nil end |
#fetch ⇒ Array<FontFamily>
Fetch and parse font families from the API
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_raw ⇒ Hash
Fetch raw JSON data from the API
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.}" end JSON.parse(response.body) rescue JSON::ParserError => e raise "Failed to parse API response: #{e.}" rescue StandardError => e raise "Failed to fetch from API: #{e.}" end |
#parse_response(raw_data) ⇒ Array<FontFamily>
Parse API response into FontFamily models
Subclasses should override this method to customize parsing
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 |
#url ⇒ String
Build the API URL with parameters
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 |