Class: Lsaws::SDKParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lsaws/sdk_parser.rb

Constant Summary collapse

IGNORED_SDKS =
[
  "core", "resources", # do not contain any resource listing methods
  "s3control" # requires account_id param for all requests
].freeze
LIST_METHOD_PREFIXES =

order is important!

%w[list describe get].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdk) ⇒ SDKParser

Returns a new instance of SDKParser.



23
24
25
26
# File 'lib/lsaws/sdk_parser.rb', line 23

def initialize(sdk)
  @sdk = sdk
  require "aws-sdk-#{sdk}"
end

Class Method Details

.get_sdksObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lsaws/sdk_parser.rb', line 10

def self.get_sdks
  r = []
  Gem.path.each do |p|
    next unless Dir.exist?(p)

    r.append(*Dir[File.join(p, "gems/aws-sdk-*")].map do |gem_dir|
      a = File.basename(gem_dir).split("-")
      a.size == 4 ? a[2] : nil
    end)
  end
  r.compact.uniq.sort - IGNORED_SDKS
end

Instance Method Details

#client_classObject



37
38
39
# File 'lib/lsaws/sdk_parser.rb', line 37

def client_class
  @client_class ||= Kernel.const_get(client_class_name)
end

#client_class_nameObject



28
29
30
31
32
33
34
35
# File 'lib/lsaws/sdk_parser.rb', line 28

def client_class_name
  @client_class_name ||=
    begin
      # TODO: use constants from gems/aws-sdk-resources-x.xxx
      c = Aws.constants.find { |x| x.to_s.downcase == @sdk }
      "Aws::#{c}::Client"
    end
end

#entity_typesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lsaws/sdk_parser.rb', line 56

def entity_types
  methods = client_class
            .instance_methods
            .find_all { |m| m =~ /^(?:#{LIST_METHOD_PREFIXES.join("|")})_.+s$/ && m !~ /(?:status|access)$/ }

  return [] if methods.empty?

  methods.delete_if do |m|
    rdoc = get_method_rdoc(m)
    next(true) unless rdoc

    required_params = rdoc.scan(/^\s+# @option params \[required, (.+?)\] :(\w+)/)
    required_params.any? || Lsaws.config.dig(@sdk, method2etype(m), "required_params")
  end

  methods.map { |m| method2etype(m) }.uniq.sort
end

#etype2method(etype) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/lsaws/sdk_parser.rb', line 44

def etype2method(etype)
  LIST_METHOD_PREFIXES.each do |prefix|
    m = "#{prefix}_#{etype}"
    return m if client_class.public_method_defined?(m)
  end
  nil
end

#get_method_api(method) ⇒ Object



89
90
91
92
# File 'lib/lsaws/sdk_parser.rb', line 89

def get_method_api(method)
  # calling private API!
  client_class.api.operation(method)
end

#get_method_rdoc(method) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lsaws/sdk_parser.rb', line 74

def get_method_rdoc(method)
  @source ||= File.read(client_class.instance_method(method).source_location[0])

  pos = @source =~ /^\s+def\s+#{method}\s*\(/
  return nil unless pos

  chunk = ""
  bs = 4096
  until chunk["\n\n"]
    chunk = @source[pos - bs..pos]
    bs *= 2
  end
  chunk[chunk.rindex("\n\n") + 2..]
end

#method2etype(method) ⇒ Object



52
53
54
# File 'lib/lsaws/sdk_parser.rb', line 52

def method2etype(method)
  method.to_s.sub(/^(?:#{LIST_METHOD_PREFIXES.join("|")})_/, "")
end