Sha256: 6d46a4371defcf98845bdba8d7f00af2dc874d0bc92b4129473cb60979e86225

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

#!/usr/bin/env bundle exec ruby

require 'octokit'
require 'base64'
require 'json'
require 'yaml'
require 'erb'

class ApiMethodsGenerator
  def initialize(version = '6.2.3')
    @version = version

    sliced = @version.split(".").slice(0,2)
    @short_version = sliced.join(".")
    @class_version = sliced.join("_")
  end
  attr_reader :version, :short_version, :class_version

  def run
    api_spec = fetch_api_spec("v#{version}")
    File.open("lib/antbird/rest_api/rest_api_v#{class_version}.rb", "w") do |f|
      f.puts ERB.new(File.read('script/rest_api_template.erb'), 0, "<>").result(binding)
    end
  end

  def fetch_api_spec(ref)
    cache_file = "tmp/api_spec_#{ref}.yml"
    return YAML.load_file(cache_file) if File.exists?(cache_file)

    client = Octokit::Client.new(access_token: ENV['GITHUB_TOKEN'])
    files = client.contents('elastic/elasticsearch', path: 'rest-api-spec/src/main/resources/rest-api-spec/api', query: {ref: ref})
    api_spec = {}
    files.each do |f|
      puts f.url
      file = client.content('elastic/elasticsearch', path: f.path, query: { ref: ref })
      api_spec[File.basename(file.name)] = JSON.parse(Base64.decode64(file.content))
    end

    File.open(cache_file, 'w') { |f| f.write(api_spec.to_yaml) }
    api_spec
  end
end

travis_yaml = YAML.load_file('.travis.yml')
versions = travis_yaml['env'].map { |v| v.scan(/ES_VERSION=([\d\.]+)/) }.flatten.uniq
versions.each do |version|
  ApiMethodsGenerator.new(version).run
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
antbird-0.0.7 script/generate_api_methods
antbird-0.0.6 script/generate_api_methods
antbird-0.0.5 script/generate_api_methods
antbird-0.0.4 script/generate_api_methods
antbird-0.0.3 script/generate_api_methods
antbird-0.0.2 script/generate_api_methods