Sha256: bc38264dc8665148e1d96692ce5c6af116ddb5e927b9f8f85b7dd6f97337d480

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# Client Class
#
# builds a base class for all the api calls
# to inherit from.

require "add_this/requests"

module AddThis

  class Client
    # include all other classes to inherit from
    include Requests

    attr_accessor :user_id, :password

    # Creating the base client
    #
    # @option opts [String] :user_id login for Add This
    # @option opts [String] :password password for Add This
    #
    # @example
    #   AddThis::Client.new
    def initialize(opts = {})
      @user_id    = opts[:user_id]  || AddThis.configuration.user_id
      @password   = opts[:password] || AddThis.configuration.password
      connection.basic_auth(@user_id, @password)
    end

    # Creates a Faraday connection block
    #
    # @return [Faraday::Connection]
    def connection
      @connection ||= Faraday.new(url: base_url, headers: default_headers, ssl: {verify: false}) do |builder|
        builder.use Faraday::Request::UrlEncoded
        builder.use Faraday::Response::Mashify
        builder.use Faraday::Response::ParseJson
        builder.adapter Faraday.default_adapter
      end
    end
    
    protected

      # @protected
      def base_url
        "https://api.addthis.com/analytics/1.0/pub/"
      end

      # @protected
      def default_headers
        {
          accept:  'application/json',
          user_agent: 'Ruby gem',
        }
      end

  end
  
  class << self
    attr_accessor :client
  end

  def self.get
    self.client ||= Client.new
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
add_this-0.0.1 lib/add_this/client.rb