wbookstore 0.0.1

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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.idea/.rakeTasks +7 -0
  3. data/.idea/current.iml +35 -0
  4. data/.idea/misc.xml +14 -0
  5. data/.idea/modules.xml +8 -0
  6. data/.idea/vcs.xml +6 -0
  7. data/.idea/workspace.xml +424 -0
  8. data/Gemfile +4 -0
  9. data/Gemfile.lock +34 -0
  10. data/RakeFile.rb +11 -0
  11. data/coverage/.last_run.json +5 -0
  12. data/coverage/.resultset.json +146 -0
  13. data/coverage/.resultset.json.lock +0 -0
  14. data/coverage/assets/0.10.0/application.css +799 -0
  15. data/coverage/assets/0.10.0/application.js +1707 -0
  16. data/coverage/assets/0.10.0/colorbox/border.png +0 -0
  17. data/coverage/assets/0.10.0/colorbox/controls.png +0 -0
  18. data/coverage/assets/0.10.0/colorbox/loading.gif +0 -0
  19. data/coverage/assets/0.10.0/colorbox/loading_background.png +0 -0
  20. data/coverage/assets/0.10.0/favicon_green.png +0 -0
  21. data/coverage/assets/0.10.0/favicon_red.png +0 -0
  22. data/coverage/assets/0.10.0/favicon_yellow.png +0 -0
  23. data/coverage/assets/0.10.0/loading.gif +0 -0
  24. data/coverage/assets/0.10.0/magnify.png +0 -0
  25. data/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  26. data/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  27. data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  28. data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  29. data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  30. data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  31. data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  32. data/coverage/assets/0.10.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  33. data/coverage/assets/0.10.0/smoothness/images/ui-icons_222222_256x240.png +0 -0
  34. data/coverage/assets/0.10.0/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  35. data/coverage/assets/0.10.0/smoothness/images/ui-icons_454545_256x240.png +0 -0
  36. data/coverage/assets/0.10.0/smoothness/images/ui-icons_888888_256x240.png +0 -0
  37. data/coverage/assets/0.10.0/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  38. data/coverage/index.html +490 -0
  39. data/lib/BookStoreCore.rb +52 -0
  40. data/lib/bookstore.rb +95 -0
  41. data/lib/bookstore_test.rb +32 -0
  42. data/lib/fi.json +1 -0
  43. data/spec/BookStoreCore_spec.rb +38 -0
  44. data/spec/spec_helper.rb +4 -0
  45. data/wbookstore.gemspec +17 -0
  46. metadata +86 -0
@@ -0,0 +1,52 @@
1
+ require 'json'
2
+
3
+ # core class for bookstore
4
+ class BookStoreCore
5
+ def writef(path, book_hash)
6
+ File.open(path, 'w') do |f|
7
+ f.write(book_hash.to_json)
8
+ end
9
+ end
10
+
11
+ def readf(path)
12
+ file = File.read(path)
13
+ JSON.parse(file)
14
+ end
15
+
16
+ def exist?(title, book_hash)
17
+ is_exist = false
18
+ book_hash.each do |k, _v|
19
+ is_exist = true if k.to_s == title
20
+ end
21
+ is_exist
22
+ end
23
+
24
+ def add_update_book(title, rating, book_hash)
25
+ book_hash[title.to_sym] = rating.to_i
26
+ book_hash
27
+ end
28
+
29
+ def delete_book(title, book_hash)
30
+ updated_books = {}
31
+ book_hash.each do |k, _v|
32
+ updated_books[k] = v if k.to_s != title
33
+ end
34
+ updated_books
35
+ end
36
+
37
+ def get_full_title(start_chars, book_hash)
38
+ output_string = ''
39
+ book_hash.each do |title, _rating|
40
+ output_string += title.to_s if title.to_s.include? start_chars
41
+ end
42
+ output_string
43
+ end
44
+
45
+ def get_all_books(book_hash)
46
+ output_string = ''
47
+ book_hash.each do |book, rating|
48
+ output_string += "#{book} has rating #{rating}."
49
+ end
50
+ output_string
51
+ end
52
+ end
data/lib/bookstore.rb ADDED
@@ -0,0 +1,95 @@
1
+ require 'json'
2
+ require 'BookStoreCore'
3
+
4
+ # Bookstore running script
5
+ class BookStore
6
+ def initialize
7
+ puts 'Store initialized!'
8
+ end
9
+
10
+ def main_store
11
+ case main_message
12
+ when 'add' then add_case
13
+ when 'update' then update_case
14
+ when 'search' then search_all_case
15
+ when 'all'
16
+ bs = BookStoreCore.new
17
+ puts bs.get_all_books(bs.readf('fi.json'))
18
+ when 'delete' then delete_case
19
+ else puts 'Sorry, wrong input!'
20
+ end
21
+ end
22
+
23
+ def main_message
24
+ puts 'What would you like to do?'
25
+ puts "-- Type 'add' to Add a book."
26
+ puts "-- Type 'update' to Update a book."
27
+ puts "-- Type 'search' to Display all books."
28
+ puts "-- Type 'all' to Display all books."
29
+ puts "-- Type 'delete' to Delete a book."
30
+ choice = gets.chomp.downcase
31
+ choice
32
+ end
33
+
34
+ def search_all_case
35
+ puts 'Please add starting characters.'
36
+ start_chars = gets.chomp
37
+ bs = BookStoreCore.new
38
+ puts bs.get_full_title(start_chars, bs.readf('fi.json'))
39
+ end
40
+
41
+ def add_case
42
+ puts 'What is the title of the book?'
43
+ title = gets.chomp
44
+ bs = BookStoreCore.new
45
+ if bs.is_exist(title, bs.readf('fi.json'))
46
+ puts 'This book already exist.'
47
+ else
48
+ add_book(title)
49
+ end
50
+ end
51
+
52
+ def add_book(title)
53
+ puts 'What is the rating of the book?'
54
+ rating = gets.chomp
55
+ bs = BookStoreCore.new
56
+ book = bs.add_update_book(title, rating, bs.readf('fi.json'))
57
+ bs.writef('fi.json', book)
58
+ puts 'Added successfully.'
59
+ end
60
+
61
+ def update_case
62
+ puts 'Which book do you want to update?'
63
+ title = gets.chomp
64
+ decide_update(title)
65
+ end
66
+
67
+ def decide_update(title)
68
+ bs = BookStoreCore.new
69
+ if bs.is_exist(title, bs.readf('fi.json'))
70
+ puts 'What is the rating of the book?'
71
+ rating = gets.chomp
72
+ book = bs.add_update_book(title, rating, bs.readf('fi.json'))
73
+ bs.writef('fi.json', book)
74
+ puts 'Updated successfully.'
75
+ else
76
+ puts 'This book does not exist.'
77
+ end
78
+ end
79
+
80
+ def delete_case
81
+ puts 'Which book do you want to delete?'
82
+ title = gets.chomp
83
+ bs = BookStoreCore.new
84
+ if bs.is_exist(title, bs.readf('fi.json'))
85
+ book_hash = bs.delete_book(title, bs.readf('fi.json'))
86
+ bs.writef('fi.json', book_hash)
87
+ puts 'Deleted successfully.'
88
+ else
89
+ puts 'This book does not exist.'
90
+ end
91
+ end
92
+ end
93
+
94
+ # initializing the bookstore
95
+ BookStore.new.main_store
@@ -0,0 +1,32 @@
1
+ require_relative 'spec/spec_helper'
2
+ require 'minitest/autorun'
3
+ require './BookStoreCore'
4
+
5
+ class TestBookStore < Minitest::Test
6
+
7
+ def setup
8
+ @BookStore = BookStoreCore.new
9
+ @bookHash={"Bone":2,"Btwo":2,"Bthree":1,"Bfour":4}
10
+ end
11
+
12
+ def test_is_exist
13
+ assert_equal true, @BookStore.exist?("Btwo",{"Bone":2,"Btwo":2,"Bthree":1,"Bfour":4})
14
+ end
15
+
16
+ def test_get_full_title
17
+ assert_equal ('B-one'), @BookStore.get_full_title("B-on",{"B-one":2,"B-two":2,"B-three":1,"B-four":4})
18
+ end
19
+
20
+ def test_get_all_books
21
+ assert_equal ("B-one has rating 2."), @BookStore.get_all_books({"B-one":2})
22
+ end
23
+
24
+
25
+ def test_add_update_book
26
+ assert_equal ({"B-one":2,"B-two":2,"B-three":1,"B-four":4,"B-to":2}), @BookStore.add_update_book("B-to",2,{"B-one":2,"B-two":2,"B-three":1,"B-four":4})
27
+ end
28
+
29
+ def test_delete_book
30
+ assert_equal ({"B-one":2,"B-three":1,"B-four":4}), @BookStore.delete_book("B-two",{"B-one":2,"B-two":2,"B-three":1,"B-four":4})
31
+ end
32
+ end
data/lib/fi.json ADDED
@@ -0,0 +1 @@
1
+ {"Bone":2,"Btwo":2,"Bfour":4,"Bfive":3}
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require './BookStoreCore'
3
+
4
+ describe BookStoreCore do
5
+ book_store = BookStoreCore.new
6
+
7
+ describe "#get_all_books" do
8
+ it "returns All books" do
9
+ expected_string = "B-one has rating 2."
10
+ expect(book_store.get_all_books({"B-one":2})).to eq(expected_string)
11
+ end
12
+ end
13
+
14
+ describe "#get_full_title" do
15
+ it "Gets title of given book." do
16
+ expect(book_store.get_full_title("B-on",{"B-one":2,"B-two":2,"B-three":1,"B-four":4})).to eq('B-one')
17
+ end
18
+ end
19
+
20
+ describe "#is_exist" do
21
+ it "Checks if a book exists." do
22
+ expect(book_store.exist?("Btwo",{"Bone":2,"Btwo":2,"Bthree":1,"Bfour":4})).to eq(true)
23
+ end
24
+ end
25
+
26
+ describe "#add_update_book" do
27
+ it "Adds or update a book." do
28
+ expect(book_store.add_update_book("B-to",2,{"B-one":2,"B-two":2,"B-three":1,"B-four":4})).to eq({"B-one":2,"B-two":2,"B-three":1,"B-four":4,"B-to":2})
29
+ end
30
+ end
31
+
32
+ describe "#delete_book" do
33
+ it "Delets a book." do
34
+ expect(book_store.delete_book("B-two",{"B-one":2,"B-two":2,"B-three":1,"B-four":4}) ).to eq({"B-one":2,"B-three":1,"B-four":4})
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,4 @@
1
+ require 'simplecov'
2
+ SimpleCov.start if ENV['COVERAGE']
3
+
4
+ require_relative '../BookStoreCore'
@@ -0,0 +1,17 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.name = 'wbookstore'
4
+ s.version = '0.0.1'
5
+ s.date = '2016-10-04'
6
+ s.summary = "BookStore!"
7
+ s.description = "A simple BookStore"
8
+ s.authors = ["Waqar Haider"]
9
+ s.email = 'waqar.haider@confiz.com'
10
+ s.files = `git ls-files`.split("\n")
11
+ s.homepage =
12
+ 'http://rubygems.org/gems/waqar-bookstore'
13
+ end
14
+
15
+
16
+
17
+
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wbookstore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Waqar Haider
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple BookStore
14
+ email: waqar.haider@confiz.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".idea/.rakeTasks"
20
+ - ".idea/current.iml"
21
+ - ".idea/misc.xml"
22
+ - ".idea/modules.xml"
23
+ - ".idea/vcs.xml"
24
+ - ".idea/workspace.xml"
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - RakeFile.rb
28
+ - coverage/.last_run.json
29
+ - coverage/.resultset.json
30
+ - coverage/.resultset.json.lock
31
+ - coverage/assets/0.10.0/application.css
32
+ - coverage/assets/0.10.0/application.js
33
+ - coverage/assets/0.10.0/colorbox/border.png
34
+ - coverage/assets/0.10.0/colorbox/controls.png
35
+ - coverage/assets/0.10.0/colorbox/loading.gif
36
+ - coverage/assets/0.10.0/colorbox/loading_background.png
37
+ - coverage/assets/0.10.0/favicon_green.png
38
+ - coverage/assets/0.10.0/favicon_red.png
39
+ - coverage/assets/0.10.0/favicon_yellow.png
40
+ - coverage/assets/0.10.0/loading.gif
41
+ - coverage/assets/0.10.0/magnify.png
42
+ - coverage/assets/0.10.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png
43
+ - coverage/assets/0.10.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png
44
+ - coverage/assets/0.10.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png
45
+ - coverage/assets/0.10.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png
46
+ - coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png
47
+ - coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png
48
+ - coverage/assets/0.10.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png
49
+ - coverage/assets/0.10.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png
50
+ - coverage/assets/0.10.0/smoothness/images/ui-icons_222222_256x240.png
51
+ - coverage/assets/0.10.0/smoothness/images/ui-icons_2e83ff_256x240.png
52
+ - coverage/assets/0.10.0/smoothness/images/ui-icons_454545_256x240.png
53
+ - coverage/assets/0.10.0/smoothness/images/ui-icons_888888_256x240.png
54
+ - coverage/assets/0.10.0/smoothness/images/ui-icons_cd0a0a_256x240.png
55
+ - coverage/index.html
56
+ - lib/BookStoreCore.rb
57
+ - lib/bookstore.rb
58
+ - lib/bookstore_test.rb
59
+ - lib/fi.json
60
+ - spec/BookStoreCore_spec.rb
61
+ - spec/spec_helper.rb
62
+ - wbookstore.gemspec
63
+ homepage: http://rubygems.org/gems/waqar-bookstore
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.6.6
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: BookStore!
86
+ test_files: []