Class: CodingResources::Books
- Inherits:
-
Object
- Object
- CodingResources::Books
- Defined in:
- lib/coding_resources/books.rb
Constant Summary collapse
- @@all =
[]
- @@pages =
[]
- @@searched_books =
[]
Instance Attribute Summary collapse
-
#book_url ⇒ Object
Returns the value of attribute book_url.
-
#desc_url ⇒ Object
Returns the value of attribute desc_url.
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .all ⇒ Object
- .all_clear ⇒ Object
- .create_all_books ⇒ Object
- .create_from_collection(book_array) ⇒ Object
- .create_pages(books) ⇒ Object
- .find_by_name(name) ⇒ Object
- .list(page) ⇒ Object
- .pages ⇒ Object
- .pages=(pages) ⇒ Object
- .pages_clear ⇒ Object
- .search(name) ⇒ Object
- .searched_books ⇒ Object
- .searched_books_clear ⇒ Object
Instance Method Summary collapse
- #details ⇒ Object
-
#initialize(book_hash) ⇒ Books
constructor
A new instance of Books.
Constructor Details
#initialize(book_hash) ⇒ Books
Returns a new instance of Books.
9 10 11 12 13 |
# File 'lib/coding_resources/books.rb', line 9 def initialize(book_hash) @name = book_hash.values_at(:name).join @desc_url = book_hash.values_at(:desc_url).join @@all << self unless CodingResources::Books.find_by_name(self.name) end |
Instance Attribute Details
#book_url ⇒ Object
Returns the value of attribute book_url.
3 4 5 |
# File 'lib/coding_resources/books.rb', line 3 def book_url @book_url end |
#desc_url ⇒ Object
Returns the value of attribute desc_url.
3 4 5 |
# File 'lib/coding_resources/books.rb', line 3 def desc_url @desc_url end |
#description ⇒ Object
Returns the value of attribute description.
3 4 5 |
# File 'lib/coding_resources/books.rb', line 3 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/coding_resources/books.rb', line 3 def name @name end |
Class Method Details
.all ⇒ Object
19 20 21 |
# File 'lib/coding_resources/books.rb', line 19 def self.all @@all end |
.all_clear ⇒ Object
23 24 25 |
# File 'lib/coding_resources/books.rb', line 23 def self.all_clear @@all.clear end |
.create_all_books ⇒ Object
57 58 59 60 |
# File 'lib/coding_resources/books.rb', line 57 def self.create_all_books book_array = CodingResources::Scraper.scrape_all_books self.create_from_collection(book_array) end |
.create_from_collection(book_array) ⇒ Object
15 16 17 |
# File 'lib/coding_resources/books.rb', line 15 def self.create_from_collection(book_array) book_array.each { |book| self.new(book) } end |
.create_pages(books) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/coding_resources/books.rb', line 47 def self.create_pages(books) if books == "all_books" self.pages_clear self.pages = self.all.each_slice(15).to_a elsif books == "search" self.pages_clear self.pages = self.searched_books.each_slice(15).to_a end end |
.find_by_name(name) ⇒ Object
82 83 84 |
# File 'lib/coding_resources/books.rb', line 82 def self.find_by_name(name) self.all.detect { |i| i.name == name } end |
.list(page) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/coding_resources/books.rb', line 69 def self.list(page) puts "" puts "Page #{page + 1}" puts "" if self.pages[page] == nil puts "No reults found." else self.pages[page].each.with_index(1) do |book, i| puts "#{i}. #{book.name}" end end end |
.pages ⇒ Object
35 36 37 |
# File 'lib/coding_resources/books.rb', line 35 def self.pages @@pages end |
.pages=(pages) ⇒ Object
39 40 41 |
# File 'lib/coding_resources/books.rb', line 39 def self.pages=(pages) @@pages = pages end |
.pages_clear ⇒ Object
43 44 45 |
# File 'lib/coding_resources/books.rb', line 43 def self.pages_clear @@pages.clear end |
.search(name) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/coding_resources/books.rb', line 62 def self.search(name) self.searched_books_clear self.all.each do |book| @@searched_books << book if book.name.downcase.split(":").join.split(",").join.split.include?(name) end end |
.searched_books ⇒ Object
27 28 29 |
# File 'lib/coding_resources/books.rb', line 27 def self.searched_books @@searched_books end |
.searched_books_clear ⇒ Object
31 32 33 |
# File 'lib/coding_resources/books.rb', line 31 def self.searched_books_clear @@searched_books.clear end |
Instance Method Details
#details ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/coding_resources/books.rb', line 86 def details info = CodingResources::Scraper.scrape_book_details(self.desc_url) self.description = info.values_at(:description).join self.book_url = info.values_at(:book_url).join puts "#{self.description}" puts "" puts "To download the book please go to this website: #{self.book_url}" end |