class Sentofu::Resource
Constants
- TVP
Attributes
parent[R]
segment[R]
Public Class Methods
new(parent, segment)
click to toggle source
# File lib/sentofu/api.rb, line 10 def initialize(parent, segment) @parent = parent @segment = segment @children = {} end
Public Instance Methods
add_leaf_segment(segment, point)
click to toggle source
# File lib/sentofu/api.rb, line 37 def add_leaf_segment(segment, point) if m = segment.match(/\A\{([^}]+)\}\z/) define_singleton_method(:[]) { |index, query={}| fetch(segment, point, index, query) } else define_singleton_method(segment) { |query={}| fetch(segment, point, nil, query) } end end
add_segment(segment)
click to toggle source
# File lib/sentofu/api.rb, line 17 def add_segment(segment) m = segment.match(/\A\{[^}]+\}\z/) mth = m ? :[] : segment return @children[mth] if @children[mth] res = @children[mth] = Sentofu::Resource.new(self, segment) if mth == :[] define_singleton_method(:[]) { |i| Thread.current.thread_variable_set(TVP + segment, i); res } else define_singleton_method(mth) { res } end res end
Protected Instance Methods
api()
click to toggle source
# File lib/sentofu/api.rb, line 136 def api parent ? parent.api : self end
fetch(segment, point, index, query)
click to toggle source
# File lib/sentofu/api.rb, line 50 def fetch(segment, point, index, query) Thread.current.thread_variable_set(TVP + segment, index) if index q = rectify_query_parameters(point, query) pa = File.join(path, segment) pa = pa.gsub(/_/, '-') pa = pa + '?' + URI.encode_www_form(q) if q.any? return query.merge(path: pa) if query[:debug] get(pa) end
get(path)
click to toggle source
# File lib/sentofu/api.rb, line 66 def get(path) res = Sentofu::Http.get_and_parse(path, api.token) api.on_response(res) if api.respond_to?(:on_response) Sentofu.on_response(res) if Sentofu.respond_to?(:on_response) res end
path()
click to toggle source
# File lib/sentofu/api.rb, line 76 def path seg = segment[0, 1] == '{' ? Thread.current.thread_variable_get(TVP + segment).to_s : segment if parent File.join(parent.send(:path), seg) else seg end end
rectify_query_parameters(point, query)
click to toggle source
# File lib/sentofu/api.rb, line 90 def rectify_query_parameters(point, query) q = query .inject({}) { |h, (k, v)| next h if k == :debug h[k.to_s.gsub(/_/, '-')] = case v when Symbol then v.to_s when Array then v.collect(&:to_s).join(',') #when Time, Date then v.utc.strftime('%F') when Time, Date then v.strftime('%F') else v end h } (point['get']['parameters'] || []) .each { |par| next if par['in'] != 'query' nam = par['name'] key = nam.gsub(/-/, '_') fail ArgumentError.new( "missing query parameter :#{key}" ) if par['required'] == true && !q.has_key?(nam) v = q[nam] typ = par['schema']['type'] fail ArgumentError.new( "argument to :#{key} not an integer" ) if v && typ == 'integer' && ! v.is_a?(Integer) fail ArgumentError.new( "argument to :#{key} not a string (or a symbol)" ) if v && typ == 'string' && ! v.is_a?(String) enu = par['schema']['enum'] fail ArgumentError.new( "value #{v.inspect} for :#{key} not present in #{enu.inspect}" ) if v && enu && ! enu.include?(v) } q end