Sha256: 9f3dda021a55c121e8aaafefd8ebe579a921f998e031c7397205face440d15a9

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

# Client Class
#
# builds a base class for all the api calls
# to inherit from.
#
# @example
#   add_this = AddThis::Client.new
#   add_this.shares_url
#
# @example
#   AddThis.get.shares_url
#

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
    # attrs for storing the client object
    attr_accessor :client
  end

  # defining the client object so you can make requests on the AddThis class
  def self.get
    self.client ||= Client.new
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
add_this-0.0.3 lib/add_this/client.rb
add_this-0.0.2 lib/add_this/client.rb