Class: Resty

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/resty.rb

Overview

Author:

  • Simon Russell

Defined Under Namespace

Classes: Actions, Attributes, Transport

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Resty

Note:

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.

Examples:

Reading an attribute value

r = Resty.href('http://fish.fish/123')  # { ':href': 'http://fish.fish/123', 'name': 'Bob' }
r.name                                  # => "Bob"

Checking a boolean value

r = Resty.href('http://fish.fish/123')  # { ':href': 'http://fish.fish/123', 'fun': true }
r.fun?                                  # => true

Calling an action on the resource

r = Resty.href('http://fish.fish/123')  # { 
                                        #   ':href': 'http://fish.fish/123',
                                        #   ':actions': {
                                        #     'cook': { 
                                        #       ':href': 'http://fish.fish/123/cook'
                                        #       ':method': 'POST'
                                        #     } 
                                        #   }
                                        # }
r.cook!

Calling an action on the resource with params

r = Resty.href('http://fish.fish/123')
r.cook!(style: 'lightly', flavoursomeness: 12)  # this will cause those parameters to be posted with the action


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.

Returns:

  • (String)

    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.

Returns:

  • (String)

    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.

Examples:

r = Resty.from('name' => 'Bob')

Returns:

  • (Resty)

    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.

Returns:

  • (Resty)

    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.

Examples:

r = Resty.href('http://fish.fish/')

Returns:

  • (Resty)

    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.

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

#_hrefString?

Returns The URL of the resource, or nil if none present.

Returns:

  • (String, nil)

    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_dataHash

The data from the resource; will cause the resource to be loaded if it hasn’t already occurred.

Returns:

  • (Hash)

    The data from the resource



21
22
23
# File 'lib/resty.rb', line 21

def _populated_data
  @attributes.populated_data
end

#eachObject

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.

Returns:

  • (Boolean)


26
27
28
# File 'lib/resty.rb', line 26

def respond_to_missing?(name, include_private)
  @attributes.key?(name.to_s)
end

#to_paramString?

Returns The URL of the resource, encoded for safe insertion into a URL. Used for Rails routing.

Returns:

  • (String, nil)

    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_sObject



87
88
89
# File 'lib/resty.rb', line 87

def to_s
  super.gsub('>', _href ? " #{_href}>" : " no-href>")
end