romniture 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ test/config.yml
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
6
+ .DS_STORE
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in romniture.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,9 @@
1
+ TODO: Write an actual read me file...for now check out the stuff in test
2
+
3
+ To run unit tests properly, add a config.yml in /test with this structure:
4
+
5
+ omniture:
6
+ username:
7
+ shared_secret:
8
+ report_suite_id:
9
+ environment:
data/ROmniture.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "romniture/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "romniture"
6
+ s.version = ROmniture::VERSION
7
+ s.authors = ["Mike Sukmanowsky"]
8
+ s.email = ["mike.sukmanowsky@gmail.com"]
9
+ s.homepage = "http://github.com/msukmanowsky/ROmniture"
10
+ s.summary = "Use Omniture's REST API with ease."
11
+ s.description = "A library that allows access to Omniture's REST API libraries (developer.omniture.com)"
12
+
13
+ s.rubyforge_project = s.name
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency("httpi")
21
+ s.add_runtime_dependency("json")
22
+
23
+ end
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,137 @@
1
+ module ROmniture
2
+
3
+ class Client
4
+
5
+ DEFAULT_REPORT_WAIT_TIME = 5
6
+
7
+ ENVIRONMENTS = {
8
+ :san_jose => "https://api.omniture.com/admin/1.2/rest/",
9
+ :dallas => "https://api2.omniture.com/admin/1.2/rest/",
10
+ :london => "https://api3.omniture.com/admin/1.2/rest/",
11
+ :san_jose_beta => "https://beta-api.omniture.com/admin/1.2/rest/",
12
+ :dallas_beta => "https://beta-api2.omniture.com/admin/1.2/rest/",
13
+ :sandbox => "https://api-sbx1.omniture.com/admin/1.2/rest/"
14
+ }
15
+
16
+ def initialize(options={})
17
+ @username = options[:username]
18
+ @shared_secret = options[:shared_secret]
19
+ @base_uri = options[:environment].is_a?(Symbol) ? ENVIRONMENTS[options[:environment]] : options[:environment].to_s
20
+ HTTPI.log = false
21
+ end
22
+
23
+ def request(method, parameters = {})
24
+ response = send_request(method, parameters)
25
+
26
+ JSON.parse(response.body)
27
+ end
28
+
29
+ def get_report(method, report_description)
30
+ response = send_request(method, report_description)
31
+
32
+ json = JSON.parse response.body
33
+ if json["status"] == "queued"
34
+ log(Logger::INFO, "Report with ID (" + json["reportID"].to_s + ") queued. Now fetching report...")
35
+ return get_queued_report json["reportID"]
36
+ else
37
+ log(Logger::ERROR, "Could not queue report. Omniture returned with error:\n#{response.body}")
38
+ raise "Could not queue report. Omniture returned with error:\n#{response.body}"
39
+ end
40
+ end
41
+
42
+ attr_writer :log
43
+
44
+ def log?
45
+ @log != false
46
+ end
47
+
48
+ def logger
49
+ @logger ||= ::Logger.new(STDOUT)
50
+ end
51
+
52
+ def log_level
53
+ @log_level ||= ::Logger::INFO
54
+ end
55
+
56
+ def log(*args)
57
+ level = args.first.is_a?(Numeric) || args.first.is_a?(Symbol) ? args.shift : log_level
58
+ logger.log(level, args.join(" ")) if log?
59
+ end
60
+
61
+ private
62
+
63
+ def send_request(method, data)
64
+ log(Logger::INFO, "Requesting #{method}...")
65
+ generate_nonce
66
+
67
+ log(Logger::INFO, "Created new nonce: #{@password}")
68
+
69
+ request = HTTPI::Request.new
70
+ request.url = @base_uri + "?method=#{method}"
71
+ request.headers = request_headers
72
+ request.body = data.to_json
73
+
74
+ response = HTTPI.post(request)
75
+
76
+ if response.code >= 400
77
+ log(:error, "Request failed and returned with response code: #{response.code}\n\n#{response.body}")
78
+ raise "Request failed and returned with response code: #{response.code}\n\n#{response.body}"
79
+ end
80
+
81
+ log(Logger::INFO, "Server responded with response code #{response.code}.")
82
+
83
+ response
84
+ end
85
+
86
+ def generate_nonce
87
+ @nonce = Digest::MD5.new.hexdigest(rand().to_s)
88
+ @created = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")
89
+ combined_string = @nonce + @created + @shared_secret
90
+ sha1_string = Digest::SHA1.new.hexdigest(combined_string)
91
+ @password = Base64.encode64(sha1_string).to_s.chomp("\n")
92
+ end
93
+
94
+ def request_headers
95
+ {
96
+ "X-WSSE" => "UsernameToken Username=\"#{@username}\", PasswordDigest=\"#{@password}\", Nonce=\"#{@nonce}\", Created=\"#{@created}\""
97
+ }
98
+ end
99
+
100
+ def get_queued_report(report_id)
101
+ done = false
102
+ error = false
103
+ status = nil
104
+ start_time = Time.now
105
+ end_time = nil
106
+
107
+ begin
108
+ response = send_request("Report.GetStatus", {"reportID" => "#{report_id}"})
109
+ log(Logger::INFO, "Checking on status of report #{report_id}...")
110
+
111
+ json = JSON.parse(response.body)
112
+ status = json["status"]
113
+
114
+ if status == "done"
115
+ done = true
116
+ elsif status == "failed"
117
+ error = true
118
+ end
119
+
120
+ sleep DEFAULT_REPORT_WAIT_TIME if !done && !error
121
+ end while !done && !error
122
+
123
+ if error
124
+ log(:error, "Unable to get data for report #{report_id}. Omniture returned a status of #{status}.")
125
+ raise "Unable to get data for report #{report_id}. Omniture returned a status of #{status}."
126
+ end
127
+
128
+ response = send_request("Report.GetReport", {"reportID" => "#{report_id}"})
129
+
130
+ end_time = Time.now
131
+ log(Logger::INFO, "Report with ID #{report_id} has finished processing in #{((end_time - start_time)*1000).to_i} ms")
132
+
133
+ JSON.parse(response.body)
134
+ end
135
+ end
136
+
137
+ end
@@ -0,0 +1,3 @@
1
+ module ROmniture
2
+ VERSION = "0.0.1"
3
+ end
data/lib/romniture.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "romniture/version"
2
+ require "romniture/client"
3
+
4
+ require "rubygems"
5
+
6
+ require "logger"
7
+ require "httpi"
8
+ require "digest/md5"
9
+ require "digest/sha1"
10
+ require "base64"
11
+ require "json"
12
+
13
+ module ROmniture
14
+
15
+ end
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+
5
+ require 'romniture'
6
+
7
+ class ClientTest < Test::Unit::TestCase
8
+
9
+ def setup
10
+ config = YAML::load(File.open("test/config.yml"))
11
+ @config = config["omniture"]
12
+
13
+ @client = ROmniture::Client.new({
14
+ :username => "#{@config["username"]}",
15
+ :shared_secret => "#{@config["shared_secret"]}",
16
+ :environment => "#{@config["environment"]}"
17
+ })
18
+ end
19
+
20
+ def test_simple_request
21
+ response = @client.request('Company.GetReportSuites')
22
+
23
+ assert_instance_of Hash, response, "Returned object is not a hash."
24
+ assert(response.has_key?("report_suites"), "Returned hash does not contain any report suites.")
25
+ end
26
+
27
+ def test_report_request
28
+ response = @client.get_report "Report.QueueOvertime", {
29
+ "reportDescription" => {
30
+ "reportSuiteID" => "#{@config["report_suite_id"]}",
31
+ "dateFrom" => "2011-01-01",
32
+ "dateTo" => "2011-01-10",
33
+ "metrics" => [{"id" => "pageviews"}]
34
+ }
35
+ }
36
+
37
+ assert_instance_of Hash, response, "Returned object is not a hash."
38
+ assert(response["report"].has_key?("data"), "Returned hash has no data!")
39
+ end
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: romniture
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mike Sukmanowsky
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-12 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httpi
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: json
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: A library that allows access to Omniture's REST API libraries (developer.omniture.com)
49
+ email:
50
+ - mike.sukmanowsky@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - README
61
+ - ROmniture.gemspec
62
+ - Rakefile
63
+ - lib/romniture.rb
64
+ - lib/romniture/client.rb
65
+ - lib/romniture/version.rb
66
+ - test/client_test.rb
67
+ homepage: http://github.com/msukmanowsky/ROmniture
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project: romniture
96
+ rubygems_version: 1.8.4
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Use Omniture's REST API with ease.
100
+ test_files:
101
+ - test/client_test.rb