module Harpy::Resource::InstanceMethods

Public Class Methods

new(attrs = nil) click to toggle source
# File lib/harpy/resource.rb, line 136
def initialize(attrs = nil)
  @attrs = {}
  self.attributes = attrs || {}
end

Public Instance Methods

==(other) click to toggle source
# File lib/harpy/resource.rb, line 210
def ==(other)
  other.equal?(self) || (urn && other.instance_of?(self.class) && other.urn == urn)
end
Also aliased as: eql?
as_json(*args) click to toggle source
# File lib/harpy/resource.rb, line 151
def as_json(*args)
  hash = @attrs.dup
  hash.delete "link"
  hash.delete "urn"
  hash
end
attributes=(attrs) click to toggle source
# File lib/harpy/resource.rb, line 141
def attributes=(attrs)
  attrs.each do |key, value|
    if respond_to? "#{key}="
      send "#{key}=", value
    else
      @attrs[key] = value
    end
  end
end
destroy() click to toggle source
# File lib/harpy/resource.rb, line 170
def destroy
  raise Harpy::UrlRequired unless url
  run_callbacks :destroy do
    process_response Harpy.client.delete(url), :destroy
  end
end
eql?(other)
Alias for: ==
has_key?(key) click to toggle source
# File lib/harpy/resource.rb, line 202
def has_key?(key)
  @attrs.has_key? key.to_s
end
hash() click to toggle source
# File lib/harpy/resource.rb, line 206
def hash
  urn.hash
end
id() click to toggle source
# File lib/harpy/resource.rb, line 190
def id
  @attrs["urn"].split(":").last if @attrs["urn"]
end
inspect() click to toggle source
# File lib/harpy/resource.rb, line 198
def inspect
  "<#{self.class.name} @attrs:#{@attrs.inspect} @errors:#{errors.full_messages.inspect} persisted:#{persisted?}>"
end
persisted?() click to toggle source
# File lib/harpy/resource.rb, line 194
def persisted?
  @attrs["urn"].present?
end
save() click to toggle source
# File lib/harpy/resource.rb, line 158
def save
  if valid?
    run_callbacks :save do
      json = JSON.generate as_json
      raise BodyToBig, "Size: #{json.bytesize} bytes (max 1MB)" if json.bytesize > 1.megabyte
      persisted? ? update(json) : create(json)
    end
  else
    false
  end
end
url() click to toggle source
# File lib/harpy/resource.rb, line 182
def url
  link "self"
end
url_collection() click to toggle source
# File lib/harpy/resource.rb, line 186
def url_collection
  Harpy.entry_point.resource_url self.class.resource_name
end

Private Instance Methods

create(json) click to toggle source
# File lib/harpy/resource.rb, line 217
def create(json)
  run_callbacks :create do
    process_response Harpy.client.post(url_collection, :body => json), :create
  end
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/harpy/resource.rb, line 253
def method_missing(method, *args)
  key = method.to_s
  if persisted? && !@attrs.has_key?(key)
    super
  elsif key=~/[\]=]\z/
    super
  else
    @attrs[key]
  end
end
process_response(response, context) click to toggle source
# File lib/harpy/resource.rb, line 230
def process_response(response, context)
  case response.code
  when 200, 201, 302
    @attrs.merge! JSON.parse(response.body)
    true
  when 204
    context==:create ? Harpy.client.invalid_code(response) : true
  when 401
    raise Harpy::Unauthorized, "Server returned a 401 response code"
  when 422
    JSON.parse(response.body)["errors"].each do |attr, attr_errors|
      attr_errors.each{|attr_error| errors.add(attr, :invalid, message: attr_error) }
    end
    false
  else
    Harpy.client.invalid_code response
  end
end
read_attribute_for_validation(key) click to toggle source
# File lib/harpy/resource.rb, line 249
def read_attribute_for_validation(key)
  @attrs[key]
end
update(json) click to toggle source
# File lib/harpy/resource.rb, line 223
def update(json)
  run_callbacks :update do
    raise Harpy::UrlRequired unless url
    process_response Harpy.client.put(url, :body => json), :update
  end
end