Class: Nestful::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/nestful/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



82
83
84
85
# File 'lib/nestful/resource.rb', line 82

def initialize(attributes = {})
  @attributes = {}
  load(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object (protected)

:nodoc:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/nestful/resource.rb', line 170

def method_missing(method_symbol, *arguments) #:nodoc:
  method_name = method_symbol.to_s

  if method_name =~ /(=|\?)$/
    case $1
    when "="
      attributes[$`] = arguments.first
    when "?"
      attributes[$`]
    end
  else
    return attributes[method_name] if attributes.include?(method_name)
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



80
81
82
# File 'lib/nestful/resource.rb', line 80

def attributes
  @attributes
end

Class Method Details

.all(*args) ⇒ Object



64
65
66
# File 'lib/nestful/resource.rb', line 64

def self.all(*args)
  self.new(get('', *args))
end

.defaults(value = nil) ⇒ Object Also known as: defaults=, options, options=



17
18
19
20
21
# File 'lib/nestful/resource.rb', line 17

def self.defaults(value = nil)
  @defaults = value if value
  return @defaults if defined?(@defaults) && @defaults
  superclass.respond_to?(:defaults) ? superclass.defaults : {}
end

.delete(action = '', params = {}, options = {}) ⇒ Object



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

def self.delete(action = '', params = {}, options = {})
  request(uri(action), options.merge(:method => :delete, :params => params))
end

.endpoint(value = nil) ⇒ Object Also known as: endpoint=



5
6
7
8
9
# File 'lib/nestful/resource.rb', line 5

def self.endpoint(value = nil)
  @endpoint = value if value
  return @endpoint if @endpoint
  superclass.respond_to?(:endpoint) ? superclass.endpoint : nil
end

.find(id) ⇒ Object



68
69
70
# File 'lib/nestful/resource.rb', line 68

def self.find(id)
  self.new(get(id))
end

.get(action = '', params = {}, options = {}) ⇒ Object



44
45
46
# File 'lib/nestful/resource.rb', line 44

def self.get(action = '', params = {}, options = {})
  request(uri(action), options.merge(:method => :get, :params => params))
end

.new(attributes = {}) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/nestful/resource.rb', line 72

def self.new(attributes = {})
  if attributes.is_a?(Array)
    attributes.map {|set| super(set) }
  else
    super
  end
end

.path(value = nil) ⇒ Object



11
12
13
14
15
# File 'lib/nestful/resource.rb', line 11

def self.path(value = nil)
  @path = value if value
  return @path if @path
  superclass.respond_to?(:path) ? superclass.path : nil
end

.path=Object



25
26
27
28
29
# File 'lib/nestful/resource.rb', line 25

def self.path(value = nil)
  @path = value if value
  return @path if @path
  superclass.respond_to?(:path) ? superclass.path : nil
end

.post(action = '', params = {}, options = {}) ⇒ Object



52
53
54
# File 'lib/nestful/resource.rb', line 52

def self.post(action = '', params = {}, options = {})
  request(uri(action), options.merge(:method => :post, :params => params))
end

.put(action = '', params = {}, options = {}) ⇒ Object



48
49
50
# File 'lib/nestful/resource.rb', line 48

def self.put(action = '', params = {}, options = {})
  request(uri(action), options.merge(:method => :put, :params => params))
end

.request(url, options = {}) ⇒ Object



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

def self.request(url, options = {})
  Request.new(url, Helpers.deep_merge(self.options, options)).execute
end

.uri(*parts) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/nestful/resource.rb', line 35

def self.uri(*parts)
  # If an absolute URI already
  if (uri = parts.first) && uri.is_a?(URI)
    return uri if uri.host
  end

  URI.parse(Helpers.to_path(url, *parts))
end

.urlObject



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

def self.url
  URI.join(endpoint.to_s, path.to_s).to_s
end

Instance Method Details

#==(other) ⇒ Object



141
142
143
144
# File 'lib/nestful/resource.rb', line 141

def ==(other)
  other.equal?(self) ||
    (other.instance_of?(self.class) && other.attributes == attributes)
end

#[](key) ⇒ Object



115
116
117
# File 'lib/nestful/resource.rb', line 115

def [](key)
  attributes[key]
end

#[]=(key, value) ⇒ Object



119
120
121
# File 'lib/nestful/resource.rb', line 119

def []=(key,value)
  attributes[key] = value
end

#as_jsonObject



127
128
129
# File 'lib/nestful/resource.rb', line 127

def as_json(*)
  to_hash
end

#delete(action = '', *args) ⇒ Object



99
100
101
# File 'lib/nestful/resource.rb', line 99

def delete(action = '', *args)
  self.class.delete(path(action), *args)
end

#eql?(other) ⇒ Boolean

Tests for equality (delegates to ==).

Returns:

  • (Boolean)


147
148
149
# File 'lib/nestful/resource.rb', line 147

def eql?(other)
  self == other
end

#get(action = '', *args) ⇒ Object



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

def get(action = '', *args)
  self.class.get(path(action), *args)
end

#hashObject



151
152
153
# File 'lib/nestful/resource.rb', line 151

def hash
  attributes.hash
end

#idObject

:nodoc:



107
108
109
# File 'lib/nestful/resource.rb', line 107

def id #:nodoc:
  self['id']
end

#load(attributes = {}) ⇒ Object



135
136
137
138
139
# File 'lib/nestful/resource.rb', line 135

def load(attributes = {})
  attributes.to_hash.each do |key, value|
    send("#{key}=", value)
  end
end

#path(*parts) ⇒ Object



103
104
105
# File 'lib/nestful/resource.rb', line 103

def path(*parts)
  Helpers.to_path(self.id, *parts)
end

#post(action = '', *args) ⇒ Object



95
96
97
# File 'lib/nestful/resource.rb', line 95

def post(action = '', *args)
  self.class.post(path(action), *args)
end

#put(action = '', *args) ⇒ Object



91
92
93
# File 'lib/nestful/resource.rb', line 91

def put(action = '', *args)
  self.class.put(path(action), *args)
end

#respond_to?(method, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
165
166
# File 'lib/nestful/resource.rb', line 157

def respond_to?(method, include_priv = false)
  method_name = method.to_s
  if attributes.nil?
    super
  elsif attributes.include?(method_name.sub(/[=\?]\Z/, ''))
    true
  else
    super
  end
end

#respond_to_without_attributes?Object



155
# File 'lib/nestful/resource.rb', line 155

alias_method :respond_to_without_attributes?, :respond_to?

#to_hashObject



123
124
125
# File 'lib/nestful/resource.rb', line 123

def to_hash
  attributes.dup
end

#to_jsonObject



131
132
133
# File 'lib/nestful/resource.rb', line 131

def to_json(*)
  as_json.to_json
end

#typeObject

:nodoc:



111
112
113
# File 'lib/nestful/resource.rb', line 111

def type #:nodoc:
  self['type']
end