sem4r 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. data/Gemfile +13 -2
  2. data/Gemfile.lock +13 -7
  3. data/README.rdoc +44 -7
  4. data/Rakefile +2 -122
  5. data/VERSION.yml +1 -1
  6. data/bin/sem +1 -0
  7. data/examples_sem4r/01_get_account.rb +1 -0
  8. data/examples_sem4r/02_get_info.rb +1 -0
  9. data/examples_sem4r/03_list_ad.rb +1 -0
  10. data/examples_sem4r/04_list_keywords.rb +1 -0
  11. data/examples_sem4r/05_request_report.rb +25 -14
  12. data/examples_sem4r/{05_request_report_2010.rb → 06_request_report_definition.rb} +2 -4
  13. data/examples_sem4r/{06_create_campaigns.rb → 07_create_campaigns.rb} +1 -0
  14. data/examples_sem4r/{07_create_campaigns_block.rb → 08_create_campaigns_block.rb} +1 -0
  15. data/examples_sem4r/{07_create_campaigns_simple.rb → 09_create_campaigns_simple.rb} +1 -0
  16. data/examples_sem4r/{08_ad_params.rb → 10_ad_params.rb} +1 -0
  17. data/examples_sem4r/{09_targeting_idea.rb → 11_targeting_idea.rb} +1 -0
  18. data/examples_sem4r/{10_get_location.rb → 12_get_location.rb} +1 -0
  19. data/examples_sem4r/{11_submit_bulk_job.rb → 13_submit_bulk_job.rb} +1 -1
  20. data/examples_sem4r/{12_list_bulk_job.rb → 14_list_bulk_job.rb} +3 -0
  21. data/examples_sem4r/30_prune_empty_adgroup.rb +1 -0
  22. data/examples_sem4r/31_empty_accounts.rb +1 -0
  23. data/examples_sem4r/example_helper.rb +2 -1
  24. data/lib/sem4r/ad_group/ad_group.rb +22 -7
  25. data/lib/sem4r/ad_group/ad_group_bids.rb +1 -1
  26. data/lib/sem4r/ad_group/ad_group_service.rb +12 -5
  27. data/lib/sem4r/ad_group_ad/ad_group_ad.rb +1 -0
  28. data/lib/sem4r/ad_group_ad/ad_group_ad_operations.rb +1 -0
  29. data/lib/sem4r/ad_group_ad/ad_group_ad_service.rb +14 -9
  30. data/lib/sem4r/ad_group_ad/ad_group_mobile_ad.rb +16 -25
  31. data/lib/sem4r/ad_group_ad/ad_group_text_ad.rb +18 -14
  32. data/lib/sem4r/ad_group_criterion/ad_group_criterion.rb +27 -19
  33. data/lib/sem4r/ad_group_criterion/ad_group_criterion_bids.rb +2 -1
  34. data/lib/sem4r/ad_group_criterion/ad_group_criterion_operations.rb +1 -0
  35. data/lib/sem4r/ad_group_criterion/ad_group_criterion_service.rb +9 -4
  36. data/lib/sem4r/ad_group_criterion/criterion.rb +1 -0
  37. data/lib/sem4r/ad_group_criterion/criterion_keyword.rb +1 -0
  38. data/lib/sem4r/ad_group_criterion/criterion_placement.rb +1 -0
  39. data/lib/sem4r/ad_param/ad_param.rb +24 -8
  40. data/lib/sem4r/ad_param/ad_param_service.rb +8 -5
  41. data/lib/sem4r/adwords.rb +232 -76
  42. data/lib/sem4r/base.rb +1 -1
  43. data/lib/sem4r/bulk_mutate_job/bulk_mutate_job.rb +41 -35
  44. data/lib/sem4r/bulk_mutate_job/bulk_mutate_job_account_extension.rb +49 -15
  45. data/lib/sem4r/bulk_mutate_job/bulk_mutate_job_selector.rb +29 -20
  46. data/lib/sem4r/bulk_mutate_job/bulk_mutate_job_service.rb +13 -14
  47. data/lib/sem4r/bulk_mutate_job/job_operations.rb +1 -1
  48. data/lib/sem4r/campaign/campaign.rb +1 -0
  49. data/lib/sem4r/campaign/campaign_account_extension.rb +2 -1
  50. data/lib/sem4r/campaign/campaign_service.rb +9 -5
  51. data/lib/sem4r/credentials.rb +11 -5
  52. data/lib/sem4r/geo_location/address.rb +56 -0
  53. data/lib/sem4r/geo_location/geo_location_account_extension.rb +27 -6
  54. data/lib/sem4r/geo_location/geo_location_selector.rb +57 -0
  55. data/lib/sem4r/geo_location/geo_location_service.rb +9 -16
  56. data/lib/sem4r/info/info_account_extension.rb +3 -2
  57. data/lib/sem4r/info/info_selector.rb +2 -1
  58. data/lib/sem4r/info/info_service.rb +10 -3
  59. data/lib/sem4r/operation.rb +39 -20
  60. data/lib/sem4r/report_definition/report_definition.rb +37 -19
  61. data/lib/sem4r/report_definition/report_definition_account_extension.rb +66 -16
  62. data/lib/sem4r/report_definition/report_definition_operation.rb +2 -2
  63. data/lib/sem4r/report_definition/report_definition_selector.rb +1 -1
  64. data/lib/sem4r/report_definition/report_definition_service.rb +11 -7
  65. data/lib/sem4r/report_definition/report_field.rb +3 -2
  66. data/lib/sem4r/sem4r_templates.rb +77 -0
  67. data/lib/sem4r/{services/service.rb → service.rb} +23 -23
  68. data/lib/sem4r/targeting_idea/targeting_idea.rb +4 -4
  69. data/lib/sem4r/targeting_idea/targeting_idea_account_extension.rb +1 -1
  70. data/lib/sem4r/targeting_idea/targeting_idea_selector.rb +6 -6
  71. data/lib/sem4r/targeting_idea/targeting_idea_service.rb +7 -5
  72. data/lib/sem4r/v13_account/account_account_extension.rb +7 -10
  73. data/lib/sem4r/v13_account/account_service.rb +10 -4
  74. data/lib/sem4r/v13_account/billing_address.rb +2 -2
  75. data/lib/sem4r/v13_report/report_service.rb +11 -9
  76. data/lib/sem4r.rb +46 -31
  77. data/lib/{sem4r/cli → sem4r_cli}/cli_helpers.rb +3 -2
  78. data/lib/{sem4r/cli/cli_command.rb → sem4r_cli/cli_mini_framework.rb} +77 -32
  79. data/lib/{sem4r/cli/cli_common_args.rb → sem4r_cli/cli_sem.rb} +177 -157
  80. data/lib/{sem4r/cli/commands/cli_list_ads.rb → sem4r_cli/commands/cli_ads.rb} +7 -9
  81. data/lib/sem4r_cli/commands/cli_campaign.rb +69 -0
  82. data/lib/{sem4r/cli/commands/cli_list_client.rb → sem4r_cli/commands/cli_clients.rb} +2 -2
  83. data/lib/{sem4r/cli → sem4r_cli}/commands/cli_ideas.rb +35 -27
  84. data/lib/{sem4r/cli → sem4r_cli}/commands/cli_info.rb +3 -4
  85. data/lib/{sem4r/cli/commands/cli_repdef.rb → sem4r_cli/commands/cli_job.rb} +57 -55
  86. data/lib/{sem4r/cli/commands/cli_list_keywords.rb → sem4r_cli/commands/cli_keywords.rb} +14 -13
  87. data/lib/sem4r_cli/commands/cli_profile.rb +138 -0
  88. data/lib/{sem4r/cli/commands/cli_report.rb → sem4r_cli/commands/cli_repdef.rb} +67 -20
  89. data/lib/{sem4r/cli/commands/cli_request_report.rb → sem4r_cli/commands/cli_report.rb} +82 -19
  90. data/lib/sem4r_cli.rb +5 -7
  91. data/lib/sem4r_soap/http_connector.rb +206 -0
  92. data/lib/{soap_helpers → sem4r_soap}/soap_attributes.rb +8 -3
  93. data/lib/{sem4r/services → sem4r_soap}/soap_dumper.rb +36 -20
  94. data/lib/{sem4r/services → sem4r_soap}/soap_error.rb +5 -2
  95. data/lib/sem4r_soap/soap_response.rb +75 -0
  96. data/lib/sem4r_soap/soap_service.rb +137 -0
  97. data/lib/{sem4r/cli/cli_sem.rb → sem4r_soap/soap_service_v13.rb} +27 -28
  98. data/lib/sem4r_soap/soap_service_v2010.rb +80 -0
  99. data/lib/sem4r_soap.rb +17 -0
  100. data/sem4r.gemspec +93 -58
  101. data/spec/build_fixtures.rb +49 -42
  102. data/spec/fixtures/password.example.yml +3 -0
  103. data/spec/fixtures/sem4r.example.yml +6 -0
  104. data/spec/fixtures/{services/error.xml → soap_error.xml} +0 -0
  105. data/spec/fixtures/soap_error2.xml +29 -0
  106. data/spec/helpers/dump_interceptor.rb +90 -0
  107. data/spec/helpers/fixtures_bulk_mutate_job.rb +48 -0
  108. data/spec/helpers/fixtures_geo_location.rb +45 -0
  109. data/{lib/sem4r/campaign_criterion/campaign_criterion_service.rb → spec/helpers/fixtures_info.rb} +10 -6
  110. data/spec/helpers/fixtures_report_definition.rb +65 -0
  111. data/spec/{rspec_hash.rb → helpers/rspec_hash.rb} +1 -0
  112. data/spec/{rspec_matchers.rb → helpers/rspec_matchers.rb} +46 -19
  113. data/spec/{rspec_sem4r_helper.rb → helpers/rspec_sem4r_helper.rb} +19 -16
  114. data/spec/{sem4r_stubs.rb → helpers/sem4r_stubs.rb} +5 -4
  115. data/spec/rspec_helper.rb +4 -4
  116. data/spec/sem4r/account_spec.rb +2 -3
  117. data/spec/sem4r/ad_group/ad_group_service_spec.rb +1 -1
  118. data/spec/sem4r/ad_group/ad_group_spec.rb +1 -1
  119. data/spec/sem4r/ad_group_ad/ad_group_ad_operation_spec.rb +3 -3
  120. data/spec/sem4r/adwords_spec.rb +62 -39
  121. data/{lib/sem4r/cli/commands/cli_list_campaign.rb → spec/sem4r/bulk_mutate_job/bulk_mutate_job_selector_spec.rb} +14 -11
  122. data/spec/sem4r/bulk_mutate_job/bulk_mutate_job_service_spec.rb +2 -3
  123. data/spec/sem4r/bulk_mutate_job/bulk_mutate_job_spec.rb +23 -17
  124. data/spec/sem4r/bulk_mutate_job/fixtures/get-list_job-req.xml +35 -0
  125. data/spec/sem4r/bulk_mutate_job/fixtures/get-list_job-res.xml +417 -0
  126. data/spec/sem4r/bulk_mutate_job/fixtures/mutate-add_job-req.xml +106 -0
  127. data/spec/sem4r/bulk_mutate_job/fixtures/mutate-add_job-res.xml +48 -0
  128. data/spec/sem4r/bulk_mutate_job/job_operation_spec.rb +5 -7
  129. data/spec/sem4r/campaign/campaign_service_spec.rb +2 -2
  130. data/spec/sem4r/credentials_spec.rb +10 -8
  131. data/spec/sem4r/geo_location/address_spec.rb +62 -0
  132. data/spec/sem4r/geo_location/fixtures/get-req.xml +42 -0
  133. data/spec/sem4r/geo_location/fixtures/get-res.xml +60 -0
  134. data/spec/sem4r/info/fixtures/get-req.xml +34 -0
  135. data/spec/sem4r/info/fixtures/get-res.xml +27 -0
  136. data/spec/sem4r/nokogiri_parsing_spec.rb +1 -0
  137. data/spec/sem4r/operation_spec.rb +72 -0
  138. data/spec/sem4r/report_definition/fixtures/get-list-repdef-req.xml +22 -0
  139. data/spec/sem4r/report_definition/fixtures/get-list-repdef-res.xml +73 -0
  140. data/spec/sem4r/report_definition/fixtures/getReportFields-req.xml +24 -0
  141. data/spec/sem4r/report_definition/fixtures/getReportFields-res.xml +1199 -0
  142. data/spec/sem4r/report_definition/fixtures/mutate-add-report-req.xml +63 -0
  143. data/spec/sem4r/report_definition/fixtures/mutate-add-report-res.xml +68 -0
  144. data/spec/sem4r/report_definition/report_definition_service_spec.rb +5 -5
  145. data/spec/sem4r/report_definition/report_definition_spec.rb +28 -43
  146. data/{lib/sem4r/cli/commands/cli_list_report.rb → spec/sem4r/report_definition/report_field_spec.rb} +12 -10
  147. data/spec/sem4r/rexml_parsing_spec.rb +1 -0
  148. data/spec/sem4r/{services/service_spec.rb → service_spec.rb} +1 -1
  149. data/spec/sem4r/targeting_idea/targeting_idea_selector_spec.rb +1 -1
  150. data/spec/sem4r/targeting_idea/targeting_idea_service_spec.rb +1 -1
  151. data/spec/sem4r/targeting_idea/targeting_idea_spec.rb +1 -1
  152. data/spec/sem4r/v13_account/account_service_spec.rb +2 -2
  153. data/spec/sem4r/v13_report/report_service_spec.rb +2 -2
  154. data/spec/{sem4r/cli → sem4r_cli}/cli_spec.rb +22 -27
  155. data/spec/{soap_helpers → sem4r_soap}/soap_attributes_spec.rb +3 -3
  156. data/spec/{sem4r/services/soap_message_v13_spec.rb → sem4r_soap/soap_response_spec.rb} +10 -15
  157. data/spec/{sem4r/services/soap_call_spec.rb → sem4r_soap/soap_service_spec.rb} +35 -54
  158. data/tasks/jeweler.rake +66 -0
  159. data/tasks/rspec.rake +21 -0
  160. data/tasks/sem4r.rake +25 -0
  161. data/tasks/yard.rake +31 -0
  162. metadata +173 -76
  163. data/lib/sem4r/ad_extension_override/ad_extension_override_service.rb +0 -30
  164. data/lib/sem4r/api_counters.rb +0 -8
  165. data/lib/sem4r/campaign_target/campaign_target_service.rb +0 -30
  166. data/lib/sem4r/cli/commands/cli_download_report.rb +0 -82
  167. data/lib/sem4r/services/http_connector.rb +0 -93
  168. data/lib/sem4r/services/soap_call.rb +0 -122
  169. data/lib/sem4r/services/soap_connector.rb +0 -139
  170. data/lib/sem4r/services/soap_message_v13.rb +0 -135
  171. data/lib/sem4r/services/soap_message_v2010.rb +0 -184
  172. data/lib/sem4r/v13_traffic_estimator/traffic_estimator_service.rb +0 -30
  173. data/spec/aggregates_rspec_helper.rb +0 -59
  174. data/spec/sem4r/report_definition/fixtures/mutate_add-req.xml +0 -24
  175. data/spec/sem4r/report_definition/fixtures/mutate_add-req_orig.xml +0 -45
@@ -1,82 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- # -------------------------------------------------------------------------
3
- # Copyright (c) 2009-2010 Sem4r sem4ruby@gmail.com
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining
6
- # a copy of this software and associated documentation files (the
7
- # "Software"), to deal in the Software without restriction, including
8
- # without limitation the rights to use, copy, modify, merge, publish,
9
- # distribute, sublicense, and/or sell copies of the Software, and to
10
- # permit persons to whom the Software is furnished to do so, subject to
11
- # the following conditions:
12
- #
13
- # The above copyright notice and this permission notice shall be
14
- # included in all copies or substantial portions of the Software.
15
- #
16
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
- #
24
- # -------------------------------------------------------------------------
25
-
26
- module Sem4r
27
- class CliDownloadReport < CliCommand
28
-
29
- def self.command
30
- "download"
31
- end
32
-
33
- def self.description
34
- "download a generated report"
35
- end
36
-
37
- def initialize(common_args)
38
- @common_args = common_args
39
- end
40
-
41
- def command_opt_parser(options)
42
- opt_parser = OptionParser.new
43
- opt_parser.banner = "Usage #{self.class.command} [command_options ] <report_id>"
44
- opt_parser.separator ""
45
- opt_parser.separator "#{self.class.description}"
46
- opt_parser.on("-h", "--help", "show this message") do
47
- puts opt_parser
48
- options.exit = true
49
- end
50
- end
51
-
52
- def parse_and_run(argv)
53
- options = OpenStruct.new
54
- rest = command_opt_parser(options).parse( argv )
55
- if options.exit
56
- return false
57
- end
58
- if rest.length != 1
59
- puts "missing report id"
60
- return false
61
- end
62
-
63
- account = @common_args.account
64
- report_id = rest[0].to_i
65
- report = account.reports.find { |r| r.id == report_id }
66
- if report.nil?
67
- puts "report '#{report_id}' not found"
68
- return false
69
- end
70
-
71
- if report.status != 'Completed'
72
- puts "cannot donwload report with status '#{report.status}'"
73
- return false
74
- end
75
- report.download("test_report.xml")
76
-
77
- account.adwords.p_counters
78
- return true
79
- end
80
-
81
- end
82
- end
@@ -1,93 +0,0 @@
1
- # -------------------------------------------------------------------------
2
- # Copyright (c) 2009-2010 Sem4r sem4ruby@gmail.com
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- #
23
- # -------------------------------------------------------------------------
24
-
25
- module Sem4r
26
-
27
- class HttpConnector
28
- begin
29
- require 'patron'
30
- UsePatron = false
31
- # $stderr.puts "Using patron gems"
32
- rescue LoadError
33
- UsePatron = false
34
- # $stderr.puts "Patron not found, degrade to net/https"
35
- end
36
-
37
- if !UsePatron
38
- require 'net/https'
39
- require 'uri'
40
- # $stderr.puts "Using standard net/https"
41
- end
42
-
43
- def get_sess_for_host(uri)
44
- @sessions ||= {}
45
-
46
- if UsePatron
47
- url = uri.scheme + "://" + uri.host
48
- sess = @sessions[url]
49
- unless sess
50
- sess = Patron::Session.new
51
- # sess.connect_timeout = 3000 #millis
52
- sess.timeout = 12
53
- sess.base_url = url
54
- sess.headers['User-Agent'] = 'ruby'
55
- @sessions[url] = sess
56
- end
57
- sess
58
- else
59
- url = uri.scheme + "://" + uri.host
60
- sess = @sessions[url]
61
- unless sess
62
- sess = Net::HTTP.new(uri.host, uri.port)
63
- sess.use_ssl = (uri.scheme == "https")
64
- @sessions[url] = sess
65
- end
66
- sess
67
- end
68
- end
69
-
70
- def invalidate_sess(uri)
71
- sess = @sessions[uri.host]
72
- if sess
73
- # sess.close
74
- @sessions[uri.host] = nil
75
- end
76
- end
77
-
78
- def download(url, path_name)
79
-
80
- if UsePatron
81
- uri = URI.parse(url)
82
- sess = get_sess_for_host(uri)
83
- sess.get_file(uri.path, path_name)
84
- else
85
- require 'open-uri'
86
- data = open(url){ |f| f.read }
87
- File.open(path_name, "w") { |f| f.write(data) }
88
- end
89
-
90
- end
91
- end
92
-
93
- end
@@ -1,122 +0,0 @@
1
- # -------------------------------------------------------------------
2
- # Copyright (c) 2009-2010 Sem4r sem4ruby@gmail.com
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- # -------------------------------------------------------------------
23
-
24
- module Sem4r
25
- module SoapCall
26
-
27
- def self.included(base)
28
- base.extend ClassMethods
29
- end
30
-
31
- def helper_call_v13(credentials, soap_body_content)
32
- re = /<(\w+)/m # use double blackslash because we are into string
33
- match_data = soap_body_content.match(re)
34
- if match_data
35
- soap_action = match_data[1]
36
- else
37
- puts "errore"
38
- puts soap_body_content
39
- raise "Soapaction not found"
40
- end
41
-
42
- soap_message = SoapMessageV13.new( @connector, credentials)
43
- soap_message.body = soap_body_content
44
- if credentials.sandbox?
45
- soap_message.send(@sandbox_service_url, soap_action)
46
- else
47
- soap_message.send(@production_service_url, soap_action)
48
- end
49
- end
50
-
51
- def helper_call_v2010(credentials, soap_body_content)
52
- soap_message = SoapMessageV2010.new(@connector, credentials)
53
- soap_message.init( @header_namespace, @service_namespace )
54
- soap_message.body = soap_body_content
55
- if credentials.sandbox?
56
- soap_message.send(@sandbox_service_url)
57
- else
58
- soap_message.send(@production_service_url)
59
- end
60
- end
61
-
62
- def helper_call_v2010_raw(credentials, xml_message)
63
- soap_message = SoapMessageV2010.new(@connector, credentials)
64
- soap_message.init( @header_namespace, @service_namespace )
65
- if credentials.sandbox?
66
- soap_message.send_raw(@sandbox_service_url, xml_message)
67
- else
68
- soap_message.send_raw(@production_service_url, xml_message)
69
- end
70
- end
71
-
72
- module ClassMethods
73
- def soap_call(helper_version, method, options = {})
74
- options.assert_valid_keys(:mutate)
75
- mutate = options.delete :mutate
76
- if mutate.nil? or mutate
77
- smutate = "credentials.mutable?"
78
- else
79
- smutate = "true"
80
- end
81
- # public_method_pars = ['credentials'].concat(args).join(",")
82
-
83
- # private_method_pars = args.join(",")
84
- # private_method_pars = ", #{private_method_pars}" unless private_method_pars.empty?
85
- rubystr =<<-EOFS
86
- define_method :#{method.to_sym} do |*args|
87
- credentials = args.shift
88
- if #{smutate}
89
- soap_body_content = send("_#{method}", *args)
90
- #{helper_version}(credentials, soap_body_content)
91
- else
92
- raise "mutate methods '#{method}' cannot be called on read_only profile"
93
- end
94
- end
95
- EOFS
96
- eval rubystr
97
-
98
- rubystr =<<-EOFS
99
- define_method :#{(method.to_s + "_raw").to_sym} do |*args|
100
- credentials = args.shift
101
- soap_message = args.shift
102
- if #{smutate}
103
- #{helper_version}_raw(credentials, soap_message)
104
- else
105
- raise "mutate methods '#{method}' cannot be called on read_only profile"
106
- end
107
- end
108
- EOFS
109
- eval rubystr
110
- end
111
-
112
- def soap_call_v13(method, options = {})
113
- soap_call("helper_call_v13", method, options)
114
- end
115
-
116
- def soap_call_v2010(method, options = {})
117
- soap_call("helper_call_v2010", method, options)
118
- end
119
- end
120
-
121
- end
122
- end
@@ -1,139 +0,0 @@
1
- # -------------------------------------------------------------------
2
- # Copyright (c) 2009-2010 Sem4r sem4ruby@gmail.com
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- # -------------------------------------------------------------------
23
-
24
- module Sem4r
25
-
26
- class SoapConnector < HttpConnector
27
- include SoapDumper
28
-
29
- MAXRETRIES = 3
30
-
31
- def initialize
32
- super
33
- @logger = nil
34
- end
35
-
36
- def logger=(log)
37
- @logger = log
38
- end
39
-
40
- def authentication_token(email, password)
41
- str = "accountType=GOOGLE&Email=#{email}&Passwd=#{password}&service=adwords"
42
- str = URI.escape(str)
43
-
44
- uri = URI.parse( "https://www.google.com/accounts/ClientLogin" )
45
- sess = get_sess_for_host(uri)
46
- retries = 0; response = nil
47
- while retries <= MAXRETRIES and response.nil?
48
- retries += 1
49
-
50
- headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
51
-
52
- #########################
53
- if UsePatron
54
- begin
55
- e = nil
56
- response = sess.post( uri.path, str, headers )
57
- status = response.status
58
- rescue Patron::Error => e
59
- end
60
- else
61
- response = sess.request_post(uri.path, str, headers )
62
- # pp response.methods
63
- # pp response.class.to_s
64
- status = response.code.to_i
65
- # pp "status: #{status}"
66
- end
67
- ##########################
68
-
69
- if e
70
- @logger.warn("authentication retries!!! #{e.to_s}") if @logger
71
- invalidate_sess(uri)
72
- sleep(2 * retries) # wait 1 sec
73
- sess = get_sess_for_host(uri)
74
- end
75
- end
76
- unless response
77
- raise "Connection Error, Network is down?? :-((("
78
- end
79
-
80
- if status == 200
81
- return response.body[/Auth=(.*)/, 1]
82
- end
83
- raise Sem4rError, "authentication failed status is #{status}"
84
- end
85
-
86
- def send(service_url, soap_action, soap_message)
87
- begin
88
- uri = URI.parse(service_url)
89
- rescue URI::InvalidURIError
90
- puts "Invalid url -- #{service_url}"
91
- raise
92
- end
93
-
94
- headers = {
95
- "Content-Type" => "text/xml; charset=utf-8",
96
- "Content-Length" => soap_message.length.to_s,
97
- "SOAPAction" => soap_action}
98
-
99
- retries = 0; response = nil
100
- sess = get_sess_for_host(uri)
101
- while retries <= MAXRETRIES and response.nil?
102
- retries += 1
103
- @logger.info("Post to #{uri.path} (#{soap_action})") if @logger
104
-
105
- ############################
106
- if UsePatron
107
- begin
108
- e = nil
109
- response = sess.post(uri.path, soap_message, headers)
110
- rescue Patron::Error => e
111
- end
112
- else
113
- begin
114
- response = sess.request_post(uri.path, soap_message, headers)
115
- rescue
116
- raise
117
- end
118
- end
119
- ##############################
120
-
121
- if e
122
- @logger.warn("soap_connector.send retries!!! #{e.to_s}") if @logger
123
- invalidate_sess(uri)
124
- sleep(2 * retries) # wait 1 sec
125
- sess = get_sess_for_host(uri)
126
- end
127
- end
128
- unless response
129
- raise Sem4rError, "Connection Error"
130
- end
131
-
132
- response_xml = response.body
133
- dump_soap_request(service_url, soap_message)
134
- dump_soap_response(service_url, response_xml)
135
- response_xml
136
- end
137
- end
138
-
139
- end
@@ -1,135 +0,0 @@
1
- # -------------------------------------------------------------------
2
- # Copyright (c) 2009-2010 Sem4r sem4ruby@gmail.com
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- # -------------------------------------------------------------------
23
-
24
- module Sem4r
25
-
26
- class SoapMessageV13
27
-
28
- attr_reader :response
29
- attr_reader :counters
30
-
31
- def initialize(connector, credentials)
32
- @credentials = credentials
33
- @connector = connector
34
- @response = nil
35
- @counters = {}
36
- end
37
-
38
- def body=(soap_body_content)
39
- @soap_body_content = soap_body_content
40
- end
41
-
42
- def send(service_url, soap_action)
43
- soap_message = build_soap_message
44
- response_xml = @connector.send(service_url, soap_action, soap_message)
45
- # erase namespace 'nsX'so it more simple parsing the xml
46
-
47
- # response_xml = response_xml.gsub(/ns\d:/, "")
48
- # @response = REXML::Document.new(response_xml)
49
- response_xml.gsub!(/\b(ns\d:|xsi:|s:|soapenv:|env:|soap:)/, "")
50
- response_xml.gsub!(/xmlns=["'].*?['"]/, '')
51
- @response = Nokogiri::XML::Document.parse(response_xml)
52
-
53
- # extract information from header
54
- # <soapenv:Header>
55
- # <responseTime soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns="https://adwords.google.com/api/adwords/v13">16</responseTime>
56
- # <operations soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns="https://adwords.google.com/api/adwords/v13">5</operations>
57
- # <units soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns="https://adwords.google.com/api/adwords/v13">5</units>
58
- # <requestId soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns="https://adwords.google.com/api/adwords/v13">abade53d3dbecd45600e7d14563f10f1</requestId>
59
- # </soapenv:Header>
60
-
61
- header = @response.xpath("//Header").first
62
- if header
63
- @counters = {
64
- :response_time => header.at_xpath('responseTime').text.to_i,
65
- :operations => header.at_xpath('operations').text.to_i,
66
- :units => header.at_xpath('units').text.to_i
67
- }
68
- end
69
-
70
- # check soap fault
71
- #<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
72
- # <soapenv:Body>
73
- # <soapenv:Fault>
74
- # <faultcode>soapenv:Server.generalException</faultcode>
75
- # <faultstring>An internal error has occurred. Please retry your request.</faultstring>
76
- # <detail>
77
- # <fault xmlns:ns1="https://adwords.google.com/api/adwords/v13">
78
- # <code>0</code>
79
- # <message>An internal error has occurred. Please retry your request.</message>
80
- # </fault>
81
- # </detail>
82
- # </soapenv:Fault>
83
- # </soapenv:Body>
84
- #</soapenv:Envelope>
85
- #=======
86
- fault_el = @response.xpath("//Fault").first
87
- if fault_el
88
- fault_code = fault_el.at_xpath('faultcode').text
89
- fault_string = fault_el.at_xpath('faultstring').text
90
- raise SoapError, "#{fault_code}: '#{fault_string}'"
91
- end
92
- self
93
- end
94
-
95
- private
96
-
97
- def build_soap_header(credentials)
98
- str= <<-EOFS
99
- <env:Header>
100
- <email env:mustUnderstand="0">#{credentials.email}</email>
101
- <password env:mustUnderstand="0">#{credentials.password}</password>
102
- EOFS
103
-
104
- if credentials.client_email
105
- str += "<clientEmail env:mustUnderstand=\"0\">#{credentials.client_email}</clientEmail>"
106
- end
107
-
108
- str += <<-EOFS
109
- <useragent env:mustUnderstand="0">#{credentials.useragent}</useragent>
110
- <developerToken env:mustUnderstand="0">#{credentials.developer_token}</developerToken>
111
- </env:Header>
112
- EOFS
113
- str
114
- end
115
-
116
- def build_soap_message
117
- soap_message = '<?xml version="1.0" encoding="utf-8" ?>'
118
- soap_message +=<<-EOFS
119
- <env:Envelope
120
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
121
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
122
- xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
123
- EOFS
124
- soap_message += build_soap_header(@credentials)
125
- soap_message += "<env:Body>"
126
- soap_message += @soap_body_content
127
- soap_message += <<-EOFS
128
- </env:Body>
129
- </env:Envelope>
130
- EOFS
131
- soap_message
132
- end
133
-
134
- end
135
- end