glibrary 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4ac86212be3fab6a235e0216dad02d0f839d9aa47dd156e3c7a9a4d1859e96a
4
- data.tar.gz: fc6357e250be3d23b5581b230ad5b2ab33c450acd080fa71392a2da66ca8ef93
3
+ metadata.gz: 500668b0fef4bc0c293f2f6cb76922df63fdd885f2b7c3bbac04a32f75dbe9cc
4
+ data.tar.gz: d0f87c340194b3ab7f543f94c99601d2f20ce571694e3ce5ac51dda30f41e65d
5
5
  SHA512:
6
- metadata.gz: 050d5cb1869f8bc163d26eb4fe623acae8582ccd26f4c8b177fc0bca8bbe6c8b3cc9db6802c857b396375bfc77c32eb92d1a70cd855446387f34a96077f1d800
7
- data.tar.gz: ed586d2e862b1c2402016447b75a3e3be642b590363359b35cf97b495e0fe5c4a885f9a390a24379c81b08e813210875d723ca8f38bff3c4619aa8825d634dd1
6
+ metadata.gz: f16dab8aeeb001c3470a0bfe513a398ac4d235fbfd024fe3f148fa5273eb2c81e3a931cbefc138191bbb3b0994e4f151b517a64c52d16e5d153dcda78b456b6e
7
+ data.tar.gz: b045b4f3bbdc9afa329afd481dc3301115fd06406d7b6c1270f46737888e13de254ad88ddecb4ff01e91fce8ab6f4ec30f2816fea83c8b35ccac109eca332848
data/bin/glibrary CHANGED
@@ -5,6 +5,7 @@ require 'io/console'
5
5
  require_relative '../modules/api_query'
6
6
  require_relative '../modules/command_line_parse'
7
7
  require_relative '../modules/errors'
8
+ require_relative '../modules/error_handler'
8
9
  require_relative '../modules/exec_helper'
9
10
  require_relative '../modules/google_api_url'
10
11
  require_relative '../modules/user_prompt'
@@ -16,44 +17,37 @@ require_relative '../lib/user_library'
16
17
  include ExecHelper
17
18
  include UserPrompt
18
19
  include CommandLineParse
20
+ include ErrorHandler
19
21
 
20
22
  CURRENT_DIR = File.join(File.dirname(__FILE__))
21
23
  DEFAULT_FILENAME = File.join File.expand_path("..", CURRENT_DIR), "saved_libraries", "library.json"
22
24
 
23
25
  process_args!
24
26
 
25
- options = {}
26
- filename = DEFAULT_FILENAME
27
- filename, options = CommandLineParse::command_line_parse!(filename, options)
27
+ @options = {}
28
+ @filename = DEFAULT_FILENAME
29
+ command_line_parse!
28
30
 
29
- prepare_filename_for_os!(filename)
31
+ prepare_filename_for_os!
30
32
 
31
- persistent_library = UserLibrary.new(filename: filename)
33
+ if @options.nil?
32
34
 
33
- temp_booklist = UserLibrary.new(nonpersistent: true)
35
+ with_library_mode_error_handling do
36
+ (@library = UserLibrary.new(filename: @filename)).pretty_print
37
+ library_mode_user_prompt
38
+ end
34
39
 
35
- begin
36
- search_results = ExecHelper::perform_search(options)
37
- search_results.selected_results.each { |info| temp_booklist.add(UserBook.new(info)) }
40
+ else
38
41
 
39
- temp_booklist.pretty_print
42
+ with_search_mode_error_handling do
43
+ @persistent_library = UserLibrary.new(filename: @filename)
44
+ @temp_booklist = UserLibrary.new(nonpersistent: true)
40
45
 
41
- search_user_prompt(temp_booklist, persistent_library)
46
+ search_results = perform_search
47
+ search_results.selected_results.each { |info| @temp_booklist.add(UserBook.new(info)) }
48
+ @temp_booklist.pretty_print
49
+
50
+ search_mode_user_prompt
51
+ end
42
52
 
43
- rescue UserQuits
44
- puts "\nThanks for browsing. Library file saved"
45
- puts ""
46
- rescue Errno::EACCES
47
- puts "\nYou don't have permission to write to the file you specified. Quitting immediately"
48
- puts ""
49
- rescue NoInternetError
50
- puts "\nNo internet connection. Please connect to the internet and try again."
51
- puts ""
52
- rescue SearchError
53
- puts "\nThere was an error with your query; be careful to format it well"
54
- puts ""
55
- rescue NoResults
56
- puts "\nNo results."
57
- puts ""
58
53
  end
59
- exit
data/lib/user_library.rb CHANGED
@@ -97,12 +97,12 @@ class UserLibrary
97
97
  def set_filename(name)
98
98
  @nonpersistent = false
99
99
  @filename = name
100
- prepare_filename_for_os!(@filename)
100
+ prepare_filename_for_os!
101
101
  end
102
102
 
103
103
  def determine_filename!
104
104
  @filename = File.absolute_path(@filename)
105
- prepare_filename_for_os!(@filename)
105
+ prepare_filename_for_os!
106
106
  end
107
107
 
108
108
  def get_raw_JSON_data
@@ -6,11 +6,9 @@ require_relative '../lib/user_library'
6
6
  module CommandLineParse
7
7
  require 'optparse'
8
8
  include Errors
9
- include UserPrompt
10
- include ExecHelper
11
9
 
12
- def handle_nonexistent_file(filename, library)
13
- (puts "Library file not found, cannot display."; exit) if library
10
+ def handle_nonexistent_file
11
+ (puts "Library file not found, cannot display."; exit)
14
12
  puts "Creating new file"
15
13
  end
16
14
 
@@ -21,46 +19,40 @@ module CommandLineParse
21
19
  puts ""
22
20
  end
23
21
 
24
-
25
- def command_line_parse!(filename, o, suppress = nil)
26
-
22
+ def command_line_parse!
27
23
  OptionParser.new do |opts|
28
24
  opts.banner = "Usage: glibrary [options...] [query]"
29
- @library = library = opts.default_argv.include?("-l")
30
25
 
31
26
  opts.on("-t", "--title=TITLE", "Specify a title keyword") do |t|
32
- o[:title] = t
27
+ @options[:title] = t
33
28
  end
34
29
 
35
30
  opts.on("-a", "--author=AUTHOR", "Specify an author keyword") do |a|
36
- o[:author] = a
31
+ @options[:author] = a
37
32
  end
38
33
 
39
34
  opts.on("-p", "--publisher=PUBLISHER", "Specify a publisher keyword") do |p|
40
- o[:publisher] = p
35
+ @options[:publisher] = p
41
36
  end
42
37
 
43
38
  opts.on("-f", "--lib-file=LIBFILE",
44
39
  "Select a library save file. Otherwise, a default save file will be used.") do |libfile|
45
- filename = File.absolute_path(libfile)
46
- handle_nonexistent_file(filename, library) unless File.exist?(filename)
40
+ @filename = File.absolute_path(libfile)
41
+ handle_nonexistent_file if ARGV.include?("-l") && !File.exist?(@filename)
47
42
  end
48
43
 
49
44
  opts.on("-l", "--library", "See your library; ignores all search options") do
50
- prepare_filename_for_os!(filename)
51
- unless suppress
52
- (l = UserLibrary.new(filename: filename)).pretty_print
53
- library_mode_user_prompt(l)
54
- exit
55
- end
45
+ @options = nil
46
+ return
56
47
  end
57
48
 
58
49
  opts.on("-h", "--help", "Prints this help") do
59
50
  print_help_message(opts)
60
51
  exit
61
52
  end
62
-
63
53
  end.parse!
64
- return [(@filename = filename), o]
54
+
55
+ @options[:search] = ARGV.join('+')
56
+
65
57
  end
66
58
  end
@@ -0,0 +1,38 @@
1
+ require_relative 'errors'
2
+
3
+ module ErrorHandler
4
+ def with_library_mode_error_handling
5
+ begin
6
+ yield
7
+ rescue UserQuits
8
+ puts "\nEnjoy your day."
9
+ rescue Errno::EACCES
10
+ puts "\nYou don't have permission to write to the file you specified. Quitting immediately"
11
+ puts ""
12
+ end
13
+ exit
14
+ end
15
+
16
+ def with_search_mode_error_handling
17
+ begin
18
+ yield
19
+ rescue UserQuits
20
+ puts "\nThanks for browsing. Library file saved"
21
+ puts ""
22
+ rescue Errno::EACCES
23
+ puts "\nYou don't have permission to write to the file you specified. Quitting immediately"
24
+ puts ""
25
+ rescue NoInternetError
26
+ puts "\nNo internet connection. Please connect to the internet and try again."
27
+ puts ""
28
+ rescue SearchError
29
+ puts "\nThere was an error with your query; be careful to format it well"
30
+ puts ""
31
+ rescue NoResults
32
+ puts "\nNo results."
33
+ puts ""
34
+ end
35
+ exit
36
+ end
37
+
38
+ end
@@ -8,13 +8,13 @@ module ExecHelper
8
8
  args << args.delete("-l") if args.include?("-l")
9
9
  end
10
10
 
11
- def prepare_filename_for_os!(filename)
12
- filename.gsub!(/\//, '\\') if ENV.values.any? { |v| v =~ /[A-Z]:\\Windows/i }
11
+ def prepare_filename_for_os!
12
+ @filename.gsub!(/\//, '\\') if ENV.values.any? { |v| v =~ /[A-Z]:\\Windows/i }
13
13
  end
14
14
 
15
- def perform_search(o)
16
- search = o[:search] || ARGV.join('+')
17
- s = BookSearch.new(search: search, title: o[:title], author: o[:author],
18
- publisher: o[:publisher])
15
+ def perform_search
16
+ search = @options[:search] || ARGV.join('+')
17
+ s = BookSearch.new(search: search, title: @options[:title], author: @options[:author],
18
+ publisher: @options[:publisher])
19
19
  end
20
20
  end
@@ -26,14 +26,15 @@ module UserPrompt
26
26
  return s
27
27
  end
28
28
 
29
- def library_mode_user_prompt(library)
29
+ def library_mode_user_prompt
30
30
  while true
31
31
  begin
32
32
  i = prompt( "Select a book number to delete, or type \"q\" to quit: ", /\A([0-9]+|[qQ])\Z/,
33
- library )
33
+ @library )
34
34
 
35
- library.delete(i)
36
- library.pretty_print
35
+ @library.delete(i)
36
+ @library.save
37
+ @library.pretty_print
37
38
 
38
39
  puts "Your library now looks like this."
39
40
  puts ""
@@ -41,25 +42,20 @@ module UserPrompt
41
42
  puts "\nSorry, your selection was invalid. Please try again."
42
43
  rescue NotABook
43
44
  puts "\nSorry, your selection was invalid. Make sure your selection is in the provided range."
44
- rescue UserQuits
45
- library.save
46
- puts "\nEnjoy your day."
47
- exit
48
45
  end
49
46
  end
50
47
  end
51
48
 
52
49
 
53
- def search_user_prompt(temp_booklist, persistent_library)
50
+ def search_mode_user_prompt
54
51
  while true
55
52
  begin
56
-
57
53
  i = prompt( "Enter a number (1-5) to add a book to your reading list (or q to quit): ",
58
- /\A[1-5]|[qQ]\Z/, temp_booklist )
59
- selected_book = temp_booklist[i]
54
+ /\A[1-5]|[qQ]\Z/, @temp_booklist )
55
+ selected_book = @temp_booklist[i]
60
56
 
61
- persistent_library << selected_book
62
- persistent_library.save
57
+ @persistent_library << selected_book
58
+ @persistent_library.save
63
59
 
64
60
  handle_successful_prompt_completion(selected_book)
65
61
  puts "Would you like to add another?"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glibrary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Engelbert
@@ -24,6 +24,7 @@ files:
24
24
  - lib/user_library.rb
25
25
  - modules/api_query.rb
26
26
  - modules/command_line_parse.rb
27
+ - modules/error_handler.rb
27
28
  - modules/errors.rb
28
29
  - modules/exec_helper.rb
29
30
  - modules/google_api_url.rb