module Sentofu

Constants

USER_AGENT
VERSION

Attributes

apis[R]
auth_uri[R]
ssl_verify_mode[RW]
user_agent[RW]

Public Class Methods

credentials=(cs) click to toggle source
# File lib/sentofu.rb, line 44
def credentials=(cs)

  apis.each { |_, api| api.credentials = cs }
end
detail_apis() click to toggle source
# File lib/sentofu/explo.rb, line 24
def detail_apis

  Sentofu::Http.get_and_parse(
    'https://api.swaggerhub.com/apis/sentifi-api-docs/')
end
dump_apis() click to toggle source
# File lib/sentofu/explo.rb, line 31
def dump_apis

  puts

  list_apis.each do |h|

    n =
      case nn = h['n']
      when /auth/i then 'auth'
      when /ESG Score API/ then 'escore'
      else nn
      end

    fn = "api_#{n}_#{h['v']}.yaml"

    res = Sentofu::Http.get(h['u'] + '/swagger.yaml')
    File.open(fn, 'wb') { |f| f.write(res.body) }

    puts "  wrote #{fn}"
  end
end
init(versions=%w[ common:1.0.0 company:1.0.0 markets:1.0.0 ]) click to toggle source
# File lib/sentofu.rb, line 35
def init(versions=%w[ common:1.0.0 company:1.0.0 markets:1.0.0 ])

  if versions.is_a?(String) && File.directory?(versions)
    init_from_dir(versions)
  else
    init_from_swagger(versions)
  end
end
list_apis() click to toggle source
# File lib/sentofu/explo.rb, line 6
def list_apis

  detail_apis['apis']
    .collect { |meta|

      ps = meta['properties']

      u = ps.find { |p| p['type'] == 'Swagger' }['url']
      v = ps.find { |p| p['type'] == 'X-Version' }['value']

      m = u.match(/intelligence_([^_]+)_api/)
      n = m ? m[1] : meta['name']

      d = meta['description']

      { 'n' => n, 'v' => v, 'd' => d, 'u' => u } }
end

Protected Class Methods

init_api(name, spec) click to toggle source
# File lib/sentofu.rb, line 117
def init_api(name, spec)

  if name == 'auth'
    @auth_uri = spec['servers'][0]['url'] + spec['paths'].keys.first
  else
    api = Sentofu::Api.new(name, spec)
    Sentofu.define_singleton_method(name) { api }
    @apis[name] = api
  end
end
init_from_dir(dir) click to toggle source
# File lib/sentofu.rb, line 51
def init_from_dir(dir)

  paths = Dir[File.join(dir, 'api_*.yaml')]

  fail RuntimeError.new("no api yaml files under #{dir.inspect}") \
    if paths.empty?

  paths.each do |path|

    m = path.match(/api_([^_]+)_(\d+(?:\.\d+)*)\.yaml\z/)
    next unless m

    api_name = m[1]
    api_version = m[2]

    api_spec = YAML.load(File.read(path))

    api_spec[:meta] = {
      name: api_name,
      version: api_version,
      path: path,
      modified: File.mtime(path).utc.strftime('%FT%RZ') }

    init_api(api_name, api_spec)
  end
end
init_from_swagger(versions) click to toggle source
# File lib/sentofu.rb, line 78
def init_from_swagger(versions)

  vers = split_versions(versions)

  vers << %w[ auth * ] unless vers.find { |n, _| n == 'auth' }

  vers.each do |api_name, ver_pattern|

    doc_uri =
      'https://api.swaggerhub.com/apis/sentifi-api-docs' +
      (api_name == 'auth' ?
        '/sentifi-api_o_auth_2_authentication_and_authorization/' :
        "/sentifi-intelligence_#{api_name}_api/")

    metas = Sentofu::Http.get_and_parse(doc_uri)

    ver, mod, url, meta = metas['apis']
      .collect { |m|
        prs = m['properties']
        [ prs.find { |pr| pr['type'] == 'X-Version' }['value'],
          prs.find { |pr| pr['type'] == 'X-Modified' }['value'],
          prs.find { |pr| pr['type'] == 'Swagger' }['url'],
          m ] }
      .select { |v, _, _, _| version_match(v, ver_pattern) }
      .sort_by(&:first)
      .first

    meta[:name] = api_name
    meta[:version] = ver
    meta[:url] = url
    meta[:modified] = mod

    api_spec = Sentofu::Http.get_and_parse(url)
    api_spec[:meta] = meta

    init_api(api_name, api_spec)
  end
end
split_version(v) click to toggle source
# File lib/sentofu.rb, line 144
def split_version(v)

  v.is_a?(Array) ? v : v.split(':').collect(&:strip)
end
split_versions(vs) click to toggle source
# File lib/sentofu.rb, line 128
def split_versions(vs)

  case vs
  when Array
    vs
      .select { |v| v.strip.length > 0 }
      .collect { |v| split_version(v) }
  when String
    vs.split(/[,;]/)
      .select { |v| v.strip.length > 0 }
      .collect { |v| split_version(v) }
  else
    vs
  end
end
version_match(version, pattern) click to toggle source
# File lib/sentofu.rb, line 149
def version_match(version, pattern)

  ves = version.split('.')
  pattern.split('.').each do |pa|
    ve = ves.shift
    next if pa == 'x' || pa == '*'
    return false if ve != pa
  end

  true
end