Class: EzWadl::Resource

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ezwadl/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Resource

Returns a new instance of Resource.



11
12
13
14
15
16
# File 'lib/ezwadl/resource.rb', line 11

def initialize(xml)
  @xml = xml
  @path = (xml.attributes['base']&.value || xml.attributes['path'].value)
  @httpmethods = []
  @resources = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



31
32
33
34
35
36
37
# 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

Instance Attribute Details

#httpmethodsObject

Returns the value of attribute httpmethods.



9
10
11
# File 'lib/ezwadl/resource.rb', line 9

def httpmethods
  @httpmethods
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/ezwadl/resource.rb', line 9

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/ezwadl/resource.rb', line 9

def path
  @path
end

#resourcesObject

Returns the value of attribute resources.



9
10
11
# File 'lib/ezwadl/resource.rb', line 9

def resources
  @resources
end

#xmlObject (readonly)

Returns the value of attribute xml.



8
9
10
# File 'lib/ezwadl/resource.rb', line 8

def xml
  @xml
end

Instance Method Details

#<<(resource) ⇒ Object



26
27
28
29
# File 'lib/ezwadl/resource.rb', line 26

def <<(resource)
  resource.parent = self
  @resources << resource
end

#pathsObject



51
52
53
# 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) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# 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) ⇒ Object



44
45
46
47
48
49
# File 'lib/ezwadl/resource.rb', line 44

def symbolize(str)
  str.tr('^a-zA-Z0-9_', '_')
     .squeeze('_')
     .gsub(/\A_|_\Z/, '')
     .to_sym
end

#uriObject



18
19
20
# File 'lib/ezwadl/resource.rb', line 18

def uri
  URI.join(parent&.uri.to_s || '', URI.escape(path + '/'))
end

#uri_template(data) ⇒ Object



22
23
24
# File 'lib/ezwadl/resource.rb', line 22

def uri_template(data)
  URI.unescape(uri.to_s).gsub(/{/, '%{') % data
end