class Sendicate::Resource

Attributes

attributes[R]
errors[R]
response[R]

Public Class Methods

all() click to toggle source
# File lib/sendicate/resource.rb, line 37
def all
  response = get(collection_path)
  response.parsed_response.map do |attributes|
    new(attributes)
  end
end
base_path(path) click to toggle source
# File lib/sendicate/resource.rb, line 33
def base_path(path)
  @base_path = path
end
collection_path() click to toggle source
# File lib/sendicate/resource.rb, line 53
def collection_path
  @base_path
end
delete(*args) click to toggle source
# File lib/sendicate/resource.rb, line 82
def delete(*args)
  Request.delete(*args)
end
find(param) click to toggle source
# File lib/sendicate/resource.rb, line 44
def find(param)
  if param.nil?
    raise ResourceNotFound
  else
    response = get(member_path(param))
    new(response.parsed_response)
  end
end
get(*args) click to toggle source
# File lib/sendicate/resource.rb, line 66
def get(*args)
  Request.get(*args)
end
member_path(param) click to toggle source
# File lib/sendicate/resource.rb, line 57
def member_path(param)
  escaped_param = if param.is_a?(String)
    CGI::escape(param)
  else
    param
  end
  [@base_path, escaped_param].join("/")
end
new(attributes={}) click to toggle source
# File lib/sendicate/resource.rb, line 12
def initialize(attributes={})
  @attributes = attributes
  @errors = {}
end
patch(*args) click to toggle source
# File lib/sendicate/resource.rb, line 74
def patch(*args)
  Request.patch(*args)
end
post(*args) click to toggle source
# File lib/sendicate/resource.rb, line 70
def post(*args)
  Request.post(*args)
end
put(*args) click to toggle source
# File lib/sendicate/resource.rb, line 78
def put(*args)
  Request.put(*args)
end

Public Instance Methods

destroy() click to toggle source
# File lib/sendicate/resource.rb, line 103
def destroy
  response = self.class.delete(member_path)
end
id() click to toggle source
# File lib/sendicate/resource.rb, line 111
def id
  attributes['id'] || attributes[:id]
end
member_path() click to toggle source
# File lib/sendicate/resource.rb, line 127
def member_path
  self.class.member_path(to_param)
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/sendicate/resource.rb, line 17
def method_missing(method, *args)
  method_string = method.to_s
  is_setter_method = method_string[-1, 1] == '='
  key = method_string.gsub(/\=/, '')
  
  if is_setter_method
    attributes[key] = args.first
  elsif attributes.include?(key)
    attributes[key]
  else
    super
  end
end
persisted?() click to toggle source
# File lib/sendicate/resource.rb, line 107
def persisted?
  !to_param.nil?
end
save() click to toggle source
# File lib/sendicate/resource.rb, line 87
def save
  response = if persisted?
    self.class.put(member_path, body: to_json)
  else
    self.class.post(member_path, body: to_json)
  end
  
  if response.success?
    @attributes = response.parsed_response
    true
  else
    @errors = response.parsed_response
    false
  end
end
to_json() click to toggle source
# File lib/sendicate/resource.rb, line 123
def to_json
  MultiJson.dump(attributes)
end
to_param() click to toggle source
# File lib/sendicate/resource.rb, line 115
def to_param
  if id.is_a?(String)
    CGI::escape(id)
  else
    id
  end
end