Sha256: be0d2f1563dd45d17faaf012a2cc6d5b35c76cda0132bde69a540d42a30505a8

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# -*- encoding: utf-8 -*-

require 'uri_template'
require 'ostruct'

# # it's a struct, we don't have to set these, up, but have a list of what we define particular logic for
# :href,          # "https://api-sandbox.pmp.io/docs/af676335-21df-4486-ab43-e88c1b48f026"
# :href_template, # "https://api-sandbox.pmp.io/users{?limit,offset,tag,collection,text,searchsort,has}"
# :href_vars,     # { "collection": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval" }
# :hreflang,      # Language of the linked document
# :hints,         # Hints about interacting with the link, such as HTTP methods, e.g. "hints": { "allow": ["GET", "PUT", "DELETE"] }
# :rels,          # [ "urn:pmp:query:users" ]
# :method,        # http method - get, post, put, etc.
# :type,          # 'image/png' - mime type of linked resource
# :title,         # name/title of thing linked in
# :operation,     # used by permissions link - read, write
# :blacklist,     # used by permissions link

module PMP
  class Link < OpenStruct

    include Parser

    attr_accessor :parent

    attr_accessor :params

    def initialize(parent, link)
      super()
      self.parent = parent || PMP::CollectionDocument.new
      self.params = link.delete('params') || {}
      parse_attributes(link)
    end

    def attributes
      HashWithIndifferentAccess.new(marshal_dump)
    end

    def where(params={})
      self.class.new(parent, attributes.merge({'params'=>params}))
    end

    def as_json
      extract_attributes
    end

    def url
      URITemplate.new(href_template || href).expand(params)
    end

    def retrieve
      @_retrieved ||= parent.request((method || 'get').to_sym, url)
    end

    def method_missing(method, *args)
      begin
        super
      rescue NoMethodError => err
        self.retrieve.send(method, *args)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pmp-0.1.1 lib/pmp/link.rb
pmp-0.1.0 lib/pmp/link.rb