Class: Mxrb::Marketplace::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/marketplace.rb

Constant Summary collapse

DEFAULT_PATH =
File.expand_path("../../marketplace/catalog.json", __dir__)

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ Catalog

Returns a new instance of Catalog.



20
21
22
# File 'lib/mxrb/marketplace.rb', line 20

def initialize(source = nil)
  @source = source || DEFAULT_PATH
end

Instance Method Details

#entriesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mxrb/marketplace.rb', line 24

def entries
  @entries ||= begin
    payload = JSON.parse(read_source)
    Array(payload.fetch("modules")).map do |item|
      Entry.new(
        item.fetch("name"), item.fetch("version"),
        item.fetch("description", ""), item.fetch("source"),
        item["ref"]
      )
    end.freeze
  rescue JSON::ParserError, KeyError => e
    raise MarketplaceError, "invalid marketplace catalog: #{e.message}"
  end
end

#find(name) ⇒ Object



48
49
50
51
# File 'lib/mxrb/marketplace.rb', line 48

def find(name)
  entries.find { _1.name == name.to_s } ||
    raise(MarketplaceError, "module #{name.inspect} was not found in the catalog")
end

#find_version(name, version) ⇒ Object



53
54
55
56
# File 'lib/mxrb/marketplace.rb', line 53

def find_version(name, version)
  entries.find { _1.name == name.to_s && _1.version == version.to_s } ||
    raise(MarketplaceError, "module #{name.inspect} version #{version.inspect} was not found in the catalog")
end

#search(query = nil) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/mxrb/marketplace.rb', line 39

def search(query = nil)
  term = query.to_s.downcase
  return entries if term.empty?

  entries.select do |entry|
    [entry.name, entry.description].any? { _1.downcase.include?(term) }
  end
end