repl.it
@jffrydsr/

ISBNdb Lookup 📚 --ruby

Ruby

ISBNdb Lookup 📚 | a quick terminal-based ISBNdb lookup tool in ruby and other languages.

fork
loading
Files
  • main.rb
  • LOG.json
  • registry.txt
  • Packager files
  • Gemfile
  • Gemfile.lock
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require "faraday"

$api_key = "43571_14a6b9a678cfd2bb934e10444647f4ad"
$base_url = "https://api2.isbndb.com"

def validate_isbn(isbn)
 if isbn.length == 10 
   puts "[Valid ✅ @ #{isbn}]: ISBN-10"
 elsif isbn.length == 13
   puts "[Valid ✅ @ #{isbn}]: ISBN-13"
 else
  puts "[Input Error ❌ @ #{isbn}]: Invalid ISBN"
 end 
end 

def lookup_isbn(isbn)
 resp = Faraday.get($base_url + '/book/' + isbn) do |req|
  req.headers['Authorization'] = $api_key
 end
 if resp.status == '404'
  puts "[ERROR: BAD REQUEST]"
  puts resp.status
 else
  info = JSON.parse(resp.body.to_json)
  File.open('LOG.json', 'a') { |f| f.write(info) }
end
end 

def load_registry(file_name)
 loaded = IO.readlines(file_name, chomp: true)
 loaded.each { |isbn|
 puts '================================='
  puts '[STAGE 1: Load & Validate]' 
  validate_isbn(isbn)
  puts '--------------------------------'
  puts '[Stage 2: Fetch & Output]'
  lookup_isbn(isbn)
  puts '[COMPLETE]'
  puts '================================'
  
  }

end 

load_registry("registry.txt")
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]
?