class CorpusGenerator::Poem

Attributes

name[RW]
poet[RW]
text[RW]

Public Class Methods

all() click to toggle source

Class Methods

# File lib/random_poetry_scraper/poem.rb, line 37
def self.all
    @@all
end
new(attributes_hash) click to toggle source

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

# 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
poems_to_hash(poems) click to toggle source
# 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) click to toggle source
# File lib/random_poetry_scraper/poem.rb, line 49
def self.poems_to_json(poems)
    self.poems_to_hash(poems).to_json
end

Public Instance Methods

as_hash() click to toggle source
# 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
poet=(poet_attributes) click to toggle source

this expects that if there is a poet attribute, it will have a name. that might hardcode too much knowledge about the structure of the hash?

# File lib/random_poetry_scraper/poem.rb, line 18
def poet=(poet_attributes)
    poet = CorpusGenerator::Poet.find_or_create(poet_attributes)
    @poet = poet
    poet.add_poem(self)
end