Sha256: 17c27c70512875efc89207b110f3f019e9596ea535d730592f6429d19205dad9

Contents?: true

Size: 1.8 KB

Versions: 8

Compression:

Stored size: 1.8 KB

Contents

require "active_support"
require "active_support/inflector"
require "active_support/core_ext/hash"
require "active_support/core_ext/object"

require 'quandl/client/base/model'
require 'quandl/client/base/attributes'
require 'quandl/client/base/validation'
require 'quandl/client/base/search'

I18n.enforce_available_locales = false

class Quandl::Client::Base
  
  class << self
    
    attr_accessor :url, :token
  
    def use(url)
      self.url = url
      models_use_her_api!
    end
    
    def token=(token)
      @token = token
      models_use_her_api!
    end
    
    def her_api
      Her::API.new.setup url: url_with_version do |c|
        c.use TokenAuthentication
        c.use Faraday::Request::UrlEncoded
        c.use Quandl::Client::Middleware::ParseJSON
        c.use Faraday::Adapter::NetHttp
      end
    end

    def url
      @url ||= "http://quandl.com/api/"
    end

    def url_with_version
      File.join( url.to_s, Quandl::Client.api_version.to_s )
    end
    
    def inherited(subclass)
      # remember models that inherit from base
      models << subclass unless models.include?(subclass)
      # include model behaviour
      subclass.class_eval do
        include Quandl::Client::Base::Model
        include Quandl::Client::Base::Attributes
        include Quandl::Client::Base::Validation
        include Quandl::Client::Base::Search
      end
    end
    
    def models
      @@models ||= []
    end
    
    protected
    
    def models_use_her_api!
      models.each{|m|
        m.url = url_with_version
        m.use_api( her_api ) 
      }
    end
    
    class TokenAuthentication < Faraday::Middleware
      def call(env)
        env[:request_headers]["X-API-Token"] = Quandl::Client::Base.token if Quandl::Client::Base.token.present?
        @app.call(env)
      end
    end
    
  end
  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
quandl_client-2.4.6 lib/quandl/client/base.rb
quandl_client-2.4.5 lib/quandl/client/base.rb
quandl_client-2.4.4 lib/quandl/client/base.rb
quandl_client-2.4.3 lib/quandl/client/base.rb
quandl_client-2.4.2 lib/quandl/client/base.rb
quandl_client-2.4.1 lib/quandl/client/base.rb
quandl_client-2.4.0 lib/quandl/client/base.rb
quandl_client-2.3.2 lib/quandl/client/base.rb