Class: CorpusGenerator::Poem

Inherits:
Object
  • Object
show all
Defined in:
lib/random_poetry_scraper/poem.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes_hash) ⇒ Poem

TODO do you actually want to save all poems as soon as you initialize them?



9
10
11
12
13
14
# File 'lib/random_poetry_scraper/poem.rb', line 9

def initialize(attributes_hash)
    attributes_hash.each do |attribute, value|
        self.send("#{attribute}=", value)
    end
    self.class.all << self
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/random_poetry_scraper/poem.rb', line 2

def name
  @name
end

#poetObject

Returns the value of attribute poet.



2
3
4
# File 'lib/random_poetry_scraper/poem.rb', line 2

def poet
  @poet
end

#textObject

Returns the value of attribute text.



2
3
4
# File 'lib/random_poetry_scraper/poem.rb', line 2

def text
  @text
end

Class Method Details

.allObject

Class Methods



37
38
39
# File 'lib/random_poetry_scraper/poem.rb', line 37

def self.all
    @@all
end

.poems_to_hash(poems) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/random_poetry_scraper/poem.rb', line 41

def self.poems_to_hash(poems)
    poems_hash = {"poems" => []}
    poems.each do |poem| 
        poems_hash["poems"] << poem.as_hash
    end
    poems_hash
end

.poems_to_json(poems) ⇒ Object



49
50
51
# File 'lib/random_poetry_scraper/poem.rb', line 49

def self.poems_to_json(poems)
    self.poems_to_hash(poems).to_json
end

Instance Method Details

#as_hashObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/random_poetry_scraper/poem.rb', line 24

def as_hash
    {
        "name" => self.name,
        "text" => self.text,
        "poet" => {
            "name" => self.poet.name,
            "profile_url" => self.poet.profile_url,
        } 
    }
end