yahoo_web_api 0.1.1
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/LICENCE.txt +21 -0
- data/lib/yahoo_web_api/search/image_search.rb +64 -0
- data/lib/yahoo_web_api/search.rb +8 -0
- data/lib/yahoo_web_api.rb +9 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/yahoo_web_api/search/image_search_sample.xml +32 -0
- data/spec/yahoo_web_api/search/image_search_spec.rb +32 -0
- metadata +62 -0
    
        data/LICENCE.txt
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            The MIT License
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Copyright (c) 2010 Tatsuya Sato
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining a copy
         | 
| 6 | 
            +
            of this software and associated documentation files (the "Software"), to deal
         | 
| 7 | 
            +
            in the Software without restriction, including without limitation the rights
         | 
| 8 | 
            +
            to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         | 
| 9 | 
            +
            copies of the Software, and to permit persons to whom the Software is
         | 
| 10 | 
            +
            furnished to do so, subject to the following conditions:
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            The above copyright notice and this permission notice shall be included in
         | 
| 13 | 
            +
            all copies or substantial portions of the Software.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         | 
| 16 | 
            +
            IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         | 
| 17 | 
            +
            FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         | 
| 18 | 
            +
            AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         | 
| 19 | 
            +
            LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         | 
| 20 | 
            +
            OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
         | 
| 21 | 
            +
            THE SOFTWARE.
         | 
| @@ -0,0 +1,64 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'net/http'
         | 
| 4 | 
            +
            require 'nokogiri'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            class ImageSearchService
         | 
| 7 | 
            +
              END_POINT = 'http://search.yahooapis.jp/ImageSearchService/V2/imageSearch'
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              class << self
         | 
| 10 | 
            +
                def parse(doc)
         | 
| 11 | 
            +
                  results = []
         | 
| 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
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              def image_search(params)
         | 
| 37 | 
            +
                params = @parameters.merge(params)
         | 
| 38 | 
            +
                res = post(params)
         | 
| 39 | 
            +
                results = self.class.parse(res.body.to_s)
         | 
| 40 | 
            +
                results
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              private 
         | 
| 44 | 
            +
              def end_point
         | 
| 45 | 
            +
                unless @end_point
         | 
| 46 | 
            +
                  @end_point = URI.parse(END_POINT)
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
                @end_point
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              def http
         | 
| 52 | 
            +
                unless @http
         | 
| 53 | 
            +
                  @http = Net::HTTP.new(end_point.host)
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
                @http
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
              def post(data)
         | 
| 59 | 
            +
                req = Net::HTTP::Post.new(end_point.path)
         | 
| 60 | 
            +
                query = data.map{|k,v| "#{k}=#{URI.encode(v)}"}.join('&')
         | 
| 61 | 
            +
                http.request(req, query)
         | 
| 62 | 
            +
              end
         | 
| 63 | 
            +
            end
         | 
| 64 | 
            +
             | 
    
        data/spec/spec_helper.rb
    ADDED
    
    
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            <?xml version="1.0" encoding="UTF-8"?>
         | 
| 2 | 
            +
            <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:jp:srchmi" xsi:schemaLocation="urn:yahoo:jp:srchmi http://search.yahooapis.jp/ImageSearchService/V2/ImageSearchResponse.xsd" totalResultsAvailable="22400030" totalResultsReturned="2" firstResultPosition="1">
         | 
| 3 | 
            +
              <Result>
         | 
| 4 | 
            +
                  <Url>http://photohito.com/uploads/photo3/user2168/17b2408c4590448d93f59b95336f8ecb/17b2408c4590448d93f59b95336f8ecb_l.jpg</Url>
         | 
| 5 | 
            +
                  <ClickUrl>http://photohito.com/uploads/photo3/user2168/17b2408c4590448d93f59b95336f8ecb/17b2408c4590448d93f59b95336f8ecb_l.jpg</ClickUrl>
         | 
| 6 | 
            +
                  <RefererUrl>http://photohito.com/photo/229218/</RefererUrl>
         | 
| 7 | 
            +
                  <Width>767</Width>
         | 
| 8 | 
            +
                  <Height>1024</Height>
         | 
| 9 | 
            +
                  <Title>17b2408c4590448d93f59b95336f8ecb_l.jpg</Title>
         | 
| 10 | 
            +
                  <FileFormat>jpeg</FileFormat>
         | 
| 11 | 
            +
                  <FileSize>92.9kB</FileSize>
         | 
| 12 | 
            +
                  <Thumbnail>
         | 
| 13 | 
            +
                      <Url>http://isearch.c.yimg.jp/image?id=1e86b3e062dd1dc8dbb2f228c49e10cd</Url>
         | 
| 14 | 
            +
                      <Width>127</Width>
         | 
| 15 | 
            +
                      <Height>170</Height>
         | 
| 16 | 
            +
                  </Thumbnail>
         | 
| 17 | 
            +
              </Result>
         | 
| 18 | 
            +
              <Result>
         | 
| 19 | 
            +
                  <Url>http://storage.kanshin.com/free/img_35/359737/k972056381.jpg</Url>
         | 
| 20 | 
            +
                  <ClickUrl>http://storage.kanshin.com/free/img_35/359737/k972056381.jpg</ClickUrl>
         | 
| 21 | 
            +
                  <RefererUrl>http://www.kanshin.com/keyword/1207321</RefererUrl>
         | 
| 22 | 
            +
                  <Width>500</Width><Height>335</Height>
         | 
| 23 | 
            +
                  <Title>k972056381.jpg</Title>
         | 
| 24 | 
            +
                  <FileFormat>jpeg</FileFormat>
         | 
| 25 | 
            +
                  <FileSize>120.3kB</FileSize>
         | 
| 26 | 
            +
                  <Thumbnail>
         | 
| 27 | 
            +
                      <Url>http://isearch.c.yimg.jp/image?id=886145b62cd92c601f57a7ed4c371876</Url>
         | 
| 28 | 
            +
                      <Width>170</Width>
         | 
| 29 | 
            +
                      <Height>114</Height>
         | 
| 30 | 
            +
                  </Thumbnail>
         | 
| 31 | 
            +
              </Result>
         | 
| 32 | 
            +
            </ResultSet>
         | 
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'uri'
         | 
| 4 | 
            +
            require File.expand_path(File.join(
         | 
| 5 | 
            +
              File.dirname(__FILE__), '..', '..', 'spec_helper'))
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            describe ImageSearchService do
         | 
| 8 | 
            +
              context "#image_search" do
         | 
| 9 | 
            +
                before do
         | 
| 10 | 
            +
                  @image_search_service = ImageSearchService.new(
         | 
| 11 | 
            +
                                            :appid => 'test_appid')
         | 
| 12 | 
            +
                  @image_search_service.stub!(:post).with(:appid => 'test_appid', :query => '沖縄').and_return do
         | 
| 13 | 
            +
                    path = File.expand_path(File.join(
         | 
| 14 | 
            +
                             File.dirname(__FILE__), 'image_search_sample.xml'))
         | 
| 15 | 
            +
                    body = nil
         | 
| 16 | 
            +
                    File.open(path, 'r') {|f| body = f.read }
         | 
| 17 | 
            +
                    response = mock(Object.new, :body => body)
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
                subject {@image_search_service.image_search(:query => '沖縄')}
         | 
| 21 | 
            +
                it {lambda{@image_search_service.image_search(:query => '沖縄')}.should_not raise_error }
         | 
| 22 | 
            +
                it { should_not be_nil }
         | 
| 23 | 
            +
                its(:size) {should == 2}
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                context "for Object returned itage_search" do
         | 
| 26 | 
            +
                  subject {@image_search_service.image_search(:query => '沖縄')[0]}
         | 
| 27 | 
            +
                  its(:url) { should eql 'http://photohito.com/uploads/photo3/user2168/17b2408c4590448d93f59b95336f8ecb/17b2408c4590448d93f59b95336f8ecb_l.jpg'}
         | 
| 28 | 
            +
                  its(:referer_url) { should eql 'http://photohito.com/photo/229218/' }
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
            end
         | 
| 32 | 
            +
             | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,62 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification 
         | 
| 2 | 
            +
            name: yahoo_web_api
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            +
              prerelease: 
         | 
| 5 | 
            +
              version: 0.1.1
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors: 
         | 
| 8 | 
            +
            - Tatsuya Sato
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            date: 2011-02-12 00:00:00 +09:00
         | 
| 14 | 
            +
            default_executable: 
         | 
| 15 | 
            +
            dependencies: []
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            description: 
         | 
| 18 | 
            +
            email: satoryu.1981@gmail.com
         | 
| 19 | 
            +
            executables: []
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            extensions: []
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            extra_rdoc_files: []
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            files: 
         | 
| 26 | 
            +
            - LICENCE.txt
         | 
| 27 | 
            +
            - lib/yahoo_web_api.rb
         | 
| 28 | 
            +
            - lib/yahoo_web_api/search.rb
         | 
| 29 | 
            +
            - lib/yahoo_web_api/search/image_search.rb
         | 
| 30 | 
            +
            - spec/yahoo_web_api/search/image_search_sample.xml
         | 
| 31 | 
            +
            - spec/yahoo_web_api/search/image_search_spec.rb
         | 
| 32 | 
            +
            - spec/spec_helper.rb
         | 
| 33 | 
            +
            has_rdoc: true
         | 
| 34 | 
            +
            homepage: http://github.com/satoryu/yahoo_web_api/
         | 
| 35 | 
            +
            licenses: []
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            post_install_message: 
         | 
| 38 | 
            +
            rdoc_options: []
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            require_paths: 
         | 
| 41 | 
            +
            - lib
         | 
| 42 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 43 | 
            +
              none: false
         | 
| 44 | 
            +
              requirements: 
         | 
| 45 | 
            +
              - - ">="
         | 
| 46 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 47 | 
            +
                  version: 1.9.2
         | 
| 48 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 49 | 
            +
              none: false
         | 
| 50 | 
            +
              requirements: 
         | 
| 51 | 
            +
              - - ">="
         | 
| 52 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 53 | 
            +
                  version: "0"
         | 
| 54 | 
            +
            requirements: []
         | 
| 55 | 
            +
             | 
| 56 | 
            +
            rubyforge_project: 
         | 
| 57 | 
            +
            rubygems_version: 1.5.2
         | 
| 58 | 
            +
            signing_key: 
         | 
| 59 | 
            +
            specification_version: 3
         | 
| 60 | 
            +
            summary: The gem to talk with Yahoo! Web API.
         | 
| 61 | 
            +
            test_files: []
         | 
| 62 | 
            +
             |