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.



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

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:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/nestful/resource.rb', line 141

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.



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

def attributes
  @attributes
end

Class Method Details

.all(*args) ⇒ Object



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

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

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



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

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

.endpoint(value = nil) ⇒ Object



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



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

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

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



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

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

.new(attributes = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/nestful/resource.rb', line 59

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

.options(value = nil) ⇒ Object



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

def self.options(value = nil)
  @options = value if value
  return @options if @options
  superclass.respond_to?(:options) ? superclass.options : {}
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

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



39
40
41
# File 'lib/nestful/resource.rb', line 39

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

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



35
36
37
# File 'lib/nestful/resource.rb', line 35

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

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



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

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

.uri(*parts) ⇒ Object



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

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

.urlObject



23
24
25
# File 'lib/nestful/resource.rb', line 23

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

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  attributes[key]
end

#[]=(key, value) ⇒ Object



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

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

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



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

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

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



74
75
76
# File 'lib/nestful/resource.rb', line 74

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

#idObject

:nodoc:



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

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

#load(attributes = {}) ⇒ Object



120
121
122
123
124
# File 'lib/nestful/resource.rb', line 120

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

#path(*parts) ⇒ Object



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

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

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



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

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

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



78
79
80
# File 'lib/nestful/resource.rb', line 78

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

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

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
# File 'lib/nestful/resource.rb', line 128

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



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

alias_method :respond_to_without_attributes?, :respond_to?

#to_hashObject Also known as: as_json



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

def to_hash
  attributes.dup
end

#to_jsonObject



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

def to_json(*)
  as_json.to_json
end

#typeObject

:nodoc:



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

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