Class: Resty
Overview
Defined Under Namespace
Classes: Actions, Attributes, Transport
Class Method Summary collapse
-
.decode_param(s) ⇒ String
The input (previously encoded using Resty::encode_param) decoded into a string.
-
.encode_param(s) ⇒ String
The input encoded for safe use in a URL.
-
.from(data) ⇒ Resty
A new Resty constructed from the given hash.
-
.from_param(s) ⇒ Resty
A new Resty created from the URL encoded in the input.
-
.href(href) ⇒ Resty
A new Resty pointing at the given URL.
-
.wrap(object) ⇒ Object
The input object, or a Resty wrapping if it’s a Hash or Array.
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#_href ⇒ String?
The URL of the resource, or nil if none present.
-
#_populated_data ⇒ Hash
The data from the resource; will cause the resource to be loaded if it hasn’t already occurred.
-
#each ⇒ Object
Iterate through each item in the array; doesn’t iterate over keys in the hash.
-
#initialize(attributes) ⇒ Resty
constructor
A new instance of Resty.
-
#method_missing(name, *args) ⇒ Object
Resty exposes the values and actions from the resource as methods on the object.
-
#respond_to_missing?(name, include_private) ⇒ Boolean
Make respond_to? return true to the corresponding magic methods added using #method_missing.
-
#to_param ⇒ String?
The URL of the resource, encoded for safe insertion into a URL.
- #to_s ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Resty
Generally, use Resty::from instead of Resty::new.
Returns a new instance of Resty.
10 11 12 |
# File 'lib/resty.rb', line 10 def initialize(attributes) @attributes = attributes end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Resty exposes the values and actions from the resource as methods on the object.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/resty.rb', line 71 def method_missing(name, *args) if name =~ /^(.+)!$/ if @attributes.actions.exist?($1) @attributes.actions.perform!($1, *args) else super end elsif name =~ /^(.+)\?$/ !!@attributes[$1] elsif @attributes.key?(name.to_s) @attributes[name.to_s] else super end end |
Class Method Details
.decode_param(s) ⇒ String
Returns The input (previously encoded using Resty::encode_param) decoded into a string.
123 124 125 |
# File 'lib/resty.rb', line 123 def self.decode_param(s) s && Base64.urlsafe_decode64(s.to_s) end |
.encode_param(s) ⇒ String
Returns The input encoded for safe use in a URL.
118 119 120 |
# File 'lib/resty.rb', line 118 def self.encode_param(s) s && Base64.urlsafe_encode64(s.to_s) end |
.from(data) ⇒ Resty
Returns A new Resty constructed from the given hash.
94 95 96 |
# File 'lib/resty.rb', line 94 def self.from(data) new(Resty::Attributes.new(data)) end |
.from_param(s) ⇒ Resty
Returns A new Resty created from the URL encoded in the input.
128 129 130 |
# File 'lib/resty.rb', line 128 def self.from_param(s) href(decode_param(s)) end |
.href(href) ⇒ Resty
Returns A new Resty pointing at the given URL.
113 114 115 |
# File 'lib/resty.rb', line 113 def self.href(href) from(':href' => href) end |
.wrap(object) ⇒ Object
Returns The input object, or a Resty wrapping if it’s a Hash or Array.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/resty.rb', line 99 def self.wrap(object) case object when Hash from(object) when Array from(':items' => object) else object end end |
Instance Method Details
#[](index) ⇒ Object
42 43 44 |
# File 'lib/resty.rb', line 42 def [](index) @attributes.items[index] end |
#_href ⇒ String?
Returns The URL of the resource, or nil if none present.
15 16 17 |
# File 'lib/resty.rb', line 15 def _href @attributes.href end |
#_populated_data ⇒ Hash
The data from the resource; will cause the resource to be loaded if it hasn’t already occurred.
21 22 23 |
# File 'lib/resty.rb', line 21 def _populated_data @attributes.populated_data end |
#each ⇒ Object
Iterate through each item in the array; doesn’t iterate over keys in the hash.
36 37 38 39 40 |
# File 'lib/resty.rb', line 36 def each @attributes.items.each do |x| yield x end end |
#respond_to_missing?(name, include_private) ⇒ Boolean
Make respond_to? return true to the corresponding magic methods added using #method_missing.
26 27 28 |
# File 'lib/resty.rb', line 26 def respond_to_missing?(name, include_private) @attributes.key?(name.to_s) end |
#to_param ⇒ String?
Returns The URL of the resource, encoded for safe insertion into a URL. Used for Rails routing.
31 32 33 |
# File 'lib/resty.rb', line 31 def to_param Resty.encode_param(_href) end |
#to_s ⇒ Object
87 88 89 |
# File 'lib/resty.rb', line 87 def to_s super.gsub('>', _href ? " #{_href}>" : " no-href>") end |