-
Notifications
You must be signed in to change notification settings - Fork 0
/
formula.rb
51 lines (39 loc) · 1.2 KB
/
formula.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
require "formula"
##################
# Mock Formula API
##################
# Generate the API JSON locally and mock the API loader to user that generated JSON.
# Add constant to module to avoid name conflicts.
module HDUtils
module StubAPI
module Formula
abort "The core formula tap needs to be installed locally to mock the API!" unless CoreTap.instance.installed?
NAMES = CoreTap.instance.formula_names.freeze
print "Generating formulae API ..."
# Generate json representation of all formulas.
::Formula.generating_hash!
JSON = NAMES.to_h do |formula_name|
formula = Formulary.factory(formula_name)
json = JSON.generate(formula.to_api_hash)
hash = JSON.parse(json)
[hash["name"], hash.except("name")]
end.freeze
::Formula.generated_hash!
private_constant :NAMES, :JSON
def self.names
NAMES
end
def self.json
JSON
end
def self.load_from_api(name)
Formulary::FormulaAPILoader
.new(name)
.get_formula(:stable)
end
end
end
end
warn " and mocking formula API loader."
require_relative "../extend/stub-api/formula"