Class: Wrest::Resource::Base

Inherits:
Object
  • Object
show all
Includes:
Components::AttributesContainer, Components::AttributesContainer::Typecaster
Defined in:
lib/wrest/resource/base.rb

Overview

Resource::Base is the equivalent of ActiveResource::Base. It is a REST client targetted at Rails REST apps.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Components::AttributesContainer::Typecaster

included

Methods included from Components::AttributesContainer

build_attribute_getter, build_attribute_queryer, build_attribute_setter, included

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



19
20
21
# File 'lib/wrest/resource/base.rb', line 19

def attributes
  @attributes
end

Class Method Details

.create(attributes = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/wrest/resource/base.rb', line 87

def create(attributes = {})
  response = Wrest::UriTemplate.new(':host/:resource_collection_name.:format').to_uri(
    :host => host,
    :resource_collection_name => resource_collection_name,
    :format => default_format
  ).post(self.new(attributes).to_xml, 'Content-Type' => "application/#{default_format}")

  self.new(response.deserialise.mutate_using(
    Wrest::Components::Mutators.chain(:xml_mini_type_caster, :camel_to_snake_case)
  ).shift.last)
end

.find(id) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wrest/resource/base.rb', line 75

def find(id)
  response = find_one_uri_template.to_uri(
    :host => host,
    :resource_collection_name => resource_collection_name,
    :id => id,
    :format => default_format
  ).get
  self.new(response.deserialise.mutate_using(
    Wrest::Components::Mutators.chain(:xml_mini_type_caster, :camel_to_snake_case)
  ).shift.last)
end

.find_one_uri_templateObject



71
72
73
# File 'lib/wrest/resource/base.rb', line 71

def find_one_uri_template
  @find_one_template ||= Wrest::UriTemplate.new(':host/:resource_collection_name/:id.:format')
end

.inherited(klass) ⇒ Object



36
37
38
# File 'lib/wrest/resource/base.rb', line 36

def inherited(klass)
  klass.set_resource_name klass.name
end

.resource_collection_nameObject



67
68
69
# File 'lib/wrest/resource/base.rb', line 67

def resource_collection_name
  @resource_collection_name ||= "#{resource_name.underscore.pluralize}"
end

.set_default_format(format) ⇒ Object



60
61
62
# File 'lib/wrest/resource/base.rb', line 60

def set_default_format(format)
  self.class_eval "def self.default_format; '#{format.to_s}';end"
end

.set_host(host) ⇒ Object

Allows the host url at which the resource is found to be configured and creates a getter method for it. For example in the url

http://localhost:3000/users/1/settings

you would set

http://localhost:3000

as the host url.



56
57
58
# File 'lib/wrest/resource/base.rb', line 56

def set_host(host)
  self.class_eval "def self.host; '#{host}';end"
end

.set_redirect_handler(method_object) ⇒ Object



64
65
# File 'lib/wrest/resource/base.rb', line 64

def set_redirect_handler(method_object)
end

.set_resource_name(resource_name) ⇒ Object

Allows the resource name to be configured and creates a getter method for it. This is a useful feature when using anonymous classes like we often do while writing tests. By default, the resource name is set to the name of the class.



45
46
47
# File 'lib/wrest/resource/base.rb', line 45

def set_resource_name(resource_name)
  self.class_eval "def self.resource_name; '#{resource_name.underscore}';end"
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
24
25
# File 'lib/wrest/resource/base.rb', line 21

def ==(other)
  return true if self.equal?(other)
  return false unless other.class == self.class
  return self.attributes == other.attributes
end

#hashObject



27
28
29
# File 'lib/wrest/resource/base.rb', line 27

def hash
  id.hash
end

#to_xml(options = {}) ⇒ Object



31
32
33
# File 'lib/wrest/resource/base.rb', line 31

def to_xml(options={})
  attributes.to_xml({:root => self.class.resource_name.gsub('_', '-')}.merge(options))
end