Class: Gcp::Api::Discovery::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/gcp/api/discovery/watcher.rb,
lib/gcp/api/discovery/watcher/diff_hash.rb,
lib/gcp/api/discovery/watcher/api_metadata.rb

Defined Under Namespace

Modules: DiffHash Classes: ApiMetadata, Error

Instance Method Summary collapse

Instance Method Details

#current_filename(dir, api, version) ⇒ Object



49
50
51
52
# File 'lib/gcp/api/discovery/watcher.rb', line 49

def current_filename(dir, api, version)
  basedir = File.join(dir, api, version)
  File.join(basedir, "#{api}_#{version}.yaml")
end

#fetch_discover_api(api_name, version) ⇒ Object



15
16
17
18
19
# File 'lib/gcp/api/discovery/watcher.rb', line 15

def fetch_discover_api(api_name, version)
  url = "https://www.googleapis.com/discovery/v1/apis/#{api_name}/#{version}/rest"
  json = URI.open(url){|io| io.read }
  ApiMetadata.new(api_name, version, json)
end

#load_file(api_name, version, filepath) ⇒ Object



21
22
23
# File 'lib/gcp/api/discovery/watcher.rb', line 21

def load_file(api_name, version, filepath)
  ApiMetadata.new(api_name, version, File.read(filepath))
end

#metadata_filename(dir, metadata) ⇒ Object



54
55
56
# File 'lib/gcp/api/discovery/watcher.rb', line 54

def (dir, )
  File.join(dir, .api, .version, "#{.api}_#{.version}_#{.revision}.yaml")
end

#parse_options(argv, conf) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gcp/api/discovery/watcher.rb', line 30

def parse_options(argv, conf)
  OptionParser.new{|opt|
    opt.on("--api API_NAME", "GCP API Name (ex. ml, bigquery ...)") do |api|
      conf[:api] = api
    end
    opt.on("--api_version API_VERSION", "API version (ex. v1, v1beta1)") do |v|
      conf[:version] = v
    end
    opt.on("--storage DIR") do |dir|
      conf[:dir] = dir
    end
    opt.on("--output FILE") do |file|
      conf[:output_file] = file
    end
    opt.parse!
  }
  conf
end

#run(argv) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gcp/api/discovery/watcher.rb', line 58

def run(argv)
  conf = {
    dir: "./cache"
  }
  conf = parse_options(argv, conf)

  current_path = current_filename(conf[:dir], conf[:api], conf[:version])
  previous = nil
  if File.readable?(current_path)
    previous = load_file(conf[:api], conf[:version], current_path)
  end

   = fetch_discover_api(conf[:api], conf[:version])

  write_current = false
   = true
  if previous
    if previous.revision == .revision
      puts "API #{conf[:api]}_#{conf[:version]}: No Change (revision #{.revision})"
       = false
    else
      if previous.revision < .revision
        write_current = true
      end
      puts "API #{conf[:api]}_#{conf[:version]}: revision #{previous.revision} -> #{.revision}"
      if conf[:output_file].nil? or conf[:output_file] == "-"
        DiffHash.diff_hash(previous, , $stdout)
      else
        open(conf[:output_file], "wb") do |f|
          DiffHash.diff_hash(previous, , f)
        end
      end
    end
  else
    puts "New API #{conf[:api]}_#{conf[:version]}"
    write_current = true
  end

  if write_current
    save_file(current_path, )
  end
  if 
    save_file((conf[:dir], ), )
  end
end

#save_file(filepath, api_metadata) ⇒ Object



25
26
27
28
# File 'lib/gcp/api/discovery/watcher.rb', line 25

def save_file(filepath, )
  FileUtils.mkdir_p(File.dirname(filepath))
  File.write(filepath, YAML.dump(.to_hash))
end