squawk 1.0.0

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 (6) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +107 -0
  3. data/Rakefile +13 -0
  4. data/VERSION +1 -0
  5. data/lib/squawk.rb +62 -0
  6. metadata +82 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Avand Amiri
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ Squawk
2
+ ==================
3
+
4
+ Squawk gives you a very simple way to keep track of activity on your site.
5
+
6
+ With Squawk you can keep tabs on what's happening on your website with simple commands like this:
7
+
8
+ Squawk.user_signed_in! @user
9
+
10
+ You define the message and see it all happen in real-time on the Twitter account of your choice
11
+
12
+ Installation
13
+ ============
14
+ Because Squawk talks to Twitter, it has to run in the background. At least for now, Squawk assumes that you're using delayed_job.
15
+
16
+ Install Squawk like a typical Rails plugin:
17
+
18
+ script/plugin install git://github.com/avand/squawk.git
19
+
20
+ Then create your the initializer config/initializers/squawk.rb:
21
+
22
+ # Set up all the authorization stuff:
23
+ Squawk.twitter_consumer_key = "..." # Get this from Twitter
24
+ Squawk.twitter_consumer_secret = "..." # Get this from Twitter
25
+ Squawk.twitter_handle = "..." # i.e. mycoolproduct_app
26
+ Squawk.twitter_access_token = "..." # Get this from Twitter
27
+ Squawk.twitter_secret = "..." # Get this from Twitter
28
+
29
+ # Register events you'd like to call in your application with plain old strings:
30
+ Squawk.register_event :site_down, "Site just went down"
31
+
32
+ # Register events that create dynamic alerts:
33
+ Squawk.register_event :user_signed_up, lambda { |user| "#{user.name} just signed up" }
34
+
35
+ # Use Squawk to catch exceptions (useful in those hard to reach places exception apps miss):
36
+ Squawk.register_event :error_caught, lambda { |error| "Boom! #{error.message}" }
37
+
38
+ Usage
39
+ =====
40
+
41
+ Register a new event in config/initializers/squawk.rb:
42
+
43
+ Squawk.register_event :not_found, "Someone just hit a page that does not exist"
44
+
45
+ Want a more dynamic message? No problem, use a lambda to construct your own message at runtime:
46
+
47
+ Squawk.register_event :user_created, lambda { |user| "#{user.name} just created the #{User.count.ordinalize} account" }
48
+
49
+ Fire an event from anywhere in your app (controller, background job, mailer, go nuts):
50
+
51
+ Squawk.event_name!
52
+
53
+ At at time, see all the registered events:
54
+
55
+ Squawk.events
56
+
57
+ Notes
58
+ =====
59
+
60
+ Squawk only tweets in production. In non-production environments you can grep the log ("Squawk") to see what it would have tweeted.
61
+
62
+ Twitter enforces [rate limits](http://apiwiki.twitter.com/Rate-limiting), so play nice!
63
+
64
+ This plugin uses [Twitter's OAuth](http://dev.twitter.com/pages/oauth_faq), so you need:
65
+
66
+ * A Twitter browser-based application (something like "<your app name> Squawk Gem" with a callback of 'http://127.0.0.1')
67
+ * A protected Twitter account to post activity to (something like "<your app name>_squawk")
68
+ * Grant access to that account from the Twitter app you created
69
+
70
+ Installation
71
+ ============
72
+
73
+ This plugin uses [Twitter's OAuth](http://dev.twitter.com/pages/oauth_faq), so first you'll need to set up:
74
+
75
+ 1. A Twitter browser-based application (something like "<your app name> Squawk Gem" with a callback of 'http://127.0.0.1')
76
+ 2. A protected Twitter account to post activity to (something like "<your app name>_squawk")
77
+ 3. Grant access to that account from the Twitter app you created
78
+
79
+ Unfortunately, to get that last step working is kind of pain:
80
+
81
+ > require 'twitter'
82
+
83
+ > consumer_key = '...' # Your Twitter app's consumer key
84
+ > consumer_secret = '...' # Your Twitter app's secret key
85
+
86
+ > oauth = Twitter::OAuth.new consumer_key, consumer_secret
87
+ > oauth.set_callback_url('http://127.0.0.1:3000') # really anything your machine can GET
88
+
89
+ > request_token = oauth.request_token.token
90
+ > request_secret = oauth.request_token.secret
91
+
92
+ > authorize_url = oauth.request_token.authorize_url
93
+
94
+ # Browse to that authorize url.
95
+ # You'll be asked to authorize your app. Use the protected Twitter account's credentials.
96
+ # Twitter will redirect you back to whatever URL you specified.
97
+
98
+ > oauth_verifier = '...' # Grab the oauth_verifier from the query parameters of the callback.
99
+
100
+ > oauth.authorize_from_request(request_token, request_secret, oauth_verifier)
101
+
102
+ > token = oauth.access_token.token # Set this in the initializer as Squawk.twitter_access_token
103
+ > secret = oauth.access_token.secret # Set this in the initializer as Squawk.twitter_secret
104
+
105
+ Please [contact me](http://avandamiri.com/talk_to_me.html) if you find this useful and/or would like to contribute.
106
+
107
+ Copyright (c) 2010 Avand Amiri, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "jeweler"
2
+
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "squawk"
5
+ gem.summary = "Get a feel for what's happening in your app with protected tweets"
6
+ gem.email = "avand@avandamiri.com"
7
+ gem.homepage = "http://github.com/avand/squawk"
8
+ gem.authors = ["Avand Amiri"]
9
+ gem.files = FileList["[A-Z]*", "{lib}/**/*"]
10
+ gem.version = '1.0.0'
11
+
12
+ gem.add_dependency("twitter", "0.9.8")
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/lib/squawk.rb ADDED
@@ -0,0 +1,62 @@
1
+ class Squawk < Struct.new(:status)
2
+ cattr_accessor :twitter_consumer_key, :twitter_consumer_secret, :twitter_handle, :twitter_access_token, :twitter_secret
3
+ cattr_reader :events
4
+
5
+ class << self
6
+ # Registers an update event. For example:
7
+ # Squawk.register_event :site_down, "Site just went down"
8
+ # Squawk.register_event :user_signed_up, lambda { |user| "#{user.name} just signed up" }
9
+ # provide you with:
10
+ # Squawk.site_down!
11
+ # Squawk.user_signed_up! @user
12
+ def register_event(event_name, message_or_method)
13
+ @@events ||= []
14
+
15
+ event_name = event_name.to_sym
16
+
17
+ return if @@events.include?(event_name)
18
+
19
+ @@events << event_name.to_sym
20
+
21
+ method_name = "#{event_name}!"
22
+
23
+ (class << self; self; end).class_eval do # http://blog.jayfields.com/2008/02/ruby-dynamically-define-method.html
24
+ # First, the easy case - string
25
+ if message_or_method.is_a?(String)
26
+ define_method method_name do
27
+ update message_or_method
28
+ end
29
+ # Now, the harder case - lambda
30
+ else
31
+ define_method method_name do |*params|
32
+ update(message_or_method.call(*params))
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def test!
39
+ update "The time is currently #{Time.now}"
40
+ end
41
+
42
+ private
43
+
44
+ def update(status)
45
+ status = status[0...140] # trim status to min Twitter length
46
+
47
+ Rails.logger.info("Squawk (@#{@@twitter_handle}): #{status}")
48
+ Delayed::Job.enqueue new(status[0...140]) if Rails.env.production?
49
+
50
+ status
51
+ end
52
+ end
53
+
54
+ def perform
55
+ oauth = Twitter::OAuth.new(@@twitter_consumer_key, @@twitter_consumer_secret, :sign_in => true)
56
+ oauth.authorize_from_access(@@twitter_access_token, @@twitter_secret)
57
+
58
+ twitter = Twitter::Base.new(oauth)
59
+
60
+ twitter.update(status)
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: squawk
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Avand Amiri
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-19 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: twitter
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 9
31
+ - 8
32
+ version: 0.9.8
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description:
36
+ email: avand@avandamiri.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ files:
44
+ - MIT-LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - VERSION
48
+ - lib/squawk.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/avand/squawk
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Get a feel for what's happening in your app with protected tweets
81
+ test_files: []
82
+