tweethook 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jordan Glasner
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.rdoc ADDED
@@ -0,0 +1,96 @@
1
+ = tweethook
2
+
3
+ A simple Ruby wrapper for accessing Tweethook's API.
4
+
5
+ = required gems
6
+
7
+ * tyhpoeus
8
+ * json
9
+
10
+ = required ENV variables
11
+
12
+ * TWEETHOOK_API_USER
13
+ * TWEETHOOK_API_PASS
14
+ * TWEETHOOK_SIGNATURE
15
+
16
+ See http://docs.heroku.com/config-vars for the how and why.
17
+
18
+ = Usage
19
+
20
+ There are three main classes
21
+
22
+ * Tweethook::Search
23
+ * Tweethook::Post
24
+ * Tweethook::Result
25
+
26
+
27
+ = Creating a new search
28
+
29
+ search = Tweethook::Search.create('my keyword', :webhook => 'http://mysite.com/callback')
30
+
31
+ All the properties are accessible as instance methods
32
+
33
+ search.id => 12560301
34
+ search.search => 'my keyword'
35
+ search.webhook => 'http://mysite.com/callback'
36
+ search.format => 'json' # this lib only works w/ the JSON format
37
+
38
+ = Receiving a new post
39
+
40
+ Tweethook::Post is a subclass of Array that takes the raw JSON from Tweethook. The JSON is parsed into a hash and the results are mapped to Tweethook::Result. The results can be accessed as if it was just an array, i.e post[0] = first result
41
+
42
+ # example JSON from post (accessible in request.body)
43
+ json = '{
44
+ "status_count": 2,
45
+ "signature": "XXXXXXXXXXXXXXXXXXXXXXXX",
46
+ "time": 1253055217,
47
+ "results": [{
48
+ "user": "deputy5765",
49
+ "tweet": "@Lu_Guz You will have to install a Twitter application in Facebook. Log in, search Twitter, install the app, then enter the info #geeksquad",
50
+ "usrimg": "http:\/\/a3.twimg.com\/profile_images\/107659093\/0_normal.jpg",
51
+ "tid": 4015703048,
52
+ "tstamp": "Tue, 15 Sep 2009 22:53:12 +0000",
53
+ "unix_tstamp": 1253055192,
54
+ "is_reply": 1,
55
+ "is_retweet": 0,
56
+ "mentions": "Lu_Guz",
57
+ "source": "Tweetie",
58
+ "sourcelink": "http:\/\/www.atebits.com\/",
59
+ "search_query": "twitter"
60
+ },
61
+ {
62
+ "user": "heatherkentart",
63
+ "tweet": "@k8otomato - Thanks, I will definitely try later. I will also postpone any negative opinions of Twitter. :D",
64
+ "usrimg": "http:\/\/a3.twimg.com\/profile_images\/417128285\/me_normal.jpg",
65
+ "tid": 4015703318,
66
+ "tstamp": "Tue, 15 Sep 2009 22:53:11 +0000",
67
+ "unix_tstamp": 1253055191,
68
+ "is_reply": 1,
69
+ "is_retweet": 0,
70
+ "mentions": "k8otomato",
71
+ "source": "web",
72
+ "sourcelink": "http:\/\/twitter.com\/",
73
+ "search_query": "twitter"
74
+ }
75
+ }'
76
+
77
+ post = Tweethook::Post.new(json)
78
+
79
+ post.size => 2
80
+
81
+ post.first.user => 'deputy5765'
82
+
83
+
84
+ == Note on Patches/Pull Requests
85
+
86
+ * Fork the project.
87
+ * Make your feature addition or bug fix.
88
+ * Add tests for it. This is important so I don't break it in a
89
+ future version unintentionally.
90
+ * Commit, do not mess with rakefile, version, or history.
91
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
92
+ * Send me a pull request. Bonus points for topic branches.
93
+
94
+ == Copyright
95
+
96
+ Copyright (c) 2009 Jordan Glasner. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "tweethook"
9
+ gem.summary = "wrapper for the Tweethook API"
10
+ gem.description = "wrapper for the Tweethook API"
11
+ gem.email = "jordan@digitalignition.com"
12
+ gem.homepage = "http://github.com/glasner/tweethook"
13
+ gem.authors = ["Jordan Glasner"]
14
+ gem.add_development_dependency "shoulda", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "tweethook #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,17 @@
1
+ class Tweethook::Post < Array
2
+
3
+ attr_accessor :signature, :time
4
+
5
+ def initialize(json)
6
+ hash = json.is_a?(Hash) ? json : JSON.parse(json)
7
+ @signature = hash['signature']
8
+ @time = Time.at(hash['time'])
9
+ super(hash['results'].map { |result| Tweethook::Result.new(result) })
10
+ end
11
+
12
+ def valid?
13
+ @signature.eql?(Tweethook.signature)
14
+ end
15
+
16
+
17
+ end
@@ -0,0 +1,15 @@
1
+ class Tweethook::Result
2
+
3
+ attr_accessor :user, :tweet, :usrimg, :tid, :tstamp, :unix_tstamp, :is_reply, :is_retweet, :mentions, :source, :sourcelink, :search_query
4
+
5
+ def initialize(args)
6
+ args.each { |key,value| instance_variable_set("@#{key}",value) }
7
+ change_boolean_values
8
+ end
9
+
10
+ def change_boolean_values
11
+ @is_reply = !@is_reply.zero?
12
+ @is_retweet = !@is_retweet.zero?
13
+ end
14
+
15
+ end
@@ -0,0 +1,85 @@
1
+ class Tweethook::Search
2
+
3
+ # create a new search at Tweethook
4
+ # Tweethook::Search.create('monkey', :webhook => 'http://where.com/callback' )
5
+ def self.create(search,args)
6
+ args.merge!({:search => search})
7
+ search = new(args)
8
+ search.save
9
+ end
10
+
11
+ def self.find(id)
12
+ response = Tweethook.get('/info.json', :id => id)
13
+ return nil if response.nil?
14
+ new(response.first)
15
+ end
16
+
17
+ def self.list
18
+ response = Tweethook.get('/list.json')
19
+ return [] if response.nil?
20
+ response.map { |hash| Tweethook::Search.new(hash) }
21
+ end
22
+
23
+ attr_accessor :id, :search, :webhook, :active
24
+
25
+ def initialize(args)
26
+ # change string keys to symbols
27
+ if args.any? { |arg| arg.is_a?(String) }
28
+ args = args.inject({}) { |output,array| key,value = array; output[key.to_sym] = value;output }
29
+ end
30
+ # setup instance variable for every arg
31
+ args.each { |key,value| instance_variable_set("@#{key}",value) }
32
+
33
+ @format ||= 'json'
34
+
35
+ change_active_to_boolean
36
+ end
37
+
38
+ def change_active_to_boolean
39
+ # @active defaults to true
40
+ @active = true if @active.nil?
41
+ # change @active to boolean value if it's an integer
42
+ @active = !@active.to_i.zero? if @active.is_a?(String)
43
+ end
44
+
45
+ def save
46
+ return modify unless new_record?
47
+ response = Tweethook.post('/create.json',:search => @search, :webhook => @webhook, :active => @active)
48
+ return nil if response.nil?
49
+ @id = response.first['id']
50
+ self
51
+ end
52
+
53
+ def modify
54
+ response = Tweethook.post('/modify.json', :id => @id, :search => @search, :webhook => @webhook, :active => @active)
55
+ return nil if response.nil?
56
+ self
57
+ end
58
+
59
+ def new_record?
60
+ not @id.nil?
61
+ end
62
+
63
+ def start
64
+ response = Tweethook.post('/start.json', :id => @id)
65
+ return false if response.nil?
66
+ @active = true
67
+ end
68
+ alias :resume :start
69
+
70
+ def stop
71
+ response = Tweethook.post('/stop.json', :id => @id)
72
+ return false if response.nil?
73
+ @active = false
74
+ end
75
+ alias :pause :stop
76
+
77
+
78
+ def destroy
79
+ response = Tweethook.post('/destroy.json',:id => @id)
80
+ return false if response.nil?
81
+ self.id = nil if response.first['id'].eql?(@id)
82
+ end
83
+
84
+
85
+ end
data/lib/tweethook.rb ADDED
@@ -0,0 +1,42 @@
1
+ $:.unshift File.expand_path(File.dirname(__FILE__))
2
+
3
+ require 'typhoeus'
4
+ require 'json'
5
+
6
+ class Tweethook
7
+
8
+ autoload :Search, 'tweethook/search'
9
+ autoload :Post, 'tweethook/post'
10
+ autoload :Result, 'tweethook/result'
11
+
12
+ def self.user
13
+ ENV['TWEETHOOK_API_USER']
14
+ end
15
+
16
+ def self.password
17
+ ENV['TWEETHOOK_API_PASS']
18
+ end
19
+
20
+ def self.signature
21
+ ENV['TWEETHOOK_SIGNATURE']
22
+ end
23
+
24
+ def self.post(path,params)
25
+ response = Typhoeus::Request.post(Tweethook.url(path),
26
+ :params => params)
27
+ response.body.empty? ? nil : JSON.parse(response.body)
28
+ end
29
+
30
+ def self.get(path,*params)
31
+ params = params.empty? ? nil : params.first
32
+ response = Typhoeus::Request.get(Tweethook.url(path), :params => params)
33
+ return nil if response.code.eql?(403)
34
+ result = JSON.parse(response.body)
35
+ end
36
+
37
+
38
+ def self.url(path)
39
+ "https://#{Tweethook.user}:#{Tweethook.password}@api.tweethook.com#{path}"
40
+ end
41
+
42
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ # require 'active_support'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'tweethook'
9
+
10
+ class Test::Unit::TestCase
11
+ end
data/test/test_post.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'helper'
2
+ require 'time'
3
+
4
+ class TestPost < Test::Unit::TestCase
5
+ context "instantiating TestPost with valid JSON" do
6
+ subject { @post }
7
+ setup do
8
+ @json = '{
9
+ "status_count": 2,
10
+ "signature": "XXXXXXXXXXXXXXXXXXXXXXXX",
11
+ "time": 1253055217,
12
+ "results": [{
13
+ "user": "deputy5765",
14
+ "tweet": "@Lu_Guz You will have to install a Twitter application in Facebook. Log in, search Twitter, install the app, then enter the info #geeksquad",
15
+ "usrimg": "http:\/\/a3.twimg.com\/profile_images\/107659093\/0_normal.jpg",
16
+ "tid": 4015703048,
17
+ "tstamp": "Tue, 15 Sep 2009 22:53:12 +0000",
18
+ "unix_tstamp": 1253055192,
19
+ "is_reply": 1,
20
+ "is_retweet": 0,
21
+ "mentions": "Lu_Guz",
22
+ "source": "Tweetie",
23
+ "sourcelink": "http:\/\/www.atebits.com\/",
24
+ "search_query": "twitter"
25
+ },
26
+ {
27
+ "user": "heatherkentart",
28
+ "tweet": "@k8otomato - Thanks, I will definitely try later. I will also postpone any negative opinions of Twitter. :D",
29
+ "usrimg": "http:\/\/a3.twimg.com\/profile_images\/417128285\/me_normal.jpg",
30
+ "tid": 4015703318,
31
+ "tstamp": "Tue, 15 Sep 2009 22:53:11 +0000",
32
+ "unix_tstamp": 1253055191,
33
+ "is_reply": 1,
34
+ "is_retweet": 0,
35
+ "mentions": "k8otomato",
36
+ "source": "web",
37
+ "sourcelink": "http:\/\/twitter.com\/",
38
+ "search_query": "twitter"
39
+ }
40
+ ]}'
41
+ @parsed_json = JSON.parse(@json)
42
+ @post = Tweethook::Post.new(@json)
43
+ end
44
+
45
+
46
+
47
+ should "take json payload" do
48
+ assert subject
49
+ end
50
+
51
+ should "have the same size as given json['results']" do
52
+ assert subject.size.eql?(@parsed_json['results'].size)
53
+ end
54
+
55
+ should "parse json['results'] into hash of Result objects" do
56
+ assert subject.all? { |element| element.is_a?(Tweethook::Result) }
57
+ end
58
+
59
+ should "save post signature into accessible instance variable" do
60
+ assert subject.signature.eql?(@parsed_json['signature'])
61
+ end
62
+
63
+ should "save post time into accessible instance variable" do
64
+ assert subject.time.eql?(Time.at(@parsed_json['time']))
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,97 @@
1
+ require 'helper'
2
+
3
+ class TestSearch < Test::Unit::TestCase
4
+ context "instantiating Search with parsed JSON from API" do
5
+ subject { @search }
6
+ setup do
7
+ @json = '{
8
+ "id": 12560300,
9
+ "search": "\"hello world\"",
10
+ "webhook": "http:\/\/example.com\/webhook.php",
11
+ "active": "1",
12
+ "format": "post"
13
+ }'
14
+ @parsed_json = JSON.parse(@json)
15
+ @search = Tweethook::Search.new(@parsed_json)
16
+ end
17
+
18
+ should 'save id in accessible instance variable' do
19
+ assert @search.id.eql?(@parsed_json['id'])
20
+ end
21
+
22
+ should 'save search in accessible instance variable' do
23
+ assert @search.search.eql?(@parsed_json['search'])
24
+ end
25
+
26
+ should 'save webhook in accessible instance variable' do
27
+ assert @search.webhook.eql?(@parsed_json['webhook'])
28
+ end
29
+
30
+ should 'save active in acessible instance variable as boolean value' do
31
+ assert @search.active.eql?(true)
32
+ end
33
+
34
+ end
35
+
36
+ context "instantiating Search with Hash with symbol keys" do
37
+
38
+ subject { @search }
39
+ setup do
40
+ @hash = {
41
+ :id => 1256033,
42
+ :search => 'hello world',
43
+ :webhook => 'http://somewhere.com',
44
+ :active => true,
45
+ :format => "post"
46
+ }
47
+ @search = Tweethook::Search.new(@hash)
48
+ end
49
+
50
+ should 'save :id in accessible instance variable' do
51
+ assert @search.id.eql?(@hash[:id])
52
+ end
53
+
54
+ should 'save :search in accessible instance variable' do
55
+ assert @search.search.eql?(@hash[:search])
56
+ end
57
+
58
+ should 'save :webhook in accessible instance variable' do
59
+ assert @search.webhook.eql?(@hash[:webhook])
60
+ end
61
+
62
+ end
63
+
64
+ context "saving a new Search record" do
65
+ subject {@search}
66
+ setup do
67
+ @args = {:search => 'monkey', :webhook => 'http://there.com'}
68
+ @search = Tweethook::Search.new(@args)
69
+ @search.save
70
+ end
71
+
72
+ should "have an id" do
73
+ assert_not_nil @search.id
74
+ @search.destroy
75
+ end
76
+ end
77
+
78
+
79
+ context "creating a new Search record" do
80
+
81
+ subject { @search }
82
+
83
+ setup do
84
+ @query = 'monkey'
85
+ @args = {:webhook => 'http://there.com'}
86
+ @search = Tweethook::Search.create(@query,@args)
87
+ end
88
+
89
+ should "have an id" do
90
+ assert_not_nil @search.id
91
+ @search.destroy
92
+ end
93
+
94
+
95
+ end
96
+
97
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tweethook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Glasner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-31 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: wrapper for the Tweethook API
26
+ email: jordan@digitalignition.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/tweethook.rb
42
+ - lib/tweethook/post.rb
43
+ - lib/tweethook/result.rb
44
+ - lib/tweethook/search.rb
45
+ - test/helper.rb
46
+ - test/test_post.rb
47
+ - test/test_search.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/glasner/tweethook
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: wrapper for the Tweethook API
76
+ test_files:
77
+ - test/helper.rb
78
+ - test/test_post.rb
79
+ - test/test_search.rb