File: GoogleSearchDriver.rb

package info (click to toggle)
ruby-soap4r 2.0.5-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,032 kB
  • sloc: ruby: 52,729; xml: 266; sh: 42; javascript: 20; makefile: 13; perl: 10
file content (102 lines) | stat: -rw-r--r-- 3,260 bytes parent folder | download | duplicates (5)
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
require 'GoogleSearch.rb'

require 'soap/rpc/driver'

class GoogleSearchPort < ::SOAP::RPC::Driver
  DefaultEndpointUrl = "http://api.google.com/search/beta2"
  MappingRegistry = ::SOAP::Mapping::Registry.new

  MappingRegistry.set(
    GoogleSearchResult,
    ::SOAP::SOAPStruct,
    ::SOAP::Mapping::Registry::TypedStructFactory,
    { :type => XSD::QName.new("urn:GoogleSearch", "GoogleSearchResult") }
  )
  MappingRegistry.set(
    ResultElementArray,
    ::SOAP::SOAPArray,
    ::SOAP::Mapping::Registry::TypedArrayFactory,
    { :type => XSD::QName.new("urn:GoogleSearch", "ResultElement") }
  )
  MappingRegistry.set(
    DirectoryCategoryArray,
    ::SOAP::SOAPArray,
    ::SOAP::Mapping::Registry::TypedArrayFactory,
    { :type => XSD::QName.new("urn:GoogleSearch", "DirectoryCategory") }
  )
  MappingRegistry.set(
    ResultElement,
    ::SOAP::SOAPStruct,
    ::SOAP::Mapping::Registry::TypedStructFactory,
    { :type => XSD::QName.new("urn:GoogleSearch", "ResultElement") }
  )
  MappingRegistry.set(
    DirectoryCategory,
    ::SOAP::SOAPStruct,
    ::SOAP::Mapping::Registry::TypedStructFactory,
    { :type => 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", :rpc
    ],
    ["doSpellingSuggestion", "doSpellingSuggestion",
      [
        [:in, "key", ["::SOAP::SOAPString"]],
        [:in, "phrase", ["::SOAP::SOAPString"]],
        [:retval, "return", ["::SOAP::SOAPString"]]
      ],
      "urn:GoogleSearchAction", "urn:GoogleSearch", :rpc
    ],
    ["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", ["GoogleSearchResult", "urn:GoogleSearch", "GoogleSearchResult"]]
      ],
      "urn:GoogleSearchAction", "urn:GoogleSearch", :rpc
    ]
  ]

  def initialize(endpoint_url = nil)
    endpoint_url ||= DefaultEndpointUrl
    super(endpoint_url, nil)
    self.mapping_registry = MappingRegistry
    init_methods
  end

private

  def init_methods
    Methods.each do |name_as, name, params, soapaction, namespace, style|
      qname = XSD::QName.new(namespace, name_as)
      if style == :document
        @proxy.add_document_method(soapaction, name, params)
        add_document_method_interface(name, params)
      else
        @proxy.add_rpc_method(qname, soapaction, name, params)
        add_rpc_method_interface(name, params)
      end
      if name_as != name and name_as.capitalize == name.capitalize
        ::SOAP::Mapping.define_singleton_method(self, name_as) do |*arg|
          __send__(name, *arg)
        end
      end
    end
  end
end