comscore_ruby 0.0.13 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ test/config.yml
6
+ doc/*
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ namespace :test do
4
+ desc "Run all tests in /test"
5
+ task :all do
6
+ Dir["test/**/*_test.rb"].each do |test_path|
7
+ system "ruby #{test_path}"
8
+ end
9
+ end
10
+ end
@@ -19,7 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_runtime_dependency "savon", ">= 0.9.7"
22
- s.add_runtime_dependency "activesupport", ">= 3.1.0"
22
+ #s.add_runtime_dependency "activesupport", ">= 3.0.0"
23
+ s.add_runtime_dependency "activesupport"
23
24
 
24
25
  # specify any dependencies here; for example:
25
26
  # s.add_development_dependency "rspec"
@@ -1,6 +1,7 @@
1
1
  module ComScore
2
2
  class Client
3
3
  BASE_URI = "https://api.comscore.com/"
4
+ DEFAULT_REPORT_WAIT_TIME = 5
4
5
  SERVICES = {
5
6
  :key_measures => {:wsdl => "#{BASE_URI}KeyMeasures.asmx?WSDL"},
6
7
  :audience_duplication => {:wsdl => "#{BASE_URI}DigitalCalc.asmx?WSDL"},
@@ -19,142 +20,112 @@ module ComScore
19
20
 
20
21
  def initialize(uname, pw)
21
22
  @username, @password = [uname, pw]
23
+ @client = Savon::Client.new
24
+ @client.http.auth.basic(@username, @password)
22
25
  end
23
26
 
24
- def log=(value)
25
- raise ArgumentError, "Parameter must be of type Boolean." unless !!value == value
26
- @log = value
27
- Savon.configure do |config|
28
- config.log = value
29
- config.log_level = :info if value == false
30
- end
31
- HTTPI.log = value
32
- end
33
-
34
- def log
35
- @log
36
- end
37
-
38
- def media_sets
39
- @client = Savon::Client.new(SERVICES[:key_measures][:wsdl])
40
- @client.http.auth.basic(@username, @password)
27
+ # Executes a request against ComScore's SOAP API. Service name should correspond to one of the available services you've subscribed to:
28
+ # - +:key_measures+
29
+ # - +:audience_duplication+
30
+ # - +:percent_media_trend+
31
+ # - etc... see http://api.comscore.com for more information
32
+ # You can (and should) pass a block to build the details of the request you're sending to the method. See the examples below.
33
+ #
34
+ # == Examples
35
+ #
36
+ # # Find the media property named "The Globe And Mail" within the Canadian dictionary from two months ago.
37
+ # client = ComScore::Client.new(username, password)
38
+ # client.request(:key_measures, :fetch_media) do |xml|
39
+ # xml.parameterId("media")
40
+ # xml.fetchMediaQuery("xmlns" => "http://comscore.com/FetchMedia") {
41
+ # xml.SearchCritera(:ExactMatch => "false", :Critera => "The Globe And Mail")
42
+ # }
43
+ # xml.reportQuery("xmlns" => "http://comscore.com/ReportQuery") {
44
+ # xml.Parameter(:KeyId => "geo", :Value => ComScore::GEOGRAPHIES["Canada"])
45
+ # xml.Parameter(:KeyId => "loc", :Value => ComScore::LOCATIONS["Home and Work"])
46
+ # xml.Parameter(:KeyId => "timeType", :Value => ComScore::TIME_TYPES["Months"])
47
+ # xml.Parameter(:KeyId => "timePeriod", :Value => DateTime.now.to_date.to_comscore_time_period - 2)
48
+ # xml.Parameter(:KeyId => "mediaSetType", :Value => ComScore::MEDIA_SET_TYPES["Ranked Categories"])
49
+ # }
50
+ # end
51
+ def request(service_name, method)
52
+ @client.wsdl.document = SERVICES[service_name][:wsdl]
41
53
 
42
- response = @client.request(:discover_parameter_values) do
54
+ @client.request(method) do
43
55
  soap.xml do |xml|
44
56
  xml.soap(:Envelope, "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema", "xmlns:soap" => "http://schemas.xmlsoap.org/soap/envelope/") {
45
57
  xml.soap(:Body) {
46
- xml.DiscoverParameterValues(:xmlns => "http://comscore.com/") {
47
- xml.parameterId("mediaSet")
48
- xml.query(:xmlns => "http://comscore.com/ReportQuery") {
49
- xml.Parameter(:KeyId => "geo", :Value => "124")
50
- xml.Parameter(:KeyId => "timeType", :Value => "1")
51
- xml.Parameter(:KeyId => "timePeriod", :Value => DateTime.now.to_date.to_comscore_time_period - 2)
52
- xml.Parameter(:KeyId => "mediaSetType", :Value => "1")
53
- }
58
+ xml.tag!(method.to_s.camelize, :xmlns => "http://comscore.com/") {
59
+ yield xml if block_given?
54
60
  }
55
61
  }
56
62
  }
57
63
  end
58
64
  end
59
-
60
- response.to_hash()[:discover_parameter_values_response][:discover_parameter_values_result][:enum_value]
61
65
  end
62
66
 
63
- def find_media(criterion = [])
64
- @client = Savon::Client.new(SERVICES[:key_measures][:wsdl])
65
- @client.http.auth.basic(@username, @password)
66
- response = @client.request(:fetch_media) do
67
- soap.xml do |xml|
68
- xml.soap(:Envelope, "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema", "xmlns:soap" => "http://schemas.xmlsoap.org/soap/envelope/") {
69
- xml.soap(:Body) {
70
- xml.FetchMedia("xmlns" => "http://comscore.com/") {
71
- xml.parameterId("media")
72
- xml.fetchMediaQuery("xmlns" => "http://comscore.com/FetchMedia") {
73
- criterion.each do |criteria|
74
- xml.SearchCritera(:ExactMatch => "false", :Critera => criteria)
75
- end
76
- } if criterion.count > 0
77
- xml.reportQuery("xmlns" => "http://comscore.com/ReportQuery") {
78
- xml.Parameter(:KeyId => "geo", :Value => "124")
79
- xml.Parameter(:KeyId => "loc", :Value => "0")
80
- xml.Parameter(:KeyId => "timeType", :Value => "1")
81
- xml.Parameter(:KeyId => "timePeriod", :Value => DateTime.now.to_date.to_comscore_time_period - 2)
82
- xml.Parameter(:KeyId => "mediaSetType", :Value => "1")
83
- }
84
- }
85
- }
86
- }
87
- end
88
- end
89
-
90
- response.to_hash()[:fetch_media_response][:fetch_media_result][:media_item]
91
- end
92
-
93
- # Requires: geo, loc, timeType, timePeriod, mediaSetType
94
- # Usage: find_media(:geo => "Canada", :loc => "")
95
- def get_report(report_type, opts = {})
96
- @client = Savon::Client.new(SERVICES[report_type][:wsdl])
97
- @client.http.auth.basic(@username, @password)
98
- job_queue_response = @client.request(:submit_report) do
99
- soap.xml do |xml|
100
- xml.soap(:Envelope, "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema", "xmlns:soap" => "http://schemas.xmlsoap.org/soap/envelope/") {
101
- xml.soap(:Body) {
102
- xml.SubmitReport("xmlns" => "http://comscore.com/") {
103
- xml.query("xmlns" => "http://comscore.com/ReportQuery") {
104
- opts.each { |key, value|
105
- if value.is_a?(Array)
106
- value.each do |v|
107
- xml.Parameter(:KeyId => key, :Value => v)
108
- end
109
- elsif value.is_a?(Date)
110
- xml.Parameter(:KeyId => key, :Value => value.to_comscore_time_period)
111
- else
112
- xml.Parameter(:KeyId => key, :Value => value)
113
- end
114
- }
115
- }
116
- }
117
- }
67
+ # Fetches report data against a given comScore +service+ (e.g. KeyMeasures, MediaTrend, etc).
68
+ #
69
+ # == Examples
70
+ #
71
+ # # Get a Key Measures report for a given media set ID
72
+ # client.get_report(:key_measures,
73
+ # :geo => ComScore::GEOGRAPHIES["Canada"],
74
+ # :loc => ComScore::LOCATIONS["Home and Work"],
75
+ # :timeType => ComScore::TIME_TYPES["Months"],
76
+ # :timePeriod => Date.new(2011, 8),
77
+ # :mediaSet => 778226,
78
+ # :mediaSetType => ComScore::MEDIA_SET_TYPES["Ranked Categories"],
79
+ # :targetType => ComScore::TARGET_TYPES["Simple"],
80
+ # :targetGroup => ComScore::TARGET_GROUPS["Total Audience"],
81
+ # :measure => [ComScore::MEASURES["Total Unique Visitors (000)"]]
82
+ # )
83
+ def get_report(service, opts = {})
84
+ job_queue_response = self.request(service, :submit_report) do |xml|
85
+ xml.query("xmlns" => "http://comscore.com/ReportQuery") {
86
+ opts.each { |key, value|
87
+ if value.is_a?(Array)
88
+ value.each do |v|
89
+ xml.Parameter(:KeyId => key, :Value => v)
90
+ end
91
+ elsif value.is_a?(Date)
92
+ xml.Parameter(:KeyId => key, :Value => value.to_comscore_time_period)
93
+ else
94
+ xml.Parameter(:KeyId => key, :Value => value)
95
+ end
118
96
  }
119
- end
97
+ }
120
98
  end
121
99
 
122
100
  job_id = job_queue_response.to_hash()[:submit_report_response][:submit_report_result][:job_id]
101
+ job_status = ""
123
102
 
124
- job_status = "Starting"
125
103
  begin
126
- sleep(2) if job_status == "Starting"
127
- job_status_response = @client.request(:ping_report_status) do
128
- soap.xml do |xml|
129
- xml.soap(:Envelope, "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema", "xmlns:soap" => "http://schemas.xmlsoap.org/soap/envelope/") {
130
- xml.soap(:Body) {
131
- xml.PingReportStatus("xmlns" => "http://comscore.com/") {
132
- xml.jobId(job_id)
133
- }
134
- }
135
- }
136
- end
137
- end
138
- job_status = job_status_response.to_hash()[:ping_report_status_response][:ping_report_status_result][:status]
104
+ sleep(DEFAULT_REPORT_WAIT_TIME) if job_status != ""
105
+ job_status_response = self.request(service, :ping_report_status) { |xml| xml.jobId(job_id) }.to_hash
106
+ job_status = job_status_response[:ping_report_status_response][:ping_report_status_result][:status]
139
107
  end while (job_status != "Completed" && job_status != "Failed")
140
108
 
141
- if (job_status == "Completed")
142
- result = @client.request(:fetch_report) do
143
- soap.xml do |xml|
144
- xml.soap(:Envelope, "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema", "xmlns:soap" => "http://schemas.xmlsoap.org/soap/envelope/") {
145
- xml.soap(:Body) {
146
- xml.FetchReport("xmlns" => "http://comscore.com/") {
147
- xml.jobId(job_id)
148
- }
149
- }
150
- }
151
- end
152
- end
153
- return result.to_hash()[:fetch_report_response][:fetch_report_result]
109
+ if job_status == "Completed"
110
+ return self.request(service, :fetch_report) {|xml| xml.jobId(job_id) }
154
111
  else
155
112
  raise "The job with id #{job_id} failed to complete. Last known status was #{job_status}."
156
113
  end
157
114
  end
158
115
 
116
+ def log=(value)
117
+ raise ArgumentError, "Parameter must be of type Boolean." unless !!value == value
118
+ @log = value
119
+ Savon.configure do |config|
120
+ config.log = value
121
+ config.log_level = :info if value == false
122
+ end
123
+ HTTPI.log = value
124
+ end
125
+
126
+ def log
127
+ @log
128
+ end
129
+
159
130
  end
160
131
  end
@@ -37,6 +37,7 @@ module ComScore
37
37
  "All Entries" => "-1"
38
38
  }
39
39
  MEASURES = {
40
+ "Target Audience (000)" => 73,
40
41
  "Total Unique Visitors (000)" => 1,
41
42
  "% Reach" => 9,
42
43
  "% Composition Unique Visitors" => 10,
@@ -1,3 +1,3 @@
1
1
  module ComScore
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.20"
3
3
  end
data/lib/comscore_ruby.rb CHANGED
@@ -18,6 +18,8 @@ require 'active_support/core_ext/date/zones'
18
18
 
19
19
  require 'active_support/core_ext/time/calculations'
20
20
 
21
+ require 'active_support/inflector'
22
+
21
23
  module ComScore
22
24
 
23
25
  end
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+
5
+ require 'comscore_ruby'
6
+
7
+ class ClientTest < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @config = YAML::load(File.open("test/config.yml"))["comscore"]
11
+
12
+ @client = ComScore::Client.new(@config["username"], @config["password"])
13
+ @client.log = false
14
+ end
15
+
16
+ def test_fetch_media
17
+ response = @client.request(:key_measures, :fetch_media) do |xml|
18
+ xml.parameterId("media")
19
+ xml.fetchMediaQuery("xmlns" => "http://comscore.com/FetchMedia") {
20
+ xml.SearchCritera(:ExactMatch => "false", :Critera => "The Globe And Mail")
21
+ }
22
+ xml.reportQuery("xmlns" => "http://comscore.com/ReportQuery") {
23
+ xml.Parameter(:KeyId => "geo", :Value => ComScore::GEOGRAPHIES["Canada"])
24
+ xml.Parameter(:KeyId => "loc", :Value => ComScore::LOCATIONS["Home and Work"])
25
+ xml.Parameter(:KeyId => "timeType", :Value => ComScore::TIME_TYPES["Months"])
26
+ xml.Parameter(:KeyId => "timePeriod", :Value => DateTime.now.to_date.to_comscore_time_period - 2)
27
+ xml.Parameter(:KeyId => "mediaSetType", :Value => ComScore::MEDIA_SET_TYPES["Ranked Categories"])
28
+ }
29
+ end
30
+
31
+ assert_equal("The Globe And Mail", response.to_hash[:fetch_media_response][:fetch_media_result][:media_item][:@name])
32
+ end
33
+
34
+ def test_category_list
35
+ response = @client.request(:key_measures, :discover_parameter_values) do |xml|
36
+ xml.parameterId("mediaSet")
37
+ xml.query(:xmlns => "http://comscore.com/ReportQuery") {
38
+ xml.Parameter(:KeyId => "geo", :Value => ComScore::GEOGRAPHIES["Canada"])
39
+ xml.Parameter(:KeyId => "timeType", :Value => ComScore::TIME_TYPES["Months"])
40
+ xml.Parameter(:KeyId => "timePeriod", :Value => DateTime.now.to_date.to_comscore_time_period - 2)
41
+ xml.Parameter(:KeyId => "mediaSetType", :Value => ComScore::MEDIA_SET_TYPES["Ranked Categories"])
42
+ }
43
+ end
44
+
45
+ assert_instance_of(Array, response.to_hash[:discover_parameter_values_response][:discover_parameter_values_result][:enum_value])
46
+ end
47
+
48
+ def test_key_measures
49
+ response = @client.get_report(:key_measures,
50
+ :geo => ComScore::GEOGRAPHIES["Canada"],
51
+ :loc => ComScore::LOCATIONS["Home and Work"],
52
+ :timeType => ComScore::TIME_TYPES["Months"],
53
+ :timePeriod => Date.new(2011, 8),
54
+ :mediaSet => 778226,
55
+ :mediaSetType => ComScore::MEDIA_SET_TYPES["Ranked Categories"],
56
+ :targetType => ComScore::TARGET_TYPES["Simple"],
57
+ :targetGroup => ComScore::TARGET_GROUPS["Total Audience"],
58
+ :measure => [ComScore::MEASURES["Total Unique Visitors (000)"]]
59
+ )
60
+
61
+ assert_instance_of(Array, response.to_hash[:fetch_report_response][:fetch_report_result][:report][:table][:tbody][:tr])
62
+ end
63
+
64
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comscore_ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 55
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 13
10
- version: 0.0.13
9
+ - 20
10
+ version: 0.0.20
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Sukmanowsky
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-02 00:00:00 Z
18
+ date: 2011-09-16 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: savon
@@ -43,10 +43,8 @@ dependencies:
43
43
  - !ruby/object:Gem::Version
44
44
  hash: 3
45
45
  segments:
46
- - 3
47
- - 1
48
46
  - 0
49
- version: 3.1.0
47
+ version: "0"
50
48
  type: :runtime
51
49
  version_requirements: *id002
52
50
  description: Basic support for things like finding media, media sets (AKA categories), and fetching reports. Documentation is...well...missing so far.
@@ -68,6 +66,7 @@ files:
68
66
  - lib/comscore_ruby/constants.rb
69
67
  - lib/comscore_ruby/core_ext/date.rb
70
68
  - lib/comscore_ruby/version.rb
69
+ - test/client_test.rb
71
70
  homepage: https://github.com/msukmanowsky/comscore_ruby/wiki
72
71
  licenses: []
73
72
 
@@ -101,5 +100,5 @@ rubygems_version: 1.8.4
101
100
  signing_key:
102
101
  specification_version: 3
103
102
  summary: Use comScore's SOAP API in a manner that does not require you to bang your head against a wall.
104
- test_files: []
105
-
103
+ test_files:
104
+ - test/client_test.rb