File: GoogleSearchDriver.rb

package info (click to toggle)
soap4r 1.4.8-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,472 kB
  • ctags: 2,206
  • sloc: ruby: 19,295; makefile: 58; sh: 41; perl: 10
file content (179 lines) | stat: -rw-r--r-- 5,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require 'GoogleSearch.rb'

require 'soap/proxy'
require 'soap/rpcUtils'
require 'soap/streamHandler'

class GoogleSearchPort
  class EmptyResponseError < ::SOAP::Error; end

  MappingRegistry = ::SOAP::RPCUtils::MappingRegistry.new

  MappingRegistry.set(
    GoogleSearchResult,
    ::SOAP::SOAPStruct,
    ::SOAP::RPCUtils::MappingRegistry::TypedStructFactory,
    [ XSD::QName.new( "urn:GoogleSearch", "GoogleSearchResult" ) ]
  )
  MappingRegistry.set(
    ResultElementArray,
    ::SOAP::SOAPArray,
    ::SOAP::RPCUtils::MappingRegistry::TypedArrayFactory,
    [ XSD::QName.new( "urn:GoogleSearch", "ResultElement" ) ]
  )
  MappingRegistry.set(
    DirectoryCategoryArray,
    ::SOAP::SOAPArray,
    ::SOAP::RPCUtils::MappingRegistry::TypedArrayFactory,
    [ XSD::QName.new( "urn:GoogleSearch", "DirectoryCategory" ) ]
  )
  MappingRegistry.set(
    ResultElement,
    ::SOAP::SOAPStruct,
    ::SOAP::RPCUtils::MappingRegistry::TypedStructFactory,
    [ XSD::QName.new( "urn:GoogleSearch", "ResultElement" ) ]
  )
  MappingRegistry.set(
    DirectoryCategory,
    ::SOAP::SOAPStruct,
    ::SOAP::RPCUtils::MappingRegistry::TypedStructFactory,
    [ XSD::QName.new( "urn:GoogleSearch", "DirectoryCategory" ) ]
  )
  
  Methods = [
    [ "doGetCachedPage", "doGetCachedPage", [
      [ "in", "key",
        [ SOAP::SOAPString ] ],
      [ "in", "url",
        [ SOAP::SOAPString ] ],
      [ "retval", "return",
        [ SOAP::SOAPBase64 ] ] ],
      "urn:GoogleSearchAction", "urn:GoogleSearch" ],
    [ "doSpellingSuggestion", "doSpellingSuggestion", [
      [ "in", "key",
        [ SOAP::SOAPString ] ],
      [ "in", "phrase",
        [ SOAP::SOAPString ] ],
      [ "retval", "return",
        [ SOAP::SOAPString ] ] ],
      "urn:GoogleSearchAction", "urn:GoogleSearch" ],
    [ "doGoogleSearch", "doGoogleSearch", [
      [ "in", "key",
        [ SOAP::SOAPString ] ],
      [ "in", "q",
        [ SOAP::SOAPString ] ],
      [ "in", "start",
        [ SOAP::SOAPInt ] ],
      [ "in", "maxResults",
        [ SOAP::SOAPInt ] ],
      [ "in", "filter",
        [ SOAP::SOAPBoolean ] ],
      [ "in", "restrict",
        [ SOAP::SOAPString ] ],
      [ "in", "safeSearch",
        [ SOAP::SOAPBoolean ] ],
      [ "in", "lr",
        [ SOAP::SOAPString ] ],
      [ "in", "ie",
        [ SOAP::SOAPString ] ],
      [ "in", "oe",
        [ SOAP::SOAPString ] ],
      [ "retval", "return",
        [ ::SOAP::SOAPStruct, "urn:GoogleSearch", "GoogleSearchResult" ] ] ],
      "urn:GoogleSearchAction", "urn:GoogleSearch" ]
  ]

  DefaultEndpointUrl = "http://api.google.com/search/beta2"

  attr_accessor :mappingRegistry
  attr_reader :endPointUrl
  attr_reader :wireDumpDev
  attr_reader :wireDumpFileBase
  attr_reader :httpProxy

  def initialize( endpointUrl = DefaultEndpointUrl, httpProxy = nil )
    @endpointUrl = endpointUrl
    @mappingRegistry = MappingRegistry
    @wireDumpDev = nil
    @dumpFileBase = nil
    @httpProxy = ENV[ 'http_proxy' ] || ENV[ 'HTTP_PROXY' ]
    @handler = ::SOAP::HTTPPostStreamHandler.new( @endpointUrl, @httpProxy,
      ::SOAP::Charset.getEncodingLabel )
    @proxy = ::SOAP::SOAPProxy.new( @namespace, @handler )
    @proxy.allowUnqualifiedElement = true
    addMethod
  end

  def setEndpointUrl( endpointUrl )
    @endpointUrl = endpointUrl
    @handler.endpointUrl = @endpointUrl if @handler
  end

  def setWireDumpDev( dumpDev )
    @wireDumpDev = dumpDev
    @handler.dumpDev = @wireDumpDev if @handler
  end

  def setWireDumpFileBase( base )
    @dumpFileBase = base
  end

  def setHttpProxy( httpProxy )
    @httpProxy = httpProxy
    @handler.proxy = @httpProxy if @handler
  end

  def setDefaultEncodingStyle( encodingStyle )
    @proxy.defaultEncodingStyle = encodingStyle
  end

  def getDefaultEncodingStyle
    @proxy.defaultEncodingStyle
  end

  def call( methodName, *params )
    # Convert parameters: params array => SOAPArray => members array
    params = ::SOAP::RPCUtils.obj2soap( params, @mappingRegistry ).to_a
    header, body = @proxy.call( nil, methodName, *params )
    unless body
      raise EmptyResponseError.new( "Empty response." )
    end

    # Check Fault.
    begin
      @proxy.checkFault( body )
    rescue ::SOAP::FaultError => e
      ::SOAP::RPCUtils.fault2exception( e )
    end

    ret = body.response ?
      ::SOAP::RPCUtils.soap2obj( body.response, @mappingRegistry ) : nil
    if body.outParams
      outParams = body.outParams.collect { | outParam |
	::SOAP::RPCUtils.soap2obj( outParam )
      }
      return [ ret ].concat( outParams )
    else
      return ret
    end
  end

private 

  def addMethod
    Methods.each do | methodNameAs, methodName, params, soapAction, namespace |
      @proxy.addMethodAs( methodNameAs, methodName, params, soapAction,
	namespace )
      addMethodInterface( methodName, params )
    end
  end

  def addMethodInterface( name, params )
    self.instance_eval <<-EOD
      def #{ name }( *params )
	call( "#{ name }", *params )
      end
    EOD
  end
end