From 7447f13e5238281ad43c51ec34daa370abca5d42 Mon Sep 17 00:00:00 2001 From: Stephanie Wilkinson Date: Fri, 26 Jan 2024 11:15:20 -0800 Subject: [PATCH] rubocop and fix some overdrive features --- .rubocop.yml | 2 +- Rakefile | 2 +- app.rb | 7 +++---- config.ru | 2 +- lib/goodreads.rb | 14 +++++++------- lib/overdrive.rb | 21 ++++++++++++--------- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index ae6a3a2d..06bca8eb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,7 @@ require: rubocop-performance AllCops: - TargetRubyVersion: 2.7.1 + TargetRubyVersion: 3.2.3 EnabledByDefault: true #################### Bundler ############################## diff --git a/Rakefile b/Rakefile index e08ecec7..79de7b61 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/app.rb b/app.rb index a89a66d4..d52dd965 100644 --- a/app.rb +++ b/app.rb @@ -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' @@ -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 @@ -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 diff --git a/config.ru b/config.ru index ac41427f..299dbbb4 100644 --- a/config.ru +++ b/config.ru @@ -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 diff --git a/lib/goodreads.rb b/lib/goodreads.rb index 67814aa0..4fbed17e 100644 --- a/lib/goodreads.rb +++ b/lib/goodreads.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/overdrive.rb b/lib/overdrive.rb index 228e56ad..2f753a54 100644 --- a/lib/overdrive.rb +++ b/lib/overdrive.rb @@ -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'] @@ -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