Class: BerkeleyLibrary::Location::WorldCat::OCLCAuth
- Inherits:
-
Object
- Object
- BerkeleyLibrary::Location::WorldCat::OCLCAuth
- Includes:
- Singleton
- Defined in:
- lib/berkeley_library/location/world_cat/oclc_auth.rb
Instance Attribute Summary collapse
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
-
#access_token ⇒ Object
Before every request check if the token is expired (OCLC tokens expire after 20 minutes).
- #fetch_token ⇒ Object
-
#initialize ⇒ OCLCAuth
constructor
A new instance of OCLCAuth.
- #oclc_token_url ⇒ Object
Constructor Details
#initialize ⇒ OCLCAuth
Returns a new instance of OCLCAuth.
12 13 14 15 16 17 |
# File 'lib/berkeley_library/location/world_cat/oclc_auth.rb', line 12 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: end |
Instance Attribute Details
#token ⇒ Object
Returns the value of attribute token.
10 11 12 |
# File 'lib/berkeley_library/location/world_cat/oclc_auth.rb', line 10 def token @token end |
Instance Method Details
#access_token ⇒ Object
Before every request check if the token is expired (OCLC tokens expire after 20 minutes)
38 39 40 41 |
# File 'lib/berkeley_library/location/world_cat/oclc_auth.rb', line 38 def access_token @token = fetch_token if token_expired? @token[:access_token] end |
#fetch_token ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/berkeley_library/location/world_cat/oclc_auth.rb', line 19 def fetch_token url = oclc_token_url http = Net::HTTP.new(url.host, url.port) http.use_ssl = url.scheme == 'https' request = Net::HTTP::Post.new(url.request_uri) request.basic_auth(Config.api_key, Config.api_secret) request['Accept'] = 'application/json' response = http.request(request) JSON.parse(response.body, symbolize_names: true) end |