Sha256: 0a2d4fdfd275b0a95acdbda838c0c7acab3b10bd63efb3ae9adb4666a66d74ea

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

require 'httparty'

module SoapyBing
  class OauthCredentials
    class TokenRefreshError < StandardError; end

    TOKEN_URL = 'https://login.live.com/oauth20_token.srf'.freeze

    attr_reader :client_id, :client_secret, :refresh_token, :token_url

    def initialize(oauth_options = {})
      param_guard = ParamGuard.new(oauth_options, env_namespace: 'BING_ADS_OAUTH')
      @client_id = param_guard.require!(:client_id)
      @client_secret = param_guard.require!(:client_secret)
      @refresh_token = param_guard.require!(:refresh_token)
      @token_url = oauth_options.fetch(:token_url, ENV['BING_ADS_OAUTH_TOKEN_URL'] || TOKEN_URL)
    end

    def access_token
      @access_token ||= request_access_token
    end

    private

    def request_access_token
      resp = HTTParty.post(token_url, body: access_token_params)
      if resp.code == 200
        resp['access_token']
      else
        fail TokenRefreshError
      end
    end

    def access_token_params
      {
        client_id: client_id,
        client_secret: client_secret,
        grant_type: 'refresh_token',
        refresh_token: refresh_token
      }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
soapy_bing-0.0.4 lib/soapy_bing/oauth_credentials.rb
soapy_bing-0.0.3 lib/soapy_bing/oauth_credentials.rb
soapy_bing-0.0.2 lib/soapy_bing/oauth_credentials.rb
soapy_bing-0.0.1 lib/soapy_bing/oauth_credentials.rb