jameswilding-magpie 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/magpie.rb +1 -0
- data/lib/magpie/search.rb +55 -0
- data/lib/magpie/tweet.rb +43 -0
- data/lib/magpie/tweet/author.rb +21 -0
- metadata +6 -3
data/lib/magpie.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'magpie/search')
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require File.join(File.dirname(__FILE__), 'tweet')
|
4
|
+
|
5
|
+
module Magpie
|
6
|
+
class Search
|
7
|
+
|
8
|
+
class SearchResponseError < StandardError; end
|
9
|
+
|
10
|
+
URI_BASE = 'http://search.twitter.com/search.atom?q='
|
11
|
+
|
12
|
+
attr_reader :uri, :results
|
13
|
+
|
14
|
+
def initialize(*terms)
|
15
|
+
@options = terms.last.is_a?(Hash) ? terms.pop : {}
|
16
|
+
xml = xml_search_result_for(terms)
|
17
|
+
@results = Tweet.map_from_xml(xml)
|
18
|
+
end
|
19
|
+
|
20
|
+
def inspect
|
21
|
+
"#<#{self.class}:#{object_id}>"
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def xml_search_result_for(terms)
|
26
|
+
response = search_response_for(terms)
|
27
|
+
case response
|
28
|
+
when Net::HTTPOK
|
29
|
+
response.body
|
30
|
+
else
|
31
|
+
raise SearchResponseError,
|
32
|
+
"Net::HTTPOK expected but got #{response.class} when connecting to #{search_uri_for(terms)}."
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def search_response_for(terms)
|
37
|
+
Net::HTTP.get_response(search_uri_for(terms))
|
38
|
+
end
|
39
|
+
|
40
|
+
def search_uri_for(terms)
|
41
|
+
uri = URI_BASE + sanitize_search_terms(terms) + search_uri_options
|
42
|
+
uri = URI.escape(uri)
|
43
|
+
@uri = URI.parse(uri)
|
44
|
+
end
|
45
|
+
|
46
|
+
def sanitize_search_terms(terms)
|
47
|
+
terms.map { |string| string.gsub(/ /, '+') }.join('+')
|
48
|
+
end
|
49
|
+
|
50
|
+
def search_uri_options
|
51
|
+
'&' + @options.map {|k,v| "#{k}=#{v}" }.join('&')
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
data/lib/magpie/tweet.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'cgi'
|
3
|
+
require File.join(File.dirname(__FILE__), 'tweet/author')
|
4
|
+
|
5
|
+
module Magpie
|
6
|
+
class Tweet
|
7
|
+
|
8
|
+
attr_reader :author, :published_at, :uri, :xml
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
def map_from_xml(xml)
|
13
|
+
parse(xml).map { |tweet| new(tweet) }
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def parse(xml)
|
18
|
+
Nokogiri::XML(xml).search('entry')
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(xml)
|
24
|
+
@xml = xml
|
25
|
+
@content = xml.search('content').inner_html
|
26
|
+
@published_at = DateTime.parse(xml.search('published').inner_html)
|
27
|
+
@uri = xml.search('link[@rel="alternate"]').attr('href')
|
28
|
+
@author = Author.new(xml)
|
29
|
+
end
|
30
|
+
|
31
|
+
def inspect
|
32
|
+
"#<#{self.class}:#{object_id}>"
|
33
|
+
end
|
34
|
+
|
35
|
+
def body(escaped=true)
|
36
|
+
escaped ? CGI.unescapeHTML(@content) : @content
|
37
|
+
end
|
38
|
+
|
39
|
+
def id
|
40
|
+
@link.split('/').last
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module Magpie
|
4
|
+
class Tweet
|
5
|
+
class Author
|
6
|
+
|
7
|
+
attr_reader :name, :uri, :image_uri
|
8
|
+
|
9
|
+
def initialize(xml)
|
10
|
+
@name = xml.search('author/name').inner_html
|
11
|
+
@uri = xml.search('author/uri').inner_html
|
12
|
+
@image_uri = xml.search('link[@rel="image"]').attr('href')
|
13
|
+
end
|
14
|
+
|
15
|
+
def inspect
|
16
|
+
"#<#{self.class}:#{object_id}>"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jameswilding-magpie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Wilding
|
@@ -30,8 +30,11 @@ extensions: []
|
|
30
30
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
|
33
|
-
files:
|
34
|
-
|
33
|
+
files:
|
34
|
+
- lib/magpie.rb
|
35
|
+
- lib/magpie/search.rb
|
36
|
+
- lib/magpie/tweet.rb
|
37
|
+
- lib/magpie/tweet/author.rb
|
35
38
|
has_rdoc: false
|
36
39
|
homepage: http://github.com/jameswilding/magpie/tree/master
|
37
40
|
post_install_message:
|