class Restfolia::Resource

Public: Resource is the representation of JSON response. It transforms all JSON attributes in attributes and provides a “links” method, to help with hypermedia navigation.

Examples

resource = Resource.new(:attr_test => "test")
resource.attr_test  # => "test"
resource.links  # => []

resource = Resource.new(:attr_test => "test",
                        :links => {:href => "http://service.com",
                                   :rel => "self",
                                   :type => "application/json"})
resource.attr_test  # => "test"
resource.links  # => [#<Restfolia::EntryPoint ...>]

By default, “links” method, expects from JSON to be the following formats:

# Array de Links
"links" : [{ "href" : "http://fakeurl.com/some/service",
            "rel" : "self",
            "type" : "application/json"
          }]

# OR 'single' Links
"links" : { "href" : "http://fakeurl.com/some/service",
            "rel" : "self",
            "type" : "application/json"
          }

# OR node 'Link', that can be Array or single too
"link" : { "href" : "http://fakeurl.com/some/service",
            "rel" : "self",
            "type" : "application/json"
         }

Attributes

_json[R]

Public: Returns the Hash that represents parsed JSON.

Public Class Methods

new(json) click to toggle source

Public: Initialize a Resource.

json - Hash that represents parsed JSON.

Raises ArgumentError if json parameter is not a Hash object.

# File lib/restfolia/resource.rb, line 50
def initialize(json)
  unless json.is_a?(Hash)
    raise(ArgumentError, "json parameter have to be a Hash object", caller)
  end
  @_json = json

  #Add json keys as methods of Resource
  #http://blog.jayfields.com/2008/02/ruby-replace-methodmissing-with-dynamic.html
  @_json.each do |method, value|
    next if self.respond_to?(method) && (method != :id) && (method != "id")

    (class << self; self; end).class_eval do
      define_method(method) do |*args|
        value
      end
    end
  end
end

Public Instance Methods

Protected Instance Methods