Sha256: 6bed88bab2ce23df6c40572bde29fe5d250af5750d9ba9e69fd57efa9334e58f

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

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

module PMP
  class Client

    include Configuration

    def initialize(options={}, &block)
      apply_configuration(options)
      yield(self) if block_given?
    end

    def credentials(opts={})
      @credentials = nil if (opts != {})
      @credentials ||= PMP::Credential.new(options.merge(opts))
    end

    def token(opts={})
      @token = nil if (opts != {})
      @token ||= PMP::Token.new(options.merge(opts)).get_token
    end

    def root(opts={})
      @root = nil if (opts != {})
      @root ||= new_root(opts)
    end

    def new_root(opts={})
      root_options = options.merge(opts).merge(href: endpoint)
      PMP::CollectionDocument.new(root_options).tap{|r| r.root = r }
    end

    def doc_of_type(type, opts={})
      doc = PMP::CollectionDocument.new(options.merge(root:root(opts)).merge(opts))
      doc.links['profile'] = Link.new(href: profile_href_for_type(type), type: "application/vnd.collection.doc+json")
      doc
    end

    # private

    def profile_href_for_type(type)
      "#{endpoint}profiles/#{type}"
    end

    # assume you want to make a call on the root doc for stuff it can do
    def method_missing(method, *args)
      self.root.send(method, *args)
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pmp-0.5.7 lib/pmp/client.rb
pmp-0.5.6 lib/pmp/client.rb
pmp-0.5.5 lib/pmp/client.rb
pmp-0.5.4 lib/pmp/client.rb
pmp-0.5.3 lib/pmp/client.rb
pmp-0.5.2 lib/pmp/client.rb
pmp-0.5.1 lib/pmp/client.rb