mcornick-laika 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,57 @@
1
+ # Laika
2
+
3
+ ## Description
4
+
5
+ Laika (Russian: Лайка, "barker") is a simple script to monitor an RSS feed, and post to Yammer when new articles are detected.
6
+
7
+ Currently, only the Yammer posting functionality is implemented.
8
+
9
+ ## Installation
10
+
11
+ Laika is delivered as a RubyGem:
12
+
13
+ sudo gem install mcornick-laika --source http://gems.github.com
14
+
15
+ ## Usage
16
+
17
+ There are two steps you'll need to do to authenticate your application to Yammer. First, you'll need to register your application with Yammer. Log in to Yammer as the user under whose account the messages should appear. Then go to https://www.yammer.com/client_applications/new. You will be asked to describe your application. Fill out the form and submit it. This will provide you with a client key and secret. Make note of them as you will need them for the next step.
18
+
19
+ Then, you'll need to do the OAuth exchange, which will give you an access token and secret. Open up a terminal and run:
20
+
21
+ laika-auth
22
+
23
+ This will prompt you for the client key and secret you just obtained. It will then echo a URL which you will need to visit while still logged in to Yammer as the user from the previous step. You'll be asked to authorize your application. Click the Authorize button, then go back to your terminal and hit Return. The script will then get the access token and secret from Yammer, and store all four pieces of information in a file called laika.yml. **Don't lose this file!** It constitutes the credentials that you will need to do anything useful with Laika. It is not encrypted, so keep it secure. Once you have the laika.yml file, you will not need to do these steps again unless you de-authorize the application.
24
+
25
+ When the RSS polling functionality is ready, it will be documented here, but for now, you can use Laika to post messages to Yammer like so:
26
+
27
+ require 'rubygems'
28
+ require 'laika'
29
+ yammer = Laika::Yammer.new('laika.yml') # path to the laika.yml file; default is laika.yml in your current directory
30
+ yammer.post('I HAZ A SPASE DOG')
31
+
32
+ There is a script called laika-post that demonstrates how to do this.
33
+
34
+ ## License
35
+
36
+ Copyright (C) 2008 Mark Cornick of Viget Labs (http://www.viget.com/)
37
+
38
+ Permission is hereby granted, free of charge, to any person
39
+ obtaining a copy of this software and associated documentation
40
+ files (the "Software"), to deal in the Software without
41
+ restriction, including without limitation the rights to use,
42
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
43
+ copies of the Software, and to permit persons to whom the
44
+ Software is furnished to do so, subject to the following
45
+ conditions:
46
+
47
+ The above copyright notice and this permission notice shall be
48
+ included in all copies or substantial portions of the Software.
49
+
50
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
51
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
52
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
53
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
54
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
55
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
56
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
57
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/laika/version'
6
+
7
+ task :default => :test
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'laika'
11
+ s.version = Laika::Version.to_s
12
+ s.has_rdoc = true
13
+ s.extra_rdoc_files = %w(README.markdown)
14
+ s.summary = "Barks about new blog posts via the Yammer API and OAuth."
15
+ s.author = 'Mark Cornick'
16
+ s.email = 'mark.cornick@viget.com'
17
+ s.homepage = 'http://www.viget.com/'
18
+ s.files = %w(README.markdown Rakefile) + Dir.glob("{lib,test,bin}/**/*")
19
+ s.executables = %w(laika-auth laika-post)
20
+
21
+ s.add_dependency('oauth', '>=0.2.7')
22
+ s.add_dependency('feed-normalizer', '>=1.5.1')
23
+ end
24
+
25
+ Rake::GemPackageTask.new(spec) do |pkg|
26
+ pkg.gem_spec = spec
27
+ end
28
+
29
+ Rake::TestTask.new do |t|
30
+ t.libs << 'test'
31
+ t.test_files = FileList["test/**/*_test.rb"]
32
+ t.verbose = true
33
+ end
34
+
35
+ desc 'Generate the gemspec to serve this Gem from Github'
36
+ task :github do
37
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
38
+ File.open(file, 'w') {|f| f << spec.to_ruby }
39
+ puts "Created gemspec: #{file}"
40
+ end
data/bin/laika-auth ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'oauth/consumer'
5
+ require 'yaml'
6
+
7
+ puts "Please enter your client key, then hit Return."
8
+ consumer_key = gets.chomp
9
+ puts "Please enter your client secret, then hit Return."
10
+ consumer_secret = gets.chomp
11
+
12
+ begin
13
+ consumer = OAuth::Consumer.new consumer_key, consumer_secret, { :site => "https://www.yammer.com" }
14
+ request_token = consumer.get_request_token
15
+ puts "Please go to:\n#{request_token.authorize_url}\nin your browser. Click Authorize on that page.\nHit Return here when you are done."
16
+ gets
17
+ access_token = request_token.get_access_token
18
+ credentials = {
19
+ :consumer_key => consumer.key,
20
+ :consumer_secret => consumer.secret,
21
+ :access_token => access_token.token,
22
+ :access_token_secret => access_token.secret
23
+ }
24
+ begin
25
+ File.open('laika.yml', 'w') { |file| YAML.dump(credentials, file) }
26
+ File.chmod(0600, 'laika.yml')
27
+ puts "Your credentials have been saved."
28
+ rescue
29
+ puts "Couldn't save your credentials."
30
+ end
31
+ rescue
32
+ puts "Couldn't authorize with Yammer. Did you enter your client credentials correctly?"
33
+ end
data/bin/laika-post ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'laika'
5
+
6
+ puts "Please enter the text you would like to post to Yammer, then hit Return."
7
+ message = gets.chomp
8
+
9
+ yammer = Laika::Yammer.new('laika.yml')
10
+ yammer.post message
data/lib/laika.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), 'laika/blog')
2
+ require File.join(File.dirname(__FILE__), 'laika/yammer')
data/lib/laika/blog.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'feed-normalizer'
3
+ require 'open-uri'
4
+
5
+ module Laika
6
+ class Blog
7
+ attr_reader :feed
8
+
9
+ def initialize(url)
10
+ @url = url
11
+ @seen_ids = []
12
+ get_new_entries
13
+ end
14
+
15
+ def get_new_entries
16
+ @feed = FeedNormalizer::FeedNormalizer.parse(open(@url))
17
+ feed_ids = @feed.entries.collect{|e| e.id}
18
+ new_ids = feed_ids - @seen_ids
19
+ @seen_ids += new_ids
20
+ @feed.entries.select{|e| new_ids.include?(e.id)}
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Laika
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 2
6
+ TINY = 0
7
+
8
+ def self.to_s
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'oauth/consumer'
3
+ require 'yaml'
4
+
5
+ module Laika
6
+ class Yammer
7
+
8
+ class CouldNotLoadCredentials < Exception; end
9
+ class CouldNotPost < Exception; end
10
+
11
+ attr_reader :access_token
12
+
13
+ def initialize(credential_file='laika.yml')
14
+ begin
15
+ @credentials = YAML.load_file(credential_file)
16
+ rescue
17
+ raise CouldNotLoadCredentials, "Couldn't load your Yammer credentials. You may need to run laika-auth."
18
+ end
19
+ @consumer = OAuth::Consumer.new(@credentials[:consumer_key], @credentials[:consumer_secret], {:site => "https://www.yammer.com"})
20
+ @access_token = OAuth::AccessToken.new(@consumer, @credentials[:access_token], @credentials[:access_token_secret])
21
+ end
22
+
23
+ def post(message)
24
+ response = @access_token.post('/api/v1/messages', {:body => message}, {'Accept' => 'application/xml'})
25
+ if response.is_a?(Net::HTTPCreated)
26
+ true
27
+ else
28
+ raise CouldNotPost, response.body
29
+ end
30
+ end
31
+
32
+ end
33
+ end
data/test/blog_test.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'test_common'
2
+
3
+ class BlogTest < Test::Unit::TestCase
4
+
5
+ context 'A Blog object' do
6
+ setup do
7
+ @stubbed_entries = [stub(:id => 1), stub(:id => 2)]
8
+ @stubbed_feed = stub(:entries => @stubbed_entries)
9
+ FeedNormalizer::FeedNormalizer.stubs(:parse).returns(@stubbed_feed)
10
+ @blog = Laika::Blog.new('http://example.com/')
11
+ end
12
+
13
+ context 'being created' do
14
+ should 'get new entries' do
15
+ assert_equal @stubbed_entries, @blog.feed.entries
16
+ end
17
+ end
18
+
19
+ context 'getting new entries' do
20
+ context 'on first invocation' do
21
+ should 'return all entries' do
22
+ # initialize calls get_new_entries the first time
23
+ assert_equal @stubbed_entries, @blog.feed.entries
24
+ end
25
+ end
26
+ context 'on subsequent invocations' do
27
+ setup do
28
+ @new_entry = stub(:id => 3)
29
+ @stubbed_entries << @new_entry
30
+ @stubbed_feed.stubs(:entries).returns(@stubbed_entries)
31
+ end
32
+ should 'return only new entries' do
33
+ assert_equal [@new_entry], @blog.get_new_entries
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha'
4
+ require 'shoulda'
5
+ require File.join(File.dirname(__FILE__), '../lib/laika')
@@ -0,0 +1,62 @@
1
+ require 'test_common'
2
+
3
+ class YammerTest < Test::Unit::TestCase
4
+
5
+ context 'A Yammer object' do
6
+ context 'being created' do
7
+ context 'without laika.yml available' do
8
+ should 'raise CouldNotLoadCredentials' do
9
+ YAML.expects(:load_file).with('laika.yml').raises(Errno::ENOENT)
10
+ assert_raise(Laika::Yammer::CouldNotLoadCredentials) do
11
+ Laika::Yammer.new
12
+ end
13
+ end
14
+ end
15
+ context 'with laika.yml available' do
16
+ setup do
17
+ YAML.expects(:load_file).with('laika.yml').returns({})
18
+ end
19
+ should 'not raise any exceptions' do
20
+ assert_nothing_raised do
21
+ @yammer = Laika::Yammer.new
22
+ end
23
+ end
24
+ should 'produce an access token' do
25
+ OAuth::Consumer.expects(:new).returns(true)
26
+ OAuth::AccessToken.expects(:new).returns(true)
27
+ @yammer = Laika::Yammer.new
28
+ assert @yammer.access_token
29
+ end
30
+ end
31
+ end
32
+
33
+ context 'posting a message' do
34
+ setup do
35
+ YAML.expects(:load_file).with('laika.yml').returns({})
36
+ OAuth::Consumer.expects(:new).returns(true)
37
+ @access_token = stub()
38
+ OAuth::AccessToken.expects(:new).returns(@access_token)
39
+ end
40
+
41
+ context 'if successful' do
42
+ should 'return true' do
43
+ @access_token.expects(:post).returns(Net::HTTPCreated.new(nil,nil,nil))
44
+ @yammer = Laika::Yammer.new
45
+ assert_equal true, @yammer.post('pants')
46
+ end
47
+ end
48
+
49
+ context 'if unsuccessful' do
50
+ should 'raise CouldNotPost' do
51
+ # FIXME: lame mocking - should try to really grok what Net::HTTP is doing
52
+ NilClass.any_instance.expects(:closed?).returns(false)
53
+ @access_token.expects(:post).returns(Net::HTTPUnauthorized.new(nil,nil,nil))
54
+ @yammer = Laika::Yammer.new
55
+ assert_raise(Laika::Yammer::CouldNotPost) do
56
+ @yammer.post('pants')
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mcornick-laika
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Cornick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-04 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: oauth
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.2.7
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: feed-normalizer
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.5.1
32
+ version:
33
+ description:
34
+ email: mark.cornick@viget.com
35
+ executables:
36
+ - laika-auth
37
+ - laika-post
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.markdown
42
+ files:
43
+ - README.markdown
44
+ - Rakefile
45
+ - lib/laika
46
+ - lib/laika/blog.rb
47
+ - lib/laika/version.rb
48
+ - lib/laika/yammer.rb
49
+ - lib/laika.rb
50
+ - test/blog_test.rb
51
+ - test/test_common.rb
52
+ - test/yammer_test.rb
53
+ - bin/laika-auth
54
+ - bin/laika-post
55
+ has_rdoc: true
56
+ homepage: http://www.viget.com/
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.2.0
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: Barks about new blog posts via the Yammer API and OAuth.
81
+ test_files: []
82
+