Sha256: db670304f62c78ff8236966d5f941f1a754d4e741e146ce98645876f25b3b20a

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module Typekit
  module Client
    def self.new(options)
      raise Error, 'Token is missing' unless options.key?(:token)

      client = Module.new
      client.extend(InstanceMethods)
      client.configure(Typekit.defaults.merge(options))

      client
    end

    module InstanceMethods
      [:process, :index, :show, :create, :update, :delete].each do |method|
        define_method(method) do |*arguments|
          translate(engine.send(method, *arguments))
        end
      end

      def configure(options)
        self.options = options
      end

      private

      attr_accessor :options

      def engine
        @engine ||= build_engine
      end

      def build_engine
        engine_options = options.merge(dictionary: Typekit.dictionary,
          headers: Typekit.headers.call(options[:token]))

        version = options[:version]
        format = options[:format]

        Apitizer::Base.new(engine_options) do
          instance_exec(version, format, &Typekit.schema)
        end
      end

      def translate(result)
        unless result.is_a?(Hash) && result.length == 1
          raise ServerError.new(result.code)
        end
        name, object = *result.first
        converter(name).process(result, object)
      end

      def const_missing(name)
        record_class(name)
      end

      def converter(name)
        (@converters ||= {})[name] ||= Converter.build(name, self)
      end

      def record_class(name)
        (@record_classes ||= {})[name] ||= Record.build(name, self)
      end
    end

    module Proxy
      attr_reader :client, :token

      def connect(object = nil, token = Helper.tokenize(self.class))
        self.client = object.respond_to?(:client) ? object.client : object
        self.token = token
      end

      private

      attr_writer :client, :token

      def process(action, *arguments)
        raise Error, 'Client is not specified' if client.nil?
        client.process(action, token, *arguments)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
typekit-client-0.0.6 lib/typekit/client.rb