class EzWadl::Resource

Attributes

httpmethods[RW]
parent[RW]
path[RW]
resources[RW]
xml[R]

Public Class Methods

new(xml) click to toggle source
# File lib/ezwadl/resource.rb, line 11
def initialize(xml)
  @xml = xml
  @path = (xml.attributes['base']&.value || xml.attributes['path'].value)
  @httpmethods = []
  @resources = []
end

Public Instance Methods

<<(resource) click to toggle source
# File lib/ezwadl/resource.rb, line 26
def <<(resource)
  resource.parent = self
  @resources << resource
end
method_missing(method, *args) click to toggle source
# File lib/ezwadl/resource.rb, line 31
def method_missing(method, *args)
  result = @resources.select {|r| symbolize(r.path) == method}[0]
  if !result && httpmethods.map(&:downcase).map(&:to_sym).include?(method)
     return Resource.send(method, uri_template(args.last[:query]), *args)
  end
  return result
end
paths() click to toggle source
# File lib/ezwadl/resource.rb, line 51
def paths
  @resources.map {|r| [r.path, symbolize(r.path)]}
end
respond_to_missing?(method, include_all = false) click to toggle source
# File lib/ezwadl/resource.rb, line 39
def respond_to_missing?(method, include_all = false)
  resource_paths = resources.map() {|r| symbolize(r.path)}
  httpmethods.map(&:downcase).map(&:to_sym).include?(method) || resource_paths.include?(method)
end
symbolize(str) click to toggle source
# File lib/ezwadl/resource.rb, line 44
def symbolize(str)
  str.tr('^a-zA-Z0-9_', '_')
     .squeeze('_')
     .gsub(/\A_|_\Z/, '')
     .to_sym
end
uri() click to toggle source
# File lib/ezwadl/resource.rb, line 18
def uri
  URI.join(parent&.uri.to_s || '', URI.escape(path + '/'))
end
uri_template(data) click to toggle source
# File lib/ezwadl/resource.rb, line 22
def uri_template(data)
  URI.unescape(uri.to_s).gsub(/{/, '%{') % data
end