Class: CodingResources::Books

Inherits:
Object
  • Object
show all
Defined in:
lib/coding_resources/books.rb

Constant Summary collapse

@@all =
[]
@@pages =
[]
@@searched_books =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_urlObject

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_urlObject

Returns the value of attribute desc_url.



3
4
5
# File 'lib/coding_resources/books.rb', line 3

def desc_url
  @desc_url
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/coding_resources/books.rb', line 3

def description
  @description
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/coding_resources/books.rb', line 3

def name
  @name
end

Class Method Details

.allObject



19
20
21
# File 'lib/coding_resources/books.rb', line 19

def self.all
  @@all
end

.all_clearObject



23
24
25
# File 'lib/coding_resources/books.rb', line 23

def self.all_clear
  @@all.clear
end

.create_all_booksObject



57
58
59
60
61
# 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)
  self.create_pages("all_books")
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



84
85
86
# File 'lib/coding_resources/books.rb', line 84

def self.find_by_name(name)
  self.all.detect { |i| i.name == name  }
end

.list(page) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/coding_resources/books.rb', line 71

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

.pagesObject



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_clearObject



43
44
45
# File 'lib/coding_resources/books.rb', line 43

def self.pages_clear
  @@pages.clear
end

.search(name) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/coding_resources/books.rb', line 63

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
  self.create_pages("search")
end

.searched_booksObject



27
28
29
# File 'lib/coding_resources/books.rb', line 27

def self.searched_books
  @@searched_books
end

.searched_books_clearObject



31
32
33
# File 'lib/coding_resources/books.rb', line 31

def self.searched_books_clear
  @@searched_books.clear
end

Instance Method Details

#detailsObject



88
89
90
91
92
93
94
95
# File 'lib/coding_resources/books.rb', line 88

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