class CircleciCrawler::Crawler

Public Class Methods

new(api_token:, project_path:, path:) click to toggle source
# File lib/circleci_crawler.rb, line 8
def initialize(api_token:, project_path:, path:)
  @api_token = api_token
  @project_path = project_path
  @path = path
end

Public Instance Methods

crawl() click to toggle source
# File lib/circleci_crawler.rb, line 14
def crawl
  results = request("https://circleci.com/api/v1.1/project/#{@project_path}/latest/artifacts?circle-token=#{@api_token}")
  target = results.find { |result| result[:path] == @path }

  request("#{target[:url]}?circle-token=#{@api_token}", should_parse_to_json: false)
end

Private Instance Methods

request(url, should_parse_to_json: true) click to toggle source
# File lib/circleci_crawler.rb, line 23
def request(url, should_parse_to_json: true)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  req = Net::HTTP::Get.new(uri.to_s)
  res = http.request(req)

  raise "Failed to fetch #{uri}" unless res.is_a?(Net::HTTPSuccess)

  if should_parse_to_json
    JSON.parse(res.body, symbolize_names: true)
  else
    res.body
  end
end