Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 4.1.0 (2025-01-10)

- Fixed token refresh function and test

# 4.0.0 (2025-01-08)

- Update from Worldcat API v1 to API v2
Expand Down
2 changes: 1 addition & 1 deletion lib/berkeley_library/location/module_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module ModuleInfo
SUMMARY = 'Locaton-related utilities for the UC Berkeley Library'.freeze
DESCRIPTION = 'A collection of location-related utilities for the UC Berkeley Library'.freeze
LICENSE = 'MIT'.freeze
VERSION = '4.0.0'.freeze
VERSION = '4.1.0'.freeze
HOMEPAGE = 'https://github.com/BerkeleyLibrary/location'.freeze
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/berkeley_library/location/world_cat/oclc_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class OCLCAuth
attr_accessor :token

def initialize
# Sorry Rubocop - needs to be ||= because we're dealing with a singleton
# rubocop:disable Lint/DisjunctiveAssignmentInConstructor
@token ||= fetch_token
# rubocop:enable Lint/DisjunctiveAssignmentInConstructor:
Expand All @@ -29,18 +30,13 @@ def fetch_token
JSON.parse(response.body, symbolize_names: true)
end

# def token
# @token = get_token if token_expired?
# @token
# end

def oclc_token_url
URI.parse("#{Config.token_uri}?#{URI.encode_www_form(token_params)}")
end

# Before every request check if the token is expired (OCLC tokens expire after 20 minutes)
def access_token
@token = token if token_expired?
@token = fetch_token if token_expired?
@token[:access_token]
end

Expand Down
16 changes: 13 additions & 3 deletions spec/berkeley_library/location/world_cat/oclc_auth_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'time'

module BerkeleyLibrary
module Location
Expand All @@ -12,17 +13,26 @@ module WorldCat
end
end

it 'refreshes a token' do
it 'refreshes an expired token' do
VCR.use_cassette('oclc_auth/refresh_token') do
# First get a token....
token = OCLCAuth.instance.token

# Need to set the token expiration to a time in the past
token[:expires_at] = (Time.now - 1).to_s
token[:expires_at] = (Time.now - 60).to_s
token[:access_token] = 'expired_token'

# Now we need to set the token instance to the token with the updated expiration
OCLCAuth.instance.token = token

# Trigger a refresh by calling access_token
OCLCAuth.instance.access_token

expect(OCLCAuth.instance.token[:access_token]).to be_a(String)
# Now check that the token has been refreshed
token = OCLCAuth.instance.token

expect(token[:access_token]).not_to eq('expired_token')
expect(Time.parse(token[:expires_at])).to be >= Time.now
end
end

Expand Down
42 changes: 21 additions & 21 deletions spec/cassettes/libraries_request/execute_holdings_1.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions spec/cassettes/libraries_request/execute_holdings_2.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions spec/cassettes/libraries_request/execute_holdings_3.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading