Sha256: 6f10a352e0d6d6900398c36ddacdab93bd8351434cb5dd91058a0e89cb4d94f7

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require 'uri_template'
require "bootic_client/entity"

module BooticClient

  class Relation

    GET = 'get'.freeze

    def initialize(attrs, client, wrapper_class = Entity)
      @attrs, @client, @wrapper_class = attrs, client, wrapper_class
    end

    def inspect
      %(#<#{self.class.name} #{attrs.inspect}>)
    end

    def href
      attrs['href']
    end

    def templated?
      !!attrs['templated']
    end

    def name
      attrs['name']
    end

    def title
      attrs['title']
    end

    def type
      attrs['type']
    end

    def docs
      attrs['docs']
    end

    def transport_method
      @transport_method ||= attrs['method'] || GET
    end

    def run(opts = {})
      if templated?
        uri_vars = uri.variables
        payload = opts.each_with_object({}) do |(k,v),memo|
          memo[k] = v unless uri_vars.include?(k.to_s)
        end
        client.request_and_wrap transport_method.to_sym, uri.expand(opts), wrapper_class, payload
      else
        client.request_and_wrap transport_method.to_sym, href, wrapper_class, opts
      end
    end

    def self.expand(href, opts = {})
      URITemplate.new(href).expand(opts)
    end

    protected
    attr_reader :wrapper_class, :client, :attrs

    def uri
      @uri ||= URITemplate.new(href)
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bootic_client-0.0.9 lib/bootic_client/relation.rb
bootic_client-0.0.8 lib/bootic_client/relation.rb