Sha256: 30462dbc988e3362af7bbe4593cc7a39d4277b0f5e75985731fc98dcaf3e49f7
Contents?: true
Size: 1.68 KB
Versions: 4
Compression:
Stored size: 1.68 KB
Contents
module Yaks module Reader class Hal include Util def call(parsed_json, _env = {}) attributes = parsed_json.dup links = convert_links(attributes.delete('_links') || {}) embedded = convert_embedded(attributes.delete('_embedded') || {}) Resource.new( type: attributes.delete('type') || type_from_links(links), attributes: Util.symbolize_keys(attributes), links: links, subresources: embedded ) end def type_from_links(links) profile = links.detect {|l| l.rel?(:profile)} profile.uri[/\w+$/] if profile end def convert_links(links) links.flat_map do |rel, link| array(link).map do |l| options = symbolize_keys(slice_hash(l, 'title', 'templated')) # if it looks like a keyword we'll assume it's a registered rel type rel = rel.to_sym if rel =~ /\A\w+\z/ Resource::Link.new(rel: rel, uri: l['href'], options: options) end end end def array(x) x.instance_of?(Array) ? x : [x] end def convert_embedded(embedded) embedded.flat_map do |rel, resource| case resource when nil NullResource.new when Array if resource.empty? NullResource.new(collection: true) else CollectionResource.new( members: resource.map { |r| call(r).with(type: Util.singularize(rel[/\w+$/])) } ) end else call(resource) end.with(rels: [rel]) end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
yaks-0.13.0 | lib/yaks/reader/hal.rb |
yaks-0.12.0 | lib/yaks/reader/hal.rb |
yaks-0.11.0 | lib/yaks/reader/hal.rb |
yaks-0.10.0 | lib/yaks/reader/hal.rb |