|
| 1 | +require "wasabi/document" |
| 2 | +require "httpi/request" |
| 3 | +require "akami" |
| 4 | + |
| 5 | +require "ads_savon/soap/xml" |
| 6 | +require "ads_savon/soap/request" |
| 7 | +require "ads_savon/soap/response" |
| 8 | +require "ads_savon/soap/request_builder" |
| 9 | + |
| 10 | +module GoogleAdsSavon |
| 11 | + |
| 12 | + # = GoogleAdsSavon::Client |
| 13 | + # |
| 14 | + # GoogleAdsSavon::Client is the main object for connecting to a SOAP service. |
| 15 | + class Client |
| 16 | + |
| 17 | + # Initializes the GoogleAdsSavon::Client for a SOAP service. Accepts a +block+ which is evaluated in the |
| 18 | + # context of this object to let you access the +wsdl+, +http+, and +wsse+ methods. |
| 19 | + # |
| 20 | + # == Examples |
| 21 | + # |
| 22 | + # # Using a remote WSDL |
| 23 | + # client = GoogleAdsSavon::Client.new("http://example.com/UserService?wsdl") |
| 24 | + # |
| 25 | + # # Using a local WSDL |
| 26 | + # client = GoogleAdsSavon::Client.new File.expand_path("../wsdl/service.xml", __FILE__) |
| 27 | + # |
| 28 | + # # Directly accessing a SOAP endpoint |
| 29 | + # client = GoogleAdsSavon::Client.new do |
| 30 | + # wsdl.endpoint = "http://example.com/UserService" |
| 31 | + # wsdl.namespace = "http://users.example.com" |
| 32 | + # end |
| 33 | + def initialize(wsdl_document = nil, &block) |
| 34 | + self.config = GoogleAdsSavon.config.clone |
| 35 | + wsdl.document = wsdl_document if wsdl_document |
| 36 | + |
| 37 | + process 1, &block if block |
| 38 | + wsdl.request = http |
| 39 | + end |
| 40 | + |
| 41 | + # Accessor for the <tt>GoogleAdsSavon::Config</tt>. |
| 42 | + attr_accessor :config |
| 43 | + |
| 44 | + # Returns the <tt>Wasabi::Document</tt>. |
| 45 | + def wsdl |
| 46 | + @wsdl ||= Wasabi::Document.new |
| 47 | + end |
| 48 | + |
| 49 | + # Returns the <tt>HTTPI::Request</tt>. |
| 50 | + def http |
| 51 | + @http ||= HTTPI::Request.new |
| 52 | + end |
| 53 | + |
| 54 | + # Returns the <tt>Akami::WSSE</tt> object. |
| 55 | + def wsse |
| 56 | + @wsse ||= Akami.wsse |
| 57 | + end |
| 58 | + |
| 59 | + # Executes a SOAP request for a given SOAP action. Accepts a +block+ which is evaluated in the |
| 60 | + # context of the <tt>SOAP::RequestBuilder</tt> object to let you access its +soap+, +wsdl+, |
| 61 | + # +http+ and +wsse+ methods. |
| 62 | + # |
| 63 | + # == Examples |
| 64 | + # |
| 65 | + # # Calls a "getUser" SOAP action with the payload of "<userId>123</userId>" |
| 66 | + # client.request(:get_user) { soap.body = { :user_id => 123 } } |
| 67 | + # |
| 68 | + # # Prefixes the SOAP input tag with a given namespace: "<wsdl:GetUser>...</wsdl:GetUser>" |
| 69 | + # client.request(:wsdl, "GetUser") { soap.body = { :user_id => 123 } } |
| 70 | + # |
| 71 | + # # SOAP input tag with attributes: <getUser xmlns:wsdl="http://example.com">...</getUser>" |
| 72 | + # client.request(:get_user, "xmlns:wsdl" => "http://example.com") |
| 73 | + def request(*args, &block) |
| 74 | + raise ArgumentError, "GoogleAdsSavon::Client#request requires at least one argument" if args.empty? |
| 75 | + |
| 76 | + options = extract_options(args) |
| 77 | + |
| 78 | + request_builder = SOAP::RequestBuilder.new(options.delete(:input), options) |
| 79 | + request_builder.wsdl = wsdl |
| 80 | + request_builder.http = http.dup |
| 81 | + request_builder.wsse = wsse.dup |
| 82 | + request_builder.config = config.dup |
| 83 | + |
| 84 | + post_configuration = lambda { process(0, request_builder, &block) if block } |
| 85 | + |
| 86 | + response = request_builder.request(&post_configuration).response |
| 87 | + http.set_cookies(response.http) |
| 88 | + |
| 89 | + if wsse.verify_response |
| 90 | + WSSE::VerifySignature.new(response.http.body).verify! |
| 91 | + end |
| 92 | + |
| 93 | + response |
| 94 | + end |
| 95 | + |
| 96 | + private |
| 97 | + |
| 98 | + # Expects an Array of +args+ and returns a Hash containing the SOAP input, |
| 99 | + # the namespace (might be +nil+), the SOAP action (might be +nil+), |
| 100 | + # the SOAP body (might be +nil+), and a Hash of attributes for the input |
| 101 | + # tag (which might be empty). |
| 102 | + def extract_options(args) |
| 103 | + attributes = Hash === args.last ? args.pop : {} |
| 104 | + body = attributes.delete(:body) |
| 105 | + soap_action = attributes.delete(:soap_action) |
| 106 | + |
| 107 | + namespace_identifier = args.size > 1 ? args.shift.to_sym : nil |
| 108 | + input = args.first |
| 109 | + |
| 110 | + remove_blank_values( |
| 111 | + :namespace_identifier => namespace_identifier, |
| 112 | + :input => input, |
| 113 | + :attributes => attributes, |
| 114 | + :body => body, |
| 115 | + :soap_action => soap_action |
| 116 | + ) |
| 117 | + end |
| 118 | + |
| 119 | + # Processes a given +block+. Yields objects if the block expects any arguments. |
| 120 | + # Otherwise evaluates the block in the context of +instance+. |
| 121 | + def process(offset = 0, instance = self, &block) |
| 122 | + block.arity > 0 ? yield_objects(offset, instance, &block) : evaluate(instance, &block) |
| 123 | + end |
| 124 | + |
| 125 | + # Yields a number of objects to a given +block+ depending on how many arguments |
| 126 | + # the block is expecting. |
| 127 | + def yield_objects(offset, instance, &block) |
| 128 | + to_yield = [:soap, :wsdl, :http, :wsse] |
| 129 | + yield *(to_yield[offset, block.arity].map { |obj_name| instance.send(obj_name) }) |
| 130 | + end |
| 131 | + |
| 132 | + # Evaluates a given +block+ inside +instance+. Stores the original block binding. |
| 133 | + def evaluate(instance, &block) |
| 134 | + original_self = eval "self", block.binding |
| 135 | + |
| 136 | + # A proxy that attemps to make method calls on +instance+. If a NoMethodError is |
| 137 | + # raised, the call will be made on +original_self+. |
| 138 | + proxy = Object.new |
| 139 | + proxy.instance_eval do |
| 140 | + class << self |
| 141 | + attr_accessor :original_self, :instance |
| 142 | + end |
| 143 | + |
| 144 | + def method_missing(method, *args, &block) |
| 145 | + instance.send(method, *args, &block) |
| 146 | + rescue NoMethodError |
| 147 | + original_self.send(method, *args, &block) |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + proxy.instance = instance |
| 152 | + proxy.original_self = original_self |
| 153 | + |
| 154 | + proxy.instance_eval &block |
| 155 | + end |
| 156 | + |
| 157 | + # Removes all blank values from a given +hash+. |
| 158 | + def remove_blank_values(hash) |
| 159 | + hash.delete_if { |_, value| value.respond_to?(:empty?) ? value.empty? : !value } |
| 160 | + end |
| 161 | + |
| 162 | + end |
| 163 | +end |
0 commit comments