Class: Marvel101::Topic

Inherits:
Object
  • Object
show all
Defined in:
lib/marvel_101/topic.rb

Direct Known Subclasses

Character, List, Team

Constant Summary collapse

LINE_LEN =
80
@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url) ⇒ Topic

Returns a new instance of Topic.



9
10
11
12
13
14
# File 'lib/marvel_101/topic.rb', line 9

def initialize(name, url)
  @name = name
  @urls = {url: url}
  @scraped = false
  @@all << self
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/marvel_101/topic.rb', line 3

def description
  @description
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/marvel_101/topic.rb', line 3

def name
  @name
end

#scrapedObject

Returns the value of attribute scraped.



3
4
5
# File 'lib/marvel_101/topic.rb', line 3

def scraped
  @scraped
end

#urlsObject

Returns the value of attribute urls.



3
4
5
# File 'lib/marvel_101/topic.rb', line 3

def urls
  @urls
end

Class Method Details

.allObject



72
73
74
# File 'lib/marvel_101/topic.rb', line 72

def self.all
  @@all
end

.find_or_create_by_name(name, url) ⇒ Object



67
68
69
70
# File 'lib/marvel_101/topic.rb', line 67

def self.find_or_create_by_name(name, url)
  search = @@all.detect {|topic| topic.name == name}
  search ? search : self.new(name, url)
end

Instance Method Details

#char?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/marvel_101/topic.rb', line 63

def char?
  self.is_a?(Marvel101::Character)
end

#display_descriptionObject



22
23
24
25
26
27
# File 'lib/marvel_101/topic.rb', line 22

def display_description
  if description
    format_output("DESCRIPTION: #{description}")
    puts "" if "DESCRIPTION: #{description}".size > 60
  end
end


29
30
31
32
33
34
35
# File 'lib/marvel_101/topic.rb', line 29

def display_links
  puts "" if urls.size > 1
  ["wiki", "101"].each do |url|
    output = "*Marvel #{url} available! Type '#{url}' to open in browser*"
    puts output if urls.include?("url_#{url}".to_sym)
  end
end

#format_output(text) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/marvel_101/topic.rb', line 37

def format_output(text)
  i = LINE_LEN
  while i < text.size
    line_break = text[0..i].rindex(" ")
    puts text[i - LINE_LEN...line_break]
    i = line_break + LINE_LEN + 1
  end
  puts text[i - LINE_LEN..-1]
end

#get_infoObject



16
17
18
19
20
# File 'lib/marvel_101/topic.rb', line 16

def get_info
  scraper = Marvel101::Scraper.new(self)
  self.list? ? scraper.scrape_list : scraper.scrape_topic
  @scraped = true
end

#has_team?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/marvel_101/topic.rb', line 51

def has_team?
  char? && team
end

#list?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/marvel_101/topic.rb', line 55

def list?
  self.is_a?(Marvel101::List)
end

#takes_input?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/marvel_101/topic.rb', line 47

def takes_input?
  list? || (team? && !members.empty?)
end

#team?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/marvel_101/topic.rb', line 59

def team?
  self.is_a?(Marvel101::Team)
end