dancroak-ruby-summize 0.4

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.
@@ -0,0 +1,70 @@
1
+ # A simple Summize client for Ruby.
2
+
3
+ Access the Summize API from your Ruby code.
4
+
5
+ ## Usage
6
+
7
+ Require the gem.
8
+
9
+ require 'summize'
10
+
11
+ Set up a Summize::Client. Name your client (a.k.a. 'user agent') to something meaningful, such as your app's name. This helps Summize answer any questions about your use of the API.
12
+
13
+ @client = Summize::Client.new 'politweets'
14
+
15
+ Request tweets by calling the query method of your client. It takes either a String or a Hash of arguments.
16
+
17
+ @tweets = @client.query 'twitter search'
18
+
19
+ The String form uses the default Summize behavior, which in this example finds tweets containing both "twitter" and "search". It is identical to the more verbose, explicit version:
20
+
21
+ @tweets = @client.query :q => 'twitter search'
22
+
23
+ Use Summize's query operators with the :q key to access the following behavior:
24
+
25
+ <table>
26
+ <tr><th>Operator</th><th>Finds tweets...</th></tr>
27
+
28
+ <tr><td><a href="http://summize.com/search?q=twitter+search">:q => 'twitter search'</a></td><td>containing both "twitter" and "search". This is the default operator.</td></tr>
29
+ <tr><td><a href="http://summize.com/search?q=%22happy+hour%22">:q => '<b>"</b>happy hour<b>"</b>'</a></td><td>containing the exact phrase "happy hour".</td></tr>
30
+ <tr><td><a href="http://summize.com/search?q=obama+OR+hillary">:q => 'obama <b>OR</b> hillary'</a></td><td>containing either "obama" or "hillary" (or both).</td></tr>
31
+
32
+ <tr><td><a href="http://summize.com/search?q=beer+-root">:q => 'beer <b>-</b>root'</a></td><td>containing "beer" but not "root".</td></tr>
33
+ <tr><td><a href="http://summize.com/search?q=%23haiku">:q => '<b>#</b>haiku</a>'</td><td>containing the hashtag "haiku".</td></tr>
34
+ <tr><td><a href="http://summize.com/search?q=from%3Aalexiskold">:q => '<b>from:</b>alexiskold'</a></td><td>sent from person "alexiskold".</td></tr>
35
+ <tr><td><a href="http://summize.com/search?q=to%3Atechcrunch">:q => '<b>to:</b>techcrunch</a>'</td><td>sent to person "techcrunch".</td></tr>
36
+ <tr><td><a href="http://summize.com/search?q=%40mashable">:q => '<b>@</b>mashable</a>'</td><td>referencing person "mashable".</td></tr>
37
+
38
+ <tr><td><a href="http://summize.com/search?q=superhero+since%3A2008-05-01">:q => 'superhero <b>since:</b>2008-05-01'</a></td><td>containing "superhero" and sent since date "2008-05-01" (year-month-day).</td></tr>
39
+
40
+ <tr><td><a href="http://summize.com/search?q=ftw+until%3A2008-05-03">:q => 'ftw <b>until:</b>2008-05-03'</a></td><td>containing "ftw" and sent up to date "2008-05-03".</td></tr>
41
+ <tr><td><a href="http://summize.com/search?q=movie+-scary+%3A%29">:q => 'movie -scary <b>:)</b>'</a></td><td>containing "movie", but not "scary", and with a positive attitude.</td></tr>
42
+ <tr><td><a href="http://summize.com/search?q=flight+%3A%28">:q => 'flight <b>:(</b>'</a></td><td>containing "flight" and with a negative attitude.</td></tr>
43
+ <tr><td><a href="http://summize.com/search?q=traffic+%3F">:q => 'traffic <b>?</b>'</a></td><td>containing "traffic" and asking a question.</td></tr>
44
+
45
+ <tr><td><a href="http://summize.com/search?q=hilarious+filter%3Alinks">:q => 'hilarious <b>filter:links</b>'</a></td><td>containing "hilarious" and linking to URLs.</td></tr>
46
+ </table>
47
+
48
+ ### Foreign Languages
49
+
50
+ The Summize API supports foreign languages, accessible via the :lang key. Use the [ISO 639-1](http://en.wikipedia.org/wiki/ISO_639-1) codes as the value:
51
+
52
+ @tweets = @client.query :q => 'programmé', :lang => 'fr'
53
+
54
+ ## Gotchas
55
+
56
+ * Searches are case-insenstive.
57
+ * The "near" operator available in the Summize web interface is not available via the API. You must geocode before making your Summize API call.
58
+ * Searching for a positive attitude :) returns tweets containing the text :), =), :D, and :-)
59
+
60
+ ## Authors
61
+
62
+ Written by Dustin Sallings (dustin@spy.net), forked by Dan Croak (dcroak@thoughtbot.com).
63
+
64
+ ## Resources
65
+
66
+ * [Official Summize API](http://summize.com/api)
67
+
68
+ ## License
69
+
70
+ MIT License, same terms as Ruby.
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'cgi'
5
+
6
+ module Summize
7
+
8
+ class Tweet
9
+ VARS = [:text, :from_user, :created_at, :id]
10
+ attr_reader *VARS
11
+ attr_reader :language
12
+
13
+ def initialize(h)
14
+ @language = h['iso_language_code']
15
+ VARS.each { |v| instance_variable_set "@#{v}", h[v.to_s] }
16
+ end
17
+ end
18
+
19
+ class Tweets
20
+ VARS = [:since_id, :max_id, :results_per_page, :page, :query, :next_page]
21
+ attr_reader *VARS
22
+
23
+ include Enumerable
24
+
25
+ def initialize(h)
26
+ @results = h['results'].map { |tweet| Tweet.new tweet }
27
+ VARS.each { |v| instance_variable_set "@#{v}", h[v.to_s] }
28
+ end
29
+
30
+ def each(&block)
31
+ @results.each(&block)
32
+ end
33
+
34
+ def size
35
+ @results.size
36
+ end
37
+ end
38
+
39
+ class Client
40
+ def initialize(agent = 'ruby-summize')
41
+ @agent = agent
42
+ end
43
+
44
+ def query(opts = {})
45
+ url = URI.parse 'http://search.twitter.com/search.json'
46
+ url.query = sanitize_query opts
47
+ Tweets.new JSON.parse(Net::HTTP.get(url))
48
+ end
49
+
50
+ private
51
+
52
+ def sanitize_query(opts)
53
+ if opts.is_a? String
54
+ "q=#{CGI.escape(opts)}"
55
+ elsif opts.is_a? Hash
56
+ "#{sanitize_query_hash(opts)}"
57
+ end
58
+ end
59
+
60
+ def sanitize_query_hash(query_hash)
61
+ query_hash.map{ |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "ruby-summize"
3
+ s.version = "0.4"
4
+ s.date = "2008-07-13"
5
+ s.summary = "Ruby client for Summize."
6
+ s.email = "dustin@spy.net"
7
+ s.homepage = "http://github.com/dancroak/ruby-summize"
8
+ s.description = "A Ruby client for the Summize conversational search engine."
9
+ s.has_rdoc = true
10
+ s.authors = ["Dustin Sallings", "Dan Croak"]
11
+ s.files = ["README.markdown", "ruby-summize.gemspec", "lib/summize.rb"]
12
+ s.rdoc_options = ["--main", "README.markdown"]
13
+ s.extra_rdoc_files = ["README.markdown"]
14
+ s.add_dependency('json', '>= 1.1.2')
15
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dancroak-ruby-summize
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.4"
5
+ platform: ruby
6
+ authors:
7
+ - Dustin Sallings
8
+ - Dan Croak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-07-13 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.2
24
+ version:
25
+ description: A Ruby client for the Summize conversational search engine.
26
+ email: dustin@spy.net
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.markdown
33
+ files:
34
+ - README.markdown
35
+ - ruby-summize.gemspec
36
+ - lib/summize.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/dancroak/ruby-summize
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --main
42
+ - README.markdown
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Ruby client for Summize.
64
+ test_files: []
65
+