Class: Develoz::CanonicalFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/develoz/canonical_fetcher.rb

Constant Summary collapse

GITHUB_API_BASE =
"https://api.github.com"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetch(repo:, path:, dest:) ⇒ Object



12
13
14
# File 'lib/develoz/canonical_fetcher.rb', line 12

def self.fetch(repo:, path:, dest:)
  new.fetch(repo: repo, path: path, dest: dest)
end

Instance Method Details

#fetch(repo:, path:, dest:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/develoz/canonical_fetcher.rb', line 16

def fetch(repo:, path:, dest:)
  url = "#{GITHUB_API_BASE}/repos/#{repo}/contents/#{path}"
  token = fetch_token

  response = make_request(url, token)
  raise "Failed to fetch #{repo}/#{path}: #{response.code}" unless response.code == "200"

  content = JSON.parse(response.body)["content"]
  decoded = Base64.decode64(content)

  FileUtils.mkdir_p(File.dirname(dest))
  File.write(dest, decoded)
end