twitter_filter 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  # encoding: UTF-8
2
2
 
3
+ require 'tweetstream'
3
4
  require 'twitter_filter/tweet'
4
5
  require 'twitter_filter/tweets'
5
6
  require 'twitter_filter/get_tweets'
@@ -19,6 +19,7 @@ class GetTweets
19
19
  def initialize(oauth_file = nil)
20
20
  raise_no_oauth_file_exception unless oauth_file && File.exists?(File.expand_path(oauth_file))
21
21
  configure_tweetstream(File.expand_path(oauth_file))
22
+ tweets = Tweets.new
22
23
  end
23
24
 
24
25
  def tweets
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'tweetstream'
4
4
  require 'json'
5
+ require_relative 'tweets'
5
6
 
6
7
  class Twitter::Tweet
7
8
 
@@ -36,7 +37,7 @@ class Twitter::Tweet
36
37
  "#{user.screen_name} [#{created_at.to_s}]: #{text}"
37
38
  end
38
39
 
39
- def to_json
40
+ def to_hash
40
41
  tweet_hash =
41
42
  {"full_text" => full_text,
42
43
  "text" => text,
@@ -45,6 +46,10 @@ class Twitter::Tweet
45
46
  "display_time_ago" => display_time_ago,
46
47
  "user" => {"screen_name" => user.screen_name, "profile_image_url" => user.profile_image_url}}
47
48
  tweet_hash["in_reply_to_screen_name"] = in_reply_to_screen_name if in_reply_to_screen_name
48
- tweet_hash.to_json
49
+ tweet_hash
50
+ end
51
+
52
+ def to_json
53
+ to_hash.to_json
49
54
  end
50
55
  end
@@ -6,6 +6,7 @@ require 'date'
6
6
  require 'fileutils'
7
7
  require 'time'
8
8
  require 'forwardable'
9
+ require_relative 'tweet'
9
10
 
10
11
  class Tweets
11
12
 
@@ -14,23 +15,46 @@ class Tweets
14
15
  include Enumerable
15
16
  extend Forwardable
16
17
 
17
- def_delegators :@tweets, :each, :<<, :[], :[]=, :concat, :size, :length, :first, :last
18
+ def_delegators :@tweets, :each, :map, :<<, :[], :[]=, :concat, :size, :length, :first, :last
18
19
 
19
20
  def initialize(config={})
20
21
  @tweets = config[:tweets] || []
21
22
  end
22
23
 
23
24
  def serialize(save_name)
25
+ File.open(save_name, 'w') do |f|
26
+ f.write YAML.dump(self)
27
+ end
28
+ end
29
+
30
+ def self.deserialize(save_name)
31
+ # returns Tweets object
32
+ YAML.load(File.read(save_name))
33
+ end
34
+
35
+ def serialize_tweets_array(save_name)
24
36
  File.open(save_name, 'w') do |f|
25
37
  f.write YAML.dump(tweets)
26
38
  end
27
39
  end
28
40
 
29
- def deserialize(save_name)
41
+ def deserialize_tweets_array(save_name)
30
42
  tweets = YAML.load(File.read(save_name))
31
43
  end
32
44
 
45
+ def to_array_of_hashes
46
+ # makes each Tweet a simple hash
47
+ # then puts all the hashes into an array
48
+ arr = []
49
+ tweets.each do |tweet|
50
+ arr << tweet.to_hash
51
+ end
52
+ arr
53
+ end
54
+
33
55
  def to_json
56
+ # formats each Tweet using JSON
57
+ # then returns a JSON array with all the tweets
34
58
  json_string = "["
35
59
  tweets.each do |tweet|
36
60
  json_string += tweet.to_json
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module TwitterFilter
4
- VERSION = '0.0.1'
5
- DATE = '2013-03-07'
4
+ VERSION = '0.0.4'
5
+ DATE = '2013-03-08'
6
6
  end
data/spec/tweet_spec.rb CHANGED
@@ -7,7 +7,7 @@ require "fileutils"
7
7
  describe Twitter::Tweet do
8
8
 
9
9
  let(:yaml_file) { File.expand_path("../../fixtures/tweets/twenty_tweets_1.yml", __FILE__) }
10
- let(:tweets) { Tweets.new.deserialize(yaml_file).tweets }
10
+ let(:tweets) { Tweets.deserialize(yaml_file).tweets }
11
11
 
12
12
  describe "#display_string" do
13
13
 
data/spec/tweets_spec.rb CHANGED
@@ -15,23 +15,23 @@ describe Tweets do
15
15
  end
16
16
  end
17
17
 
18
- describe "#deserialize" do
18
+ describe ".deserialize" do
19
19
 
20
20
  it "should deserialize correctly" do
21
- Tweets.new.deserialize(in_yaml_file).tweets.length.should == 20
21
+ Tweets.deserialize(in_yaml_file).tweets.length.should == 20
22
22
  end
23
23
  end
24
24
 
25
25
  describe "#serialize" do
26
26
 
27
27
  it "should serialize correctly" do
28
- twenty_tweets = Tweets.new.deserialize(in_yaml_file)
28
+ twenty_tweets = Tweets.deserialize(in_yaml_file)
29
29
  twenty_tweets.serialize(File.expand_path(out_yaml_file))
30
- loaded_tweets = Tweets.new.deserialize(out_yaml_file)
31
- twenty_tweets.length.should == loaded_tweets.length
32
- twenty_tweets.first.user.screen_name.should == loaded_tweets.first.user.screen_name
33
- twenty_tweets.last.user.screen_name.should == loaded_tweets.last.user.screen_name
34
- twenty_tweets[12].full_text.should == loaded_tweets[12].full_text
30
+ loaded_tweets = Tweets.deserialize(out_yaml_file)
31
+ twenty_tweets.tweets.length.should == loaded_tweets.tweets.length
32
+ twenty_tweets.tweets.first.user.screen_name.should == loaded_tweets.tweets.first.user.screen_name
33
+ twenty_tweets.tweets.last.user.screen_name.should == loaded_tweets.tweets.last.user.screen_name
34
+ twenty_tweets.tweets[12].full_text.should == loaded_tweets.tweets[12].full_text
35
35
  end
36
36
  end
37
37
 
@@ -39,7 +39,7 @@ describe Tweets do
39
39
 
40
40
  it "should return the tweets formatted as JSON" do
41
41
  # can't test time part of string because time_ago is time-dependent
42
- tweets = Tweets.new.deserialize(in_yaml_file)
42
+ tweets = Tweets.deserialize(in_yaml_file)
43
43
  tweets.to_json.should match /\A\[/
44
44
  tweets.to_json.should match /]\Z/
45
45
  tweets.to_json.should include "{\"full_text\":\"RT @yougotmeven: (151618) @jbcerveja teu fc é a+ amei tudo\",\"text\":\"RT @yougotmeven: (151618) @jbcerveja teu fc é a+ amei tudo\",\"source\":\"web\",\"created_at\":\"2013-03-07 13:47:14 -0500\",\"display_time_ago\":\""
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-07 00:00:00.000000000 Z
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec