psu-libraries/psulib_blacklight

View on GitHub
app/controllers/links/google_preview_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

require 'net/http'

module Links
  class GooglePreviewController < ActionController::API
    def data
      render json: json_response
    end

    private

      def json_response
        JSON.parse(send_request.gsub('var _GBSBookInfo = ', '')[0..-2])
      end

      def send_request
        Net::HTTP.get(uri)
      end

      def uri
        URI("https://books.google.com/books?jscmd=viewapi&bibkeys=#{search_item}")
      end

      def search_item
        params['search_item']
      end
  end
end