Class: EzWadl::Resource
- Inherits:
-
Object
- Object
- EzWadl::Resource
- Includes:
- HTTParty
- Defined in:
- lib/ezwadl/resource.rb
Instance Attribute Summary collapse
-
#httpmethods ⇒ Object
Returns the value of attribute httpmethods.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#path ⇒ Object
Returns the value of attribute path.
-
#resources ⇒ Object
Returns the value of attribute resources.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
- #<<(resource) ⇒ Object
-
#initialize(xml) ⇒ Resource
constructor
A new instance of Resource.
- #method_missing(method, *args) ⇒ Object
- #paths ⇒ Object
- #respond_to_missing?(method, include_all = false) ⇒ Boolean
- #symbolize(str) ⇒ Object
- #uri ⇒ Object
- #uri_template(data) ⇒ Object
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
#httpmethods ⇒ Object
Returns the value of attribute httpmethods.
9 10 11 |
# File 'lib/ezwadl/resource.rb', line 9 def httpmethods @httpmethods end |
#parent ⇒ Object
Returns the value of attribute parent.
9 10 11 |
# File 'lib/ezwadl/resource.rb', line 9 def parent @parent end |
#path ⇒ Object
Returns the value of attribute path.
9 10 11 |
# File 'lib/ezwadl/resource.rb', line 9 def path @path end |
#resources ⇒ Object
Returns the value of attribute resources.
9 10 11 |
# File 'lib/ezwadl/resource.rb', line 9 def resources @resources end |
#xml ⇒ Object (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 |
#paths ⇒ Object
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
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 |
#uri ⇒ Object
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 |