topsy 0.6.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/topsy.rb CHANGED
@@ -2,212 +2,36 @@ require 'hashie'
2
2
  require 'httparty'
3
3
 
4
4
  directory = File.expand_path(File.dirname(__FILE__))
5
+ Dir[directory + '/topsy/*.rb'].each{ |f| require f unless f =~ /version/ }
5
6
 
6
7
  Hash.send :include, Hashie::HashExtensions
7
8
 
8
9
 
9
10
  module Topsy
10
- class TopsyError < StandardError
11
- attr_reader :data
11
+ class << self
12
+ include Topsy::Configurable
12
13
 
13
- def initialize(data)
14
- @data = data
15
- super
14
+ def client
15
+ @client = Topsy::Client.new(options) unless defined?(@client)
16
+ @client
16
17
  end
17
- end
18
-
19
- class RateLimitExceeded < StandardError; end
20
- class Unauthorized < StandardError; end
21
- class General < TopsyError; end
22
18
 
23
- class Unavailable < StandardError; end
24
- class InformTopsy < StandardError; end
25
- class NotFound < StandardError; end
26
-
27
- # Returns profile information for an author (a twitter profile indexed by Topsy). The response contains the name, description (biography) and the influence level of the author
28
- #
29
- # @param [String] url URL string for the author.
30
- # @return [Topsy::Author]
31
- def self.author_info(url)
32
- Topsy::Client.new.author_info(url)
33
- end
34
-
35
-
36
- # Returns list of authors that talk about the query. The list is sorted by frequency of posts and the influence of authors.
37
- #
38
- # @param [String] q the search query string
39
- # @param [Hash] options method options
40
- # @option options [Symbol] :window Time window for results. (default: :auto) Options: :auto - automatically pick the most recent and relevant window. :hour last hour, :day last day, :week last week, :month last month, :all all time
41
- # @option options [Integer] :page page number of the result set. (default: 1, max: 10)
42
- # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
43
- # @return [Topsy::Page]
44
- def self.experts(q, options={})
45
- result = Topsy::Client.new.experts(q, options)
46
- Topsy::Page.new(result, Topsy::Author)
47
- end
48
-
49
- # Returns list of URLs posted by an author
50
- #
51
- # @param [String] url URL string for the author.
52
- # @param [Hash] options method options
53
- # @option options [String] :contains Query string to filter results
54
- # @option options [Integer] :page page number of the result set. (default: 1, max: 10)
55
- # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
56
- # @return [Topsy::Page]
57
- def self.link_posts(url, options={})
58
- Topsy::Client.new.link_posts(url, options)
59
- end
60
-
61
- # Returns count of links posted by an author. This is the efficient, count-only version of /linkposts
62
- #
63
- # @param [String] url URL string for the author.
64
- # @param [Hash] options method options
65
- # @option options [String] :contains Query string to filter results
66
- # @return [Topsy::LinkpostCount]
67
- def self.link_post_count(url, options={})
68
- response = Topsy::Client.new.link_post_count(url, options)
69
- Topsy::LinkpostCount.new(response)
70
- end
71
-
72
- # Returns list list of author profiles that match the query. The query is matched against the nick, name and biography information and the results are sorted by closeness of match and the influence of authors.
73
- #
74
- # @param [String] q the search query string
75
- # @param [Hash] options method options
76
- # @option options [Integer] :page page number of the result set. (default: 1, max: 10)
77
- # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
78
- # @return [Topsy::Page]
79
- def self.profile_search(q, options={})
80
- Topsy::Client.new.profile_search(q, options)
81
- end
82
-
83
- # Returns list of URLs related to a given URL
84
- #
85
- # @param [String] url URL string for the author.
86
- # @param [Hash] options method options
87
- # @option options [Integer] :page page number of the result set. (default: 1, max: 10)
88
- # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
89
- # @return [Topsy::Page]
90
- def self.related(url, options={})
91
- Topsy::Client.new.related(url, options)
92
- end
93
-
94
- # Returns list of results for a query.
95
- #
96
- # @param [String] q the search query string
97
- # @param [Hash] options method options
98
- # @option options [Symbol] :window Time window for results. (default: :auto) Options: :auto - automatically pick the most recent and relevant window. :hour last hour, :day last day, :week last week, :month last month, :realtime, :all all time
99
- # @option options [Integer] :page page number of the result set. (default: 1, max: 10)
100
- # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
101
- # @return [Topsy::Page]
102
- def self.search(q, options={})
103
- Topsy::Client.new.search(q, options)
104
- end
105
-
106
- # Returns count of results for a search query.
107
- #
108
- # @param [String] q the search query string
109
- # @return [Topsy::SearchCounts]
110
- def self.search_count(q)
111
- Topsy::Client.new.search_count(q)
112
- end
113
-
114
- # Returns data on the volume of tweets
115
- #
116
- # @param [String] the query
117
- # @params [String] what is being counted - "target" (default) - the number of unique links , or "citation" - cthe number of unique tweets about links
118
- # @params [Integer] The number of seconds for each slice ( default: 86400 = 1 day )
119
- # @params [Integer] The number of slices (default 30 = 1 month)
120
- # @return [Topsy::SearchHistogram]
121
- def self.search_histogram( query , count_method = "target" , slice = 86400 , period = 30 )
122
- Topsy::Client.new.search_histogram( query , count_method , slice , period )
123
- end
124
-
125
- # Returns counts of tweets for a URL
126
- #
127
- # @param [String] url the url to look up
128
- # @param [Hash] options method options
129
- # @option options [String] :contains Query string to filter results
130
- # @return [Topsy::Stats]
131
- def self.stats(url, options={})
132
- Topsy::Client.new.stats(url, options)
133
- end
134
-
135
- # Returns list of tags for a URL.
136
- #
137
- # @param [String] url the search query string
138
- # @param [Hash] options method options
139
- # @option options [Integer] :page page number of the result set. (default: 1, max: 10)
140
- # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
141
- # @return [Topsy::Page]
142
- def self.tags(url, options={})
143
- Topsy::Client.new.tags(url, options)
144
- end
145
-
146
- # Returns list of tweets (trackbacks) that mention the query URL, most recent first.
147
- #
148
- # @param [String] url URL string for the author.
149
- # @param [Hash] options method options
150
- # @option options [String] :contains Query string to filter results
151
- # @option options [Boolean] :infonly filters trackbacks to influential only (default 0)
152
- # @option options [Integer] :page page number of the result set. (default: 1, max: 10)
153
- # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
154
- # @return [Topsy::Page]
155
- def self.trackbacks(url, options={})
156
- Topsy::Client.new.trackbacks(url, options)
157
- end
158
-
159
-
160
- # Returns list of trending terms
161
- #
162
- # @param [Hash] options method options
163
- # @option options [Integer] :page page number of the result set. (default: 1, max: 10)
164
- # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
165
- # @return [Topsy::Page]
166
- def self.trending(options={})
167
- Topsy::Client.new.trending(options)
168
- end
169
-
170
- # Returns info about a URL
171
- #
172
- # @param [String] url the url to look up
173
- # @return [Topsy::UrlInfo]
174
- def self.url_info(url)
175
- Topsy::Client.new.url_info(url)
176
- end
177
-
178
- # Returns info about API rate limiting
179
- #
180
- # @return [Topsy::RateLimitInfo]
181
- def self.credit
182
- Topsy::Client.new.credit
183
- end
184
-
185
-
186
- def self.rate_limit
187
- self.credit if @rate_limit_info.nil?
188
- @rate_limit_info
189
- end
190
-
191
- def self.rate_limit=(info)
192
- @rate_limit_info = Topsy::RateLimitInfo.new(info)
19
+ def rate_limit
20
+ self.credit if @rate_limit_info.nil?
21
+ @rate_limit_info
22
+ end
23
+
24
+ def rate_limit=(info)
25
+ @rate_limit_info = Topsy::RateLimitInfo.new(info)
26
+ end
27
+
28
+ def respond_to_missing?(method_name , include_private=false); client.respond_to?(method_name, include_private); end if RUBY_VERSION >= "1.9"
29
+ def respond_to?(method_name, include_private=false); client.respond_to?(method_name, include_private) || super; end if RUBY_VERSION < "1.9"
30
+
31
+ private
32
+ def method_missing(method_name, *args, &block)
33
+ return super unless client.respond_to?(method_name)
34
+ client.send(method_name, *args, &block)
35
+ end
193
36
  end
194
-
195
-
196
37
  end
197
-
198
- require File.join(directory, 'topsy', 'rate_limit_info')
199
- require File.join(directory, 'topsy', 'author')
200
- require File.join(directory, 'topsy', 'linkpost')
201
- require File.join(directory, 'topsy', 'link_search_result')
202
- require File.join(directory, 'topsy', 'linkpost_count')
203
- require File.join(directory, 'topsy', 'page')
204
- require File.join(directory, 'topsy', 'search_counts')
205
- require File.join(directory, 'topsy', 'search_histogram')
206
- require File.join(directory, 'topsy', 'stats')
207
- require File.join(directory, 'topsy', 'target')
208
- require File.join(directory, 'topsy', 'tag')
209
- require File.join(directory, 'topsy', 'trend')
210
- require File.join(directory, 'topsy', 'tweet')
211
- require File.join(directory, 'topsy', 'url_info')
212
-
213
- require File.join(directory, 'topsy', 'client')
data/lib/topsy/client.rb CHANGED
@@ -1,15 +1,35 @@
1
+ require 'topsy/configurable'
1
2
  module Topsy
2
3
  class Client
3
4
  include HTTParty
4
5
  format :json
5
6
  base_uri "http://otter.topsy.com"
6
7
  @@windows = {:all => 'a', :week => 'w', :day => 'd', :month => 'm', :hour => 'h', :realtime => 'realtime'}
7
-
8
+
9
+ # topsy client modules
10
+ include Topsy::Configurable
11
+
12
+ def get( path , opts = {} )
13
+ if @api_key.length > 0
14
+ # Handle appending the api key
15
+ opts[:query] = {} unless opts.has_key?(:query)
16
+ opts[:query].merge!( { :apikey => options[:api_key] } )
17
+ end
18
+ self.class.get( path , opts )
19
+ end
20
+
21
+ def initialize( options = {} )
22
+ setup
23
+ Topsy::Configurable.keys.each do |key|
24
+ instance_variable_set(:"@#{key}", options[key] || instance_variable_get(:"@#{key}"))
25
+ end
26
+ end
27
+
8
28
  # Returns info about API rate limiting
9
29
  #
10
30
  # @return [Topsy::RateLimitInfo]
11
31
  def credit
12
- handle_response(self.class.get("/credit.json"))
32
+ handle_response(get("/credit.json"))
13
33
  end
14
34
 
15
35
  # Returns Profile information for an author (a twitter profile indexed by Topsy). The response contains the name, description (biography) and the influence level of the author
@@ -17,7 +37,7 @@ module Topsy
17
37
  # @param [String] url URL string for the author.
18
38
  # @return [Topsy::Author]
19
39
  def author_info(url)
20
- authorinfo = handle_response(self.class.get("/authorinfo.json", :query => {:url => url}))
40
+ authorinfo = handle_response(get("/authorinfo.json", :query => {:url => url}))
21
41
  Topsy::Author.new(authorinfo)
22
42
  end
23
43
 
@@ -31,7 +51,8 @@ module Topsy
31
51
  # @return [Hashie::Mash]
32
52
  def experts(q, options={})
33
53
  options = set_window_or_default(options)
34
- handle_response(self.class.get("/experts.json", :query => {:q => q}.merge(options)))
54
+ result = handle_response(get("/experts.json", :query => {:q => q}.merge(options)))
55
+ Topsy::Page.new(result, Topsy::Author)
35
56
  end
36
57
 
37
58
  # Returns list of URLs posted by an author
@@ -43,7 +64,7 @@ module Topsy
43
64
  # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
44
65
  # @return [Topsy::Page]
45
66
  def link_posts(url, options={})
46
- linkposts = handle_response(self.class.get("/linkposts.json", :query => {:url => url}.merge(options)))
67
+ linkposts = handle_response(get("/linkposts.json", :query => {:url => url}.merge(options)))
47
68
  Topsy::Page.new(linkposts,Topsy::Linkpost)
48
69
  end
49
70
 
@@ -54,7 +75,7 @@ module Topsy
54
75
  # @option options [String] :contains Query string to filter results
55
76
  # @return [Topsy::LinkpostCount]
56
77
  def link_post_count(url, options={})
57
- count = handle_response(self.class.get("/linkpostcount.json", :query => {:url => url}.merge(options)))
78
+ count = handle_response(get("/linkpostcount.json", :query => {:url => url}.merge(options)))
58
79
  Topsy::LinkpostCount.new(count)
59
80
  end
60
81
 
@@ -66,7 +87,7 @@ module Topsy
66
87
  # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
67
88
  # @return [Topsy::Page]
68
89
  def related(url, options={})
69
- response = handle_response(self.class.get("/related.json", :query => {:url => url}.merge(options)))
90
+ response = handle_response(get("/related.json", :query => {:url => url}.merge(options)))
70
91
  Topsy::Page.new(response,Topsy::LinkSearchResult)
71
92
  end
72
93
 
@@ -87,7 +108,7 @@ module Topsy
87
108
  q += " site:#{options.delete(:site)}" if options[:site]
88
109
  end
89
110
  options = set_window_or_default(options)
90
- results = handle_response(self.class.get("/search.json", :query => {:q => q}.merge(options)))
111
+ results = handle_response(get("/search.json", :query => {:q => q}.merge(options)))
91
112
  Topsy::Page.new(results,Topsy::LinkSearchResult)
92
113
  end
93
114
 
@@ -96,7 +117,7 @@ module Topsy
96
117
  # @param [String] q the search query string
97
118
  # @return [Topsy::SearchCounts]
98
119
  def search_count(q)
99
- counts = handle_response(self.class.get("/searchcount.json", :query => {:q => q}))
120
+ counts = handle_response(get("/searchcount.json", :query => {:q => q}))
100
121
  Topsy::SearchCounts.new(counts)
101
122
  end
102
123
 
@@ -107,8 +128,8 @@ module Topsy
107
128
  # @param [Integer] slice -
108
129
  # @param [Integer] period -
109
130
  #
110
- def search_histogram( q , count_method , slice , period )
111
- response = handle_response(self.class.get("/searchhistogram.json" , :query => { :q => q , :slice => slice , :period => period , :count_method => count_method } ))
131
+ def search_histogram( q , count_method = 'target' , slice = 86400 , period = 30 )
132
+ response = handle_response(get("/searchhistogram.json" , :query => { :q => q , :slice => slice , :period => period , :count_method => count_method } ))
112
133
  Topsy::SearchHistogram.new(response)
113
134
  end
114
135
 
@@ -121,7 +142,7 @@ module Topsy
121
142
  def stats(url, options={})
122
143
  query = {:url => url}
123
144
  query.merge!(options)
124
- response = handle_response(self.class.get("/stats.json", :query => query))
145
+ response = handle_response(get("/stats.json", :query => query))
125
146
  Topsy::Stats.new(response)
126
147
  end
127
148
 
@@ -133,7 +154,7 @@ module Topsy
133
154
  # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
134
155
  # @return [Topsy::Page]
135
156
  def tags(url, options={})
136
- response = handle_response(self.class.get("/tags.json", :query => {:url => url}.merge(options)))
157
+ response = handle_response(get("/tags.json", :query => {:url => url}.merge(options)))
137
158
  Topsy::Page.new(response,Topsy::Tag)
138
159
  end
139
160
 
@@ -147,7 +168,7 @@ module Topsy
147
168
  # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
148
169
  # @return [Topsy::Page]
149
170
  def trackbacks(url, options={})
150
- results = handle_response(self.class.get("/trackbacks.json", :query => {:url => url}.merge(options)))
171
+ results = handle_response(get("/trackbacks.json", :query => {:url => url}.merge(options)))
151
172
  results.list.each do |trackback|
152
173
  trackback.date = Time.at(trackback.date)
153
174
  end
@@ -161,7 +182,7 @@ module Topsy
161
182
  # @option options [Integer] :perpage limit number of results per page. (default: 10, max: 50)
162
183
  # @return [Topsy::Page]
163
184
  def trending(options={})
164
- response = handle_response(self.class.get("/trending.json", :query => options))
185
+ response = handle_response(get("/trending.json", :query => options))
165
186
  Topsy::Page.new(response,Topsy::Trend)
166
187
  end
167
188
 
@@ -170,7 +191,7 @@ module Topsy
170
191
  # @param [String] url the url to look up
171
192
  # @return [Topsy::UrlInfo]
172
193
  def url_info(url)
173
- response = handle_response(self.class.get("/urlinfo.json", :query => {:url => url}))
194
+ response = handle_response(get("/urlinfo.json", :query => {:url => url}))
174
195
  Topsy::UrlInfo.new(response)
175
196
  end
176
197
 
@@ -0,0 +1,43 @@
1
+ require 'topsy/default'
2
+ module Topsy
3
+ module Configurable
4
+ attr_writer :api_key
5
+
6
+ class << self
7
+
8
+ def keys
9
+ @keys ||= [
10
+ :api_key
11
+ ]
12
+ end
13
+
14
+ end
15
+
16
+ # Convenience method to allow configuration options to be set in a block
17
+ def configure
18
+ yield self
19
+ self
20
+ end
21
+
22
+ # @return [Fixnum]
23
+ def cache_key
24
+ options.hash
25
+ end
26
+
27
+ def reset!
28
+ Topsy::Configurable.keys.each do |key|
29
+ instance_variable_set(:"@#{key}", Topsy::Default.options[key])
30
+ end
31
+ self
32
+ end
33
+ alias setup reset!
34
+
35
+ private
36
+
37
+ # @return [Hash]
38
+ def options
39
+ Hash[Topsy::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ module Topsy
2
+ module Default
3
+ class << self
4
+ # @return [Hash]
5
+ def options
6
+ Hash[Topsy::Configurable.keys.map{|key| [key, send(key)]}]
7
+ end
8
+
9
+ # @return [String]
10
+ def api_key
11
+ ""
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module Topsy
2
+ class TopsyError < StandardError
3
+ attr_reader :data
4
+
5
+ def initialize(data)
6
+ @data = data
7
+ super
8
+ end
9
+ end
10
+
11
+ class RateLimitExceeded < StandardError; end
12
+ class Unauthorized < StandardError; end
13
+ class General < TopsyError; end
14
+
15
+ class Unavailable < StandardError; end
16
+ class InformTopsy < StandardError; end
17
+ class NotFound < StandardError; end
18
+ end
data/lib/topsy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Topsy
2
- VERSION = '0.6.0'
2
+ VERSION = '1.0.0'
3
3
  end
data/test/test_topsy.rb CHANGED
@@ -2,6 +2,16 @@ require 'helper'
2
2
 
3
3
  class TestTopsy < Test::Unit::TestCase
4
4
 
5
+ context "when initializing Topsy client" do
6
+ context "with API key" do
7
+ should "append api_key to all requests" do
8
+ client = Topsy::Client.new( { :api_key => "abcdefg123456" } )
9
+ stub_get("/search.json?q=NYE&window=m&apikey=abcdefg123456", "search.json")
10
+ client.search("NYE", :window => :month)
11
+ end
12
+ end
13
+ end
14
+
5
15
  context "when hitting the Otter API" do
6
16
 
7
17
  should "return rate limit information when calling credit" do
@@ -12,8 +22,6 @@ class TestTopsy < Test::Unit::TestCase
12
22
  end
13
23
 
14
24
  should "return rate limit information with every request" do
15
-
16
- # FakeWeb.register_uri(:get, "http://otter.topsy.com/authorinfo.json?url=http%3A%2F%2Ftwitter.com%2Fpengwynn", headers)
17
25
  stub_get("/authorinfo.json?url=http%3A%2F%2Ftwitter.com%2Fpengwynn", "authorinfo.json")
18
26
 
19
27
  info = Topsy.author_info("http://twitter.com/pengwynn")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: topsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -116,6 +116,9 @@ extra_rdoc_files: []
116
116
  files:
117
117
  - lib/topsy/author.rb
118
118
  - lib/topsy/client.rb
119
+ - lib/topsy/configurable.rb
120
+ - lib/topsy/default.rb
121
+ - lib/topsy/errors.rb
119
122
  - lib/topsy/link_search_result.rb
120
123
  - lib/topsy/linkpost.rb
121
124
  - lib/topsy/linkpost_count.rb