Sha256: 67fb6dc4172763e5c078f8020137e3e576a088f8015e4c1f3d687d064871301c

Contents?: true

Size: 1.73 KB

Versions: 11

Compression:

Stored size: 1.73 KB

Contents

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, :patch, :delete, :headers, :endpoint, :options, :adapter, :params, :raw, :to => :scope

      dsl_accessor :format, :namespace

      def subkey_class
        Hashie::Mash
      end

      def parse(response)
        if response.is_a?(Faraday::Response)
          return nil if response.status == 204
          response = response.body
        end

        if self.format == :json
          MultiJson.load(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

    private
    def method_missing(method_name, *args, &blk)
      if respond_to?(method_name) || has_special_ending?(method_name)
        super
      elsif use_strict_reader?(method_name)
        fetch(method_name)
      else
        super
      end
    end

    def use_strict_reader?(method_name)
      respond_to?(:strict_attr_reader?) &&
        self.strict_attr_reader? &&
        method_name != :to_ary
    end

    def has_special_ending?(name)
      name.to_s =~ /[?=]$/
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
api_client-0.6.0-java lib/api_client/base.rb
api_client-0.6.0 lib/api_client/base.rb
api_client-0.5.26-java lib/api_client/base.rb
api_client-0.5.26 lib/api_client/base.rb
api_client-0.5.25-java lib/api_client/base.rb
api_client-0.5.25 lib/api_client/base.rb
api_client-0.5.24 lib/api_client/base.rb
api_client-0.5.22 lib/api_client/base.rb
api_client-0.5.21 lib/api_client/base.rb
api_client-0.5.20 lib/api_client/base.rb
api_client-0.5.19 lib/api_client/base.rb