Skip to content

Commit

Permalink
rubocop and fix some overdrive features
Browse files Browse the repository at this point in the history
  • Loading branch information
stephaniewilkinson committed Jan 26, 2024
1 parent 200830d commit 7447f13
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require: rubocop-performance

AllCops:
TargetRubyVersion: 2.7.1
TargetRubyVersion: 3.2.3
EnabledByDefault: true

#################### Bundler ##############################
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace :db do
original_env = ENV.fetch('RACK_ENV', nil)
%w[test development].each do |env|
ENV['RACK_ENV'] = env
Dir['migrate/*'].sort.each do |migration|
Dir['migrate/*'].each do |migration|
sh "ruby #{migration}"
end
end
Expand Down
7 changes: 3 additions & 4 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class App < Roda
# route: GET /
r.root do
request_token = Auth.fetch_request_token
Cache.set session, request_token: request_token
Cache.set(session, request_token:)

@auth_url = request_token.authorize_url
view 'welcome'
Expand Down Expand Up @@ -142,7 +142,7 @@ class App < Roda
# route: POST /auth/shelves/:id/overdrive?consortium=1047
r.post do
titles = Overdrive.new(@book_info, r['consortium']).fetch_titles_availability
Cache.set session, titles: titles
Cache.set(session, titles:)
r.redirect '/auth/availability'
end
end
Expand Down Expand Up @@ -184,8 +184,7 @@ class App < Roda
r.redirect "shelves/#{@shelf_name}/overdrive"
end

@local_libraries = Overdrive.local_libraries zip.to_latlon.delete ' '

@local_libraries = Overdrive.local_libraries zip.delete ' '
Cache.set session, libraries: @local_libraries
r.redirect '/auth/library'
end
Expand Down
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ else
logger = Logger.new $stdout
logger.level = Logger::DEBUG

Unreloader = Rack::Unreloader.new(subclasses: %w[Roda Sequel::Model], logger: logger, reload: true) { App }
Unreloader = Rack::Unreloader.new(subclasses: %w[Roda Sequel::Model], logger:, reload: true) { App }
Unreloader.require('app.rb') { 'App' }
run Unreloader
end
14 changes: 7 additions & 7 deletions lib/goodreads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Goodreads
API_KEY = ENV.fetch 'GOODREADS_API_KEY'
GENDER_DETECTOR = GenderDetector.new
HOST = 'www.goodreads.com'
BASE_URL = "https://#{HOST}"
BASE_URL = "https://#{HOST}".freeze
GOODREADS_SECRET = ENV.fetch 'GOODREADS_SECRET'
BOOK_DETAILS = %w[isbn book/image_url title authors/author/name published rating].freeze

Expand Down Expand Up @@ -94,11 +94,11 @@ def get_book_details bodies

data.map do |isbn, image_url, title, author, published_year, rating|
{
isbn: isbn,
image_url: image_url,
title: title,
author: author,
published_year: published_year,
isbn:,
image_url:,
title:,
author:,
published_year:,
ratings: rating
}
end
Expand Down Expand Up @@ -153,7 +153,7 @@ def fetch_book_data isbn
doc = Nokogiri::XML(response.read)
title = doc.xpath('//title').text
image_url = doc.xpath('//image_url').first.text
book = Book.new title: title, image_url: image_url, isbn: isbn
book = Book.new(title:, image_url:, isbn:)

[:ok, book]
else
Expand Down
21 changes: 12 additions & 9 deletions lib/overdrive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@

class Overdrive
BASE_URL = 'https://api.overdrive.com'
API_URI = "#{BASE_URL}/v1"
MAPBOX_URI = 'https://www.overdrive.com/mapbox/find-libraries-by-location'
API_URI = "#{BASE_URL}/v1".freeze
MAPBOX_URI = 'https://www.overdrive.com/mapbox/find-libraries-by-query'
OAUTH_URI = 'https://oauth.overdrive.com'
KEY = ENV.fetch 'OVERDRIVE_KEY'
SECRET = ENV.fetch 'OVERDRIVE_SECRET'

Title = Struct.new :title, :image, :copies_available, :copies_owned, :isbn, :url, :id, :availability_url, keyword_init: true

def self.local_libraries latlon
def self.local_libraries zip_code
# here is teh url that overdrive uses to search
# https://www.overdrive.com/mapbox/find-libraries-by-query?query=91302&includePublicLibraries=true&includeSchoolLibraries=true&sort=distance

task = Async do
internet = Async::HTTP::Internet.new
params = URI.encode_www_form latLng: latlon, radius: 50

params = URI.encode_www_form query: zip_code, includePublicLibraries: true, includeSchoolLibraries: false
response = internet.get "#{MAPBOX_URI}?#{params}"
response.read
ensure
internet&.close
end

libraries = JSON.parse task.wait

# they have changed the JSON payload here so it only responds with the 'consortium' and the libraries are nested
# maybe this is ok though
libraries.first(10).map do |l|
consortium_id = l['consortiumId']
consortium_name = l['consortiumName']
Expand Down Expand Up @@ -138,11 +140,12 @@ def async_books_with_overdrive_info
task.with_timeout 25 do
books = []

@book_info.each.with_index 1 do |book, book_number|
@book_info.each.with_index 1 do |book, _book_number|
barrier.async do
response = client.get(availability_path(book), 'Authorization' => "Bearer #{@token}")
body = response.read
Async.logger.info "Book number #{book_number} of #{@book_info.size} response code: #{response.status}"

warn "Book number #{book_number} of #{@book_info.size} response code: #{response.status}"

books << [title(book), body]
end
Expand Down

0 comments on commit 7447f13

Please sign in to comment.