Sha256: 0e81a23ae4649322a03ead3492edf7c8ab663f4e1067322fb8bb6d328579ae88

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require 'oauth2'
require 'redis'

require 'active_support'
require 'active_support/core_ext/numeric/time'

module ParcelApi
  class Client

    attr_accessor :client_id,
      :client_secret,
      :username,
      :password,
      :address,
      :auth_address,
      :redis

    def self.connection
      @connection ||= new.connection
    end

    def initialize
      @client_id     = client_id     || ENV['CLIENT_ID']
      @client_secret = client_secret || ENV['CLIENT_SECRET']
      @username      = username      || ENV['USERNAME']
      @password      = password      || ENV['PASSWORD']
      @address       = address       || 'https://api.nzpost.co.nz'
      @auth_address  = auth_address  || 'https://oauth.nzpost.co.nz/as/token.oauth2'
      @redis         = redis         || Redis.new
    end

    def connection
      @access_token_cache ||= begin
        if json = @redis.get(:parcel_api_access_token)
          access_token = OAuth2::AccessToken.from_hash client, JSON.parse(json)
        else
          access_token = client.password.get_token @username, @password
          @redis.set(:parcel_api_access_token, access_token.to_hash.to_json, ex: access_token.expires_in.seconds - 6.hours)
          access_token
        end
      end
    end

    private

    def client
      OAuth2::Client.new(@client_id, @client_secret, site: @address, token_url: @auth_address, connection_opts: { headers: { 'client_id' => @client_id } })
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parcel_api-1.1.1 lib/parcel_api/client.rb