Class: Tweets

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/twitter_filter/tweets.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Tweets

Returns a new instance of Tweets.



20
21
22
# File 'lib/twitter_filter/tweets.rb', line 20

def initialize(config={})
  @tweets = config[:tweets] || []
end

Instance Attribute Details

#tweetsObject

Returns the value of attribute tweets.



13
14
15
# File 'lib/twitter_filter/tweets.rb', line 13

def tweets
  @tweets
end

Class Method Details

.deserialize(save_name) ⇒ Object



30
31
32
33
# File 'lib/twitter_filter/tweets.rb', line 30

def self.deserialize(save_name)
  # returns Tweets object
  YAML.load(File.read(save_name))
end

Instance Method Details

#deserialize_tweets_array(save_name) ⇒ Object



41
42
43
# File 'lib/twitter_filter/tweets.rb', line 41

def deserialize_tweets_array(save_name)
  tweets = YAML.load(File.read(save_name))
end

#serialize(save_name) ⇒ Object



24
25
26
27
28
# File 'lib/twitter_filter/tweets.rb', line 24

def serialize(save_name)
  File.open(save_name, 'w') do |f|
    f.write YAML.dump(self)
  end
end

#serialize_tweets_array(save_name) ⇒ Object



35
36
37
38
39
# File 'lib/twitter_filter/tweets.rb', line 35

def serialize_tweets_array(save_name)
  File.open(save_name, 'w') do |f|
    f.write YAML.dump(tweets)
  end
end

#to_array_of_hashesObject



45
46
47
48
49
50
51
52
53
# File 'lib/twitter_filter/tweets.rb', line 45

def to_array_of_hashes
  # makes each Tweet a simple hash
  # then puts all the hashes into an array
  arr = []
  tweets.each do |tweet|
    arr << tweet.to_hash
  end
  arr
end

#to_jsonObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/twitter_filter/tweets.rb', line 55

def to_json
  # formats each Tweet using JSON
  # then returns a JSON array with all the tweets
  json_string = "["
  tweets.each do |tweet|
    json_string += tweet.to_json
    json_string += ','
  end
  json_string.gsub!(/,$/,"]")
end