yahoo_web_api 0.1.3 → 0.2.0
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.
- data/lib/yahoo_web_api/client.rb +56 -0
- data/lib/yahoo_web_api/search/blog_search.rb +28 -0
- data/lib/yahoo_web_api/search/image_search.rb +16 -56
- data/lib/yahoo_web_api/search.rb +1 -0
- data/spec/spec_helper.rb +2 -2
- data/spec/yahoo_web_api/search/blog_search_sample.xml +21 -0
- data/spec/yahoo_web_api/search/blog_search_spec.rb +70 -0
- data/spec/yahoo_web_api/search/image_search_spec.rb +7 -5
- metadata +6 -2
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module YahooWebAPI
|
4
|
+
class Client
|
5
|
+
class << self
|
6
|
+
def parse(doc)
|
7
|
+
results = []
|
8
|
+
xml_doc = Nokogiri::XML(doc)
|
9
|
+
xml_doc.css('ResultSet Result').each do |doc|
|
10
|
+
results << make_response_object(doc)
|
11
|
+
end
|
12
|
+
results
|
13
|
+
end
|
14
|
+
|
15
|
+
def make_response_object(doc)
|
16
|
+
obj = Object.new
|
17
|
+
klass = (class << obj; self; end)
|
18
|
+
doc.elements.each do |elem|
|
19
|
+
method_name = elem.name.gsub(/([a-z])([A-Z])/) {
|
20
|
+
$1 + '_' + $2
|
21
|
+
}.downcase
|
22
|
+
if elem.elements.empty?
|
23
|
+
respond = elem.inner_text.gsub!(/^\s*/m) {''}
|
24
|
+
respond.gsub!(/\s*$/m) {''}
|
25
|
+
else
|
26
|
+
respond = make_response_object(elem)
|
27
|
+
end
|
28
|
+
klass.__send__(:define_method, method_name) { respond }
|
29
|
+
end
|
30
|
+
obj
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
def http(host)
|
36
|
+
unless @http
|
37
|
+
@http = Net::HTTP.new(host)
|
38
|
+
end
|
39
|
+
@http
|
40
|
+
end
|
41
|
+
|
42
|
+
def post(end_point, data)
|
43
|
+
end_point = URI.parse(end_point)
|
44
|
+
req = Net::HTTP::Post.new(end_point.path)
|
45
|
+
query = data.map{|k,v| "#{k}=#{URI.encode(v)}"}.join('&')
|
46
|
+
http(end_point.host).request(req, query)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get(end_point, data)
|
50
|
+
end_point = URI.parse(end_point)
|
51
|
+
query = data.map{|k,v| "#{k}=#{URI.encode(v)}"}.join('&')
|
52
|
+
req = Net::HTTP::Get.new(end_point.path + '?' + query)
|
53
|
+
http(end_point.host).request(req)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'nokogiri'
|
5
|
+
|
6
|
+
require 'yahoo_web_api/client'
|
7
|
+
|
8
|
+
module YahooWebAPI
|
9
|
+
class BlogSearchService < Client
|
10
|
+
END_POINT = 'http://search.yahooapis.jp/BlogSearchService/V1/blogSearch'
|
11
|
+
|
12
|
+
def initialize(args)
|
13
|
+
@parameters = {}
|
14
|
+
@parameters.merge!(args)
|
15
|
+
end
|
16
|
+
|
17
|
+
def blog_search(params)
|
18
|
+
params = @parameters.merge(params)
|
19
|
+
res = get(END_POINT, params)
|
20
|
+
if res.code == '200'
|
21
|
+
results = self.class.parse(res.body.to_s)
|
22
|
+
return results
|
23
|
+
else
|
24
|
+
raise Exception, Nokogiri::XML(res.body).css('Error Message').inner_text
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -3,66 +3,26 @@
|
|
3
3
|
require 'net/http'
|
4
4
|
require 'nokogiri'
|
5
5
|
|
6
|
-
|
7
|
-
END_POINT = 'http://search.yahooapis.jp/ImageSearchService/V2/imageSearch'
|
6
|
+
require 'yahoo_web_api/client'
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
xml_doc = Nokogiri::XML(doc)
|
13
|
-
xml_doc.css('ResultSet Result').each do |doc|
|
14
|
-
results << make_response_object(doc)
|
15
|
-
end
|
16
|
-
results
|
17
|
-
end
|
18
|
-
|
19
|
-
def make_response_object(doc)
|
20
|
-
obj = Object.new
|
21
|
-
doc.elements.each do |elem|
|
22
|
-
klass = (class << obj; self; end)
|
23
|
-
method_name = elem.name.gsub(/([a-z])([A-Z])/) { $1 + '_' + $2 }.downcase
|
24
|
-
respond = (elem.elements.empty? ? elem.inner_text : make_response_object(elem))
|
25
|
-
klass.__send__(:define_method, method_name) { respond }
|
26
|
-
end
|
27
|
-
obj
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def initialize(args)
|
32
|
-
@parameters = {}
|
33
|
-
@parameters.merge!(args)
|
34
|
-
end
|
8
|
+
module YahooWebAPI
|
9
|
+
class ImageSearchService < Client
|
10
|
+
END_POINT = 'http://search.yahooapis.jp/ImageSearchService/V2/imageSearch'
|
35
11
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
if res.code == '200'
|
40
|
-
results = self.class.parse(res.body.to_s)
|
41
|
-
return results
|
42
|
-
else
|
43
|
-
raise Exception, Nokogiri::XML(res.body).css('Error Message').inner_text
|
12
|
+
def initialize(args)
|
13
|
+
@parameters = {}
|
14
|
+
@parameters.merge!(args)
|
44
15
|
end
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
def end_point
|
49
|
-
unless @end_point
|
50
|
-
@end_point = URI.parse(END_POINT)
|
51
|
-
end
|
52
|
-
@end_point
|
53
|
-
end
|
54
16
|
|
55
|
-
|
56
|
-
|
57
|
-
|
17
|
+
def image_search(params)
|
18
|
+
params = @parameters.merge(params)
|
19
|
+
res = post(END_POINT, params)
|
20
|
+
if res.code == '200'
|
21
|
+
results = self.class.parse(res.body.to_s)
|
22
|
+
return results
|
23
|
+
else
|
24
|
+
raise Exception, Nokogiri::XML(res.body).css('Error Message').inner_text
|
25
|
+
end
|
58
26
|
end
|
59
|
-
@http
|
60
|
-
end
|
61
|
-
|
62
|
-
def post(data)
|
63
|
-
req = Net::HTTP::Post.new(end_point.path)
|
64
|
-
query = data.map{|k,v| "#{k}=#{URI.encode(v)}"}.join('&')
|
65
|
-
http.request(req, query)
|
66
27
|
end
|
67
28
|
end
|
68
|
-
|
data/lib/yahoo_web_api/search.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" ?>
|
2
|
+
<ResultSet xsi:schemaLocation="urn:yahoo:jp:blogsearch:article http://search.yahooapis.jp/BlogSearchService/V1/articleSearchResponse.xsd" totalResultsAvailable="759629" totalResultsReturned="10" firstResultPosition="1">
|
3
|
+
<Result>
|
4
|
+
<Id>6a0adcc7caa029cde502c02bcecc3101</Id>
|
5
|
+
<RssUrl>http://searchblog.yahoo.co.jp/index.xml</RssUrl>
|
6
|
+
<Title>あなたの知らないタイピングミスの世界</Title>
|
7
|
+
<Description>
|
8
|
+
タタタッと華麗なタイピング音を響かせて、最後はスマートに[Enter]キーを軽やかに打鍵。 検索完了っと! ! こんにちは 検索ランキング のイケミーです。 毎回少し変わった視点の検索分析コラムをお届けするこのはみだしコーナー ...
|
9
|
+
</Description>
|
10
|
+
<Url>
|
11
|
+
http://searchblog.yahoo.co.jp/2009/02/yahoo_84.html
|
12
|
+
</Url>
|
13
|
+
<Creator>検索スタッフブログ</Creator>
|
14
|
+
<mobileLink></mobileLink>
|
15
|
+
<Site>
|
16
|
+
<Title>Yahoo!検索 スタッフブログ</Title>
|
17
|
+
<Url>http://searchblog.yahoo.co.jp/</Url>
|
18
|
+
</Site>
|
19
|
+
<DateTime>2009-05-22T10:00:05+09:00</DateTime>
|
20
|
+
</Result>
|
21
|
+
</ResultSet>
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require File.expand_path(File.join(
|
5
|
+
File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
6
|
+
|
7
|
+
require 'yahoo_web_api/search/blog_search'
|
8
|
+
|
9
|
+
describe YahooWebAPI::BlogSearchService do
|
10
|
+
context "#image_search" do
|
11
|
+
before do
|
12
|
+
@blog_search_service = YahooWebAPI::BlogSearchService.new(
|
13
|
+
:appid => 'test_appid')
|
14
|
+
@blog_search_service.stub!(:get).with(YahooWebAPI::BlogSearchService::END_POINT, :appid => 'test_appid', :query => 'Yahoo!検索スタッフブログ').and_return do
|
15
|
+
path = File.expand_path(File.join(
|
16
|
+
File.dirname(__FILE__), 'blog_search_sample.xml'))
|
17
|
+
body = nil
|
18
|
+
File.open(path, 'r') {|f| body = f.read }
|
19
|
+
response = mock(Object.new, :code => '200', :body => body)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
subject {@blog_search_service.blog_search(:query => 'Yahoo!検索スタッフブログ')}
|
23
|
+
it {lambda{@blog_search_service.blog_search(:query => 'Yahoo!検索スタッフブログ')}.should_not raise_error }
|
24
|
+
it { should_not be_nil }
|
25
|
+
its(:size) {should == 1}
|
26
|
+
|
27
|
+
context "for Object returned blog_search" do
|
28
|
+
subject {@blog_search_service.blog_search(:query => 'Yahoo!検索スタッフブログ')[0]}
|
29
|
+
its(:url) { should eql 'http://searchblog.yahoo.co.jp/2009/02/yahoo_84.html'}
|
30
|
+
|
31
|
+
context "for Object containing site information" do
|
32
|
+
before do
|
33
|
+
@result = @blog_search_service.blog_search(:query => 'Yahoo!検索スタッフブログ')[0]
|
34
|
+
end
|
35
|
+
subject {@result.site}
|
36
|
+
its(:url) { should eql 'http://searchblog.yahoo.co.jp/'}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "When #image_search received an error response" do
|
42
|
+
before do
|
43
|
+
@blog_search_service = YahooWebAPI::BlogSearchService.new(
|
44
|
+
:appid => 'test_appid')
|
45
|
+
error_response = mock(Object.new,
|
46
|
+
:code => '400',
|
47
|
+
:body => <<-BODY)
|
48
|
+
<?xml version="1.0" encoding="utf-8"?>
|
49
|
+
<Error>
|
50
|
+
<Message>error message</Message>
|
51
|
+
</Error>
|
52
|
+
BODY
|
53
|
+
@blog_search_service.stub!(:get).with(any_args()).and_return do
|
54
|
+
mock(Object.new, :code => '400',
|
55
|
+
:body => <<-BODY)
|
56
|
+
<?xml version="1.0" encoding="utf-8"?>
|
57
|
+
<Error>
|
58
|
+
<Message>error message</Message>
|
59
|
+
</Error>
|
60
|
+
BODY
|
61
|
+
end
|
62
|
+
end
|
63
|
+
it do
|
64
|
+
lambda do
|
65
|
+
@blog_search_service.blog_search(:query => 'hoge')
|
66
|
+
end.should raise_error(Exception, 'error message')
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -4,12 +4,14 @@ require 'uri'
|
|
4
4
|
require File.expand_path(File.join(
|
5
5
|
File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
6
6
|
|
7
|
-
|
7
|
+
require 'yahoo_web_api/search/image_search'
|
8
|
+
|
9
|
+
describe YahooWebAPI::ImageSearchService do
|
8
10
|
context "#image_search" do
|
9
11
|
before do
|
10
|
-
@image_search_service = ImageSearchService.new(
|
12
|
+
@image_search_service = YahooWebAPI::ImageSearchService.new(
|
11
13
|
:appid => 'test_appid')
|
12
|
-
@image_search_service.stub!(:post).with(:appid => 'test_appid', :query => '沖縄').and_return do
|
14
|
+
@image_search_service.stub!(:post).with(YahooWebAPI::ImageSearchService::END_POINT, :appid => 'test_appid', :query => '沖縄').and_return do
|
13
15
|
path = File.expand_path(File.join(
|
14
16
|
File.dirname(__FILE__), 'image_search_sample.xml'))
|
15
17
|
body = nil
|
@@ -41,7 +43,7 @@ describe ImageSearchService do
|
|
41
43
|
|
42
44
|
context "When #image_search received an error response" do
|
43
45
|
before do
|
44
|
-
@image_search_service = ImageSearchService.new(
|
46
|
+
@image_search_service = YahooWebAPI::ImageSearchService.new(
|
45
47
|
:appid => 'test_appid')
|
46
48
|
error_response = mock(Object.new,
|
47
49
|
:code => '400',
|
@@ -51,7 +53,7 @@ describe ImageSearchService do
|
|
51
53
|
<Message>error message</Message>
|
52
54
|
</Error>
|
53
55
|
BODY
|
54
|
-
@image_search_service.stub!(:post).and_return do
|
56
|
+
@image_search_service.stub!(:post).with(any_args()).and_return do
|
55
57
|
mock(Object.new, :code => '400',
|
56
58
|
:body => <<-BODY)
|
57
59
|
<?xml version="1.0" encoding="utf-8"?>
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: yahoo_web_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tatsuya Sato
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-02-
|
13
|
+
date: 2011-02-23 00:00:00 +09:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -25,10 +25,14 @@ extra_rdoc_files: []
|
|
25
25
|
files:
|
26
26
|
- LICENCE.txt
|
27
27
|
- lib/yahoo_web_api.rb
|
28
|
+
- lib/yahoo_web_api/client.rb
|
28
29
|
- lib/yahoo_web_api/search.rb
|
30
|
+
- lib/yahoo_web_api/search/blog_search.rb
|
29
31
|
- lib/yahoo_web_api/search/image_search.rb
|
30
32
|
- spec/yahoo_web_api/search/image_search_sample.xml
|
31
33
|
- spec/yahoo_web_api/search/image_search_spec.rb
|
34
|
+
- spec/yahoo_web_api/search/blog_search_sample.xml
|
35
|
+
- spec/yahoo_web_api/search/blog_search_spec.rb
|
32
36
|
- spec/spec_helper.rb
|
33
37
|
has_rdoc: true
|
34
38
|
homepage: http://github.com/satoryu/yahoo_web_api/
|