sarnesjo-twhere 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README.rdoc +32 -2
  2. data/bin/twhere +25 -3
  3. data/lib/twhere.rb +22 -32
  4. metadata +1 -1
data/README.rdoc CHANGED
@@ -1,6 +1,36 @@
1
- = twhere
1
+ = Twhere
2
2
 
3
- Description goes here.
3
+ Twhere is a Twitter/Google Maps mashup that uses hashtags to put your tweets on
4
+ the map.
5
+
6
+ == Installation
7
+
8
+ Install via RubyGems:
9
+
10
+ gem sources -a http://gems.github.com
11
+ sudo gem install sarnesjo-twhere
12
+
13
+ Copy and edit example files and do a test run:
14
+
15
+ twhere -c config.yml >test.html
16
+
17
+ Put something like this in your crontab:
18
+
19
+ */5 * * * * /usr/bin/twhere -c /path/to/config.yml >/path/to/index.html 2>>/path/to/err.log
20
+
21
+ == Configuration
22
+
23
+ === Locations file
24
+
25
+ === Template file
26
+
27
+ === Tweets file
28
+
29
+ === Twitter
30
+
31
+ === Google Maps
32
+
33
+ == Usage
4
34
 
5
35
  == Copyright
6
36
 
data/bin/twhere CHANGED
@@ -4,12 +4,13 @@ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
  require 'twhere'
5
5
  require 'optparse'
6
6
 
7
- options = {}
7
+ config_file = nil
8
+
8
9
  OptionParser.new do |opts|
9
10
  opts.banner = 'usage: twhere.rb [options]'
10
11
 
11
12
  opts.on('-c', '--config FILE', 'specify config file') do |c|
12
- options[:config_file] = c
13
+ config_file = c
13
14
  end
14
15
 
15
16
  opts.on_tail('-h', '--help', 'show this message') do
@@ -24,4 +25,25 @@ OptionParser.new do |opts|
24
25
  end
25
26
  end.parse!
26
27
 
27
- Twhere.new(options[:config_file]).run
28
+ # load config
29
+ config = YAML::load(File.open(config_file))
30
+
31
+ # load known locations
32
+ locations = YAML::load(File.open(config[:twhere][:locations_file]))
33
+
34
+ # load template
35
+ template = File.read(config[:twhere][:template_file])
36
+
37
+ # load saved tweets (or don't)
38
+ tweets = []
39
+ begin
40
+ tweets = YAML::load(File.open(config[:twhere][:tweets_file])) if config[:twhere][:tweets_file]
41
+ rescue Errno::ENOENT
42
+ end
43
+
44
+ # create Twhere instance and puts result
45
+ twhere = Twhere.new(locations, template, config[:twitter][:username], config[:twitter][:password], config[:google_maps][:api_key], tweets)
46
+ puts twhere.result
47
+
48
+ # save tweets to file
49
+ File.open(config[:twhere][:tweets_file], 'w') { |f| f.puts tweets.to_yaml } if config[:twhere][:tweets_file]
data/lib/twhere.rb CHANGED
@@ -2,57 +2,47 @@ require 'rubygems'
2
2
  require 'erb'
3
3
  require 'twitter'
4
4
 
5
+ module TwhereHelperMethods
6
+ def hashtag(str)
7
+ md = str.match(/#([^#\s]+)/) and md[1]
8
+ end
9
+ end
10
+
5
11
  class Twhere
6
- def initialize(config_file)
7
- config = YAML::load(File.open(config_file))
8
- @locations_file = config[:twhere][:locations_file]
9
- @template_file = config[:twhere][:template_file]
10
- @tweets_file = config[:twhere][:tweets_file]
11
- @twitter_username = config[:twitter][:username]
12
- @twitter_password = config[:twitter][:password]
13
- @google_maps_api_key = config[:google_maps][:api_key]
12
+ include TwhereHelperMethods
13
+
14
+ def initialize(locations, template, twitter_username, twitter_password, google_maps_api_key, tweets = nil)
15
+ @locations = locations
16
+ @template = template
17
+ @twitter_username = twitter_username
18
+ @twitter_password = twitter_password
19
+ @google_maps_api_key = google_maps_api_key
20
+ @tweets = tweets || []
14
21
  end
15
22
 
16
23
  def result
17
- # load known locations
18
- @locations = YAML::load(File.open(@locations_file))
19
-
20
24
  # create Twitter proxy object
21
25
  twitter = Twitter::Base.new(Twitter::HTTPAuth.new(@twitter_username, @twitter_password))
22
26
 
23
- # load saved tweets (or don't)
24
- tweets = []
25
- begin
26
- tweets = YAML::load(File.open(@tweets_file)) if @tweets_file
27
- rescue Errno::ENOENT
28
- end
29
-
30
27
  # add new tweets to collection
31
28
  twitter.user_timeline.each do |t|
32
29
  # we only care about these variables
33
30
  h = {:twitter_id => t.id, :text => t.text, :created_at => t.created_at}
34
31
  # this appears to be necessary since neither Array#| nor Array#uniq works as expected
35
- tweets << h unless tweets.include? h
32
+ @tweets << h unless @tweets.include? h
36
33
  end
37
34
 
38
- # save tweets to file
39
- File.open(@tweets_file, 'w') { |f| f.puts tweets.to_yaml } if @tweets_file
40
-
41
35
  # group tweets by location
42
36
  @located_tweets = {}
43
- tweets.each do |t|
44
- md = t[:text].match(/#([a-z]+)/)
45
- if md and @locations.keys.include? md[1]
46
- @located_tweets[md[1]] ||= []
47
- @located_tweets[md[1]] << t
37
+ @tweets.each do |t|
38
+ ht = hashtag(t[:text])
39
+ if ht and @locations.keys.include? ht
40
+ @located_tweets[ht] ||= []
41
+ @located_tweets[ht] << t
48
42
  end
49
43
  end
50
44
 
51
45
  # open template file and process with ERB
52
- ERB.new(File.read(@template_file)).result(binding)
53
- end
54
-
55
- def run
56
- puts self.result
46
+ ERB.new(@template).result(binding)
57
47
  end
58
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sarnesjo-twhere
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jesper S\xC3\xA4rnesj\xC3\xB6"