class HyperResource::Adapter::HAL_JSON

HyperResource::Adapter::HAL_JSON provides support for the HAL+JSON hypermedia format by implementing the interface defined in HyperResource::Adapter.

Public Class Methods

apply(response, resource, opts={}) click to toggle source
# File lib/hyper_resource/adapter/hal_json.rb, line 22
def apply(response, resource, opts={})
  if !response.kind_of?(Hash)
    raise ArgumentError, "'response' argument must be a Hash"
  end
  if !resource.kind_of?(HyperResource)
    raise ArgumentError, "'resource' argument must be a HyperResource"
  end

  apply_objects(response, resource)
  apply_links(response, resource)
  apply_attributes(response, resource)
  resource.loaded = true
  resource.href = response['_links']['self']['href'] rescue nil
  resource
end
deserialize(string) click to toggle source
# File lib/hyper_resource/adapter/hal_json.rb, line 18
def deserialize(string)
  JSON.parse(string)
end
serialize(object) click to toggle source
# File lib/hyper_resource/adapter/hal_json.rb, line 14
def serialize(object)
  JSON.dump(object)
end

Private Class Methods

apply_attributes(resp, rsrc) click to toggle source
# File lib/hyper_resource/adapter/hal_json.rb, line 122
def apply_attributes(resp, rsrc)
  rsrc.attributes = rsrc._hr_response_class::Attributes.new(rsrc)

  given_attrs = resp.reject{|k,v| %w(_links _embedded).include?(k)}
  filtered_attrs = rsrc.incoming_body_filter(given_attrs)

  filtered_attrs.keys.each do |attr|
    rsrc.attributes[attr] = filtered_attrs[attr]
  end

  rsrc.attributes._hr_clear_changed
  # rsrc.attributes._hr_create_methods!
end
apply_objects(resp, rsrc) click to toggle source
# File lib/hyper_resource/adapter/hal_json.rb, line 41
def apply_objects(resp, rsrc)
  return unless resp['_embedded']
  rc = rsrc.class
  rsrc.objects = rc::Objects.new(rsrc)
  objs = rsrc.objects

  resp['_embedded'].each do |name, collection|
    if collection.is_a? Hash
      r = rc.new(:root => rsrc.root,
                 :headers => rsrc.headers,
                 :token => rsrc.token,
                 :namespace => rsrc.namespace)
      r.body = collection
      r = classify(collection, r)
      objs[name] = apply(collection, r)
    elsif collection
      objs[name] = collection.map do |obj|
        r = rc.new(:root => rsrc.root,
                   :headers => rsrc.headers,
                   :token => rsrc.token,
                   :namespace => rsrc.namespace)
        r.body = obj
        r = classify(obj, r)
        apply(obj, r)
      end
    end
  end

  # objs._hr_create_methods!
end
classify(resp, rsrc) click to toggle source
# File lib/hyper_resource/adapter/hal_json.rb, line 72
def classify(resp, rsrc)
  return rsrc unless (type_name = get_data_type_from_object(resp)) &&
                     (namespace = rsrc.namespace)
  klass = rsrc.class.namespaced_class(type_name, namespace)

  if klass
    # TODO: Why does this not use klass.new(rsrc)?
    rsrc = klass.new(:root => rsrc.root,
                     :headers => rsrc.headers,
                     :token => rsrc.token,
                     :namespace => rsrc.namespace)
    rsrc.body = resp
  end
  rsrc
end
get_data_type_from_object(object) click to toggle source
# File lib/hyper_resource/adapter/hal_json.rb, line 88
def get_data_type_from_object(object)
  return nil unless object && object['type']
  object['type'][0].upcase + object['type'][1..-1]
end