Sha256: 077fa5068d3e8a0c28dce7044009a7c6bddf5b23bf71fb2267508c7ba44c083e

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

require "hashie"
require "yajl"

module ApiClient

  class Base < Hashie::Mash

    extend ApiClient::Mixins::Inheritance
    extend ApiClient::Mixins::Instantiation
    extend ApiClient::Mixins::Scoping
    extend ApiClient::Mixins::ConnectionHooks

    class << self
      extend ApiClient::Mixins::Delegation
      extend ApiClient::Mixins::Configuration

      delegate :fetch, :get, :put, :post, :delete, :headers, :endpoint, :options, :adapter, :params, :raw, :to => :scope

      dsl_accessor :format, :namespace

      def subkey_class
        Hashie::Mash
      end

      def parse(response)
        response = response.body if response.is_a?(Faraday::Response)
        if self.format == :json
          Yajl::Parser.parse(response)
        elsif self.format == :xml
          MultiXml.parse(response)
        else
          response
        end
      end

    end

    # Defaults
    self.format :json

    def id
      self['id']
    end

    def inspect
      attributes = []
      attr_keys = self.keys - ['id']
      attributes.push "id: #{self.id}" if self.id
      attr_keys.each do |key|
        attributes.push("#{key}: #{self[key].inspect}")
      end
      "#<#{self.class} #{attributes.join(', ')}>"
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
api_client-0.3.3 lib/api_client/base.rb
api_client-0.3.2 lib/api_client/base.rb
api_client-0.3.1 lib/api_client/base.rb
api_client-0.3.0 lib/api_client/base.rb
api_client-0.2.0 lib/api_client/base.rb
api_client-0.1.9 lib/api_client/base.rb
api_client-0.1.8 lib/api_client/base.rb