Class: CaptiveStackDetector::GithubApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/captive_stack_detector/github_api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, repo) ⇒ GithubApiClient

Returns a new instance of GithubApiClient.



10
11
12
13
# File 'lib/captive_stack_detector/github_api_client.rb', line 10

def initialize(token, repo)
  @token = token
  @repo  = repo
end

Instance Method Details

#fetch(filename) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/captive_stack_detector/github_api_client.rb', line 15

def fetch(filename)
  uri = URI("https://api.github.com/repos/#{@repo}/contents/#{filename}")
  req = Net::HTTP::Get.new(uri)
  req["Authorization"] = "Bearer #{@token}"
  req["Accept"]        = "application/vnd.github+json"

  res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
  return nil unless res.is_a?(Net::HTTPSuccess)

  Base64.decode64(JSON.parse(res.body)["content"]).force_encoding("utf-8")
rescue StandardError
  nil
end