Class: BerkeleyLibrary::Location::WorldCat::OCLCAuth

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/berkeley_library/location/world_cat/oclc_auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOCLCAuth

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

#tokenObject

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_tokenObject

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_tokenObject



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

#oclc_token_urlObject



33
34
35
# File 'lib/berkeley_library/location/world_cat/oclc_auth.rb', line 33

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