Class: RailsDesignProfiles::CatalogClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_design_profiles/catalog_client.rb

Constant Summary collapse

REPOSITORY =
"VoltAgent/awesome-design-md"
TREE_URL =
"https://api.github.com/repos/#{REPOSITORY}/git/trees/main?recursive=1"
RAW_URL =
"https://raw.githubusercontent.com/#{REPOSITORY}/main/design-md/%<slug>s/DESIGN.md"

Instance Method Summary collapse

Constructor Details

#initialize(http_get: nil) ⇒ CatalogClient

Returns a new instance of CatalogClient.



13
14
15
# File 'lib/rails_design_profiles/catalog_client.rb', line 13

def initialize(http_get: nil)
  @http_get = http_get || method(:get)
end

Instance Method Details

#download(slug) ⇒ Object



24
25
26
27
# File 'lib/rails_design_profiles/catalog_client.rb', line 24

def download(slug)
  validate_slug!(slug)
  @http_get.call(format(RAW_URL, slug: slug))
end

#slugsObject



17
18
19
20
21
22
# File 'lib/rails_design_profiles/catalog_client.rb', line 17

def slugs
  tree = JSON.parse(@http_get.call(TREE_URL)).fetch("tree")
  tree.filter_map { |entry| entry.fetch("path")[%r{\Adesign-md/([^/]+)/DESIGN\.md\z}, 1] }.sort
rescue JSON::ParserError, KeyError => error
  raise CatalogError, "Unexpected catalog response: #{error.message}"
end