compete 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Simplificator GmbH
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,39 @@
1
+ = compete
2
+
3
+ A wrapper for compete.com API.
4
+ Can be used to look up information (trust level, rankings, visitors) for a domain.
5
+
6
+
7
+
8
+ = Requirements
9
+ You'll need an account at http://developer.compete.com/
10
+ Depending on number of requests you make you'll need to pay.
11
+ We (Simplificator) have no connection with compete.com!
12
+
13
+
14
+ = Usage
15
+ Sign up at http://developer.compete.com/ and get an API key
16
+ Install the simplificator-compete gem.
17
+
18
+ Look at compete.rb to see available information (i.e. metrics_ranking, metrics_link, ...)
19
+
20
+
21
+
22
+ ----
23
+ require 'rubygems'
24
+ require 'simplificator-compete'
25
+ COMPETE_API_KEY = 'your api key'
26
+
27
+ info = Compete.for_domain('simplificator.com')
28
+ if info.data_available?
29
+ puts "Ranking is: #{info.metrics_ranking}"
30
+ else
31
+ puts "no data"
32
+ end
33
+ ----
34
+
35
+
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2009 Simplificator GmbH. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "compete"
8
+ gem.summary = %Q{API for http://developer.compete.com/}
9
+ gem.email = "info@simplificator.com"
10
+ gem.homepage = "http://github.com/simplificator/compete"
11
+ gem.authors = ["simplificator"]
12
+ gem.add_dependency('httparty', '>= 0.4.3')
13
+
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION.yml')
46
+ config = YAML.load(File.read('VERSION.yml'))
47
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "compete #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
57
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 2
4
+ :patch: 0
data/lib/compete.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'httparty'
2
+ class Compete
3
+ include HTTParty
4
+ format :xml
5
+ # Valid size arguments for the for_domain call
6
+ VALID_SIZES = ['small', 'large']
7
+ API_URL = 'http://api.compete.com/fast-cgi/MI'
8
+ API_VERSION = 3
9
+
10
+ # readers for trust data
11
+ attr_reader(:trust_level_value, :trust_level_link, :trust_level_icon)
12
+ # readers for metric data
13
+ attr_reader(:metrics_link, :metrics_icon, :metrics_count, :metrics_ranking, :metrics_date_range)
14
+ # other readers
15
+ attr_reader(:domain, :xml)
16
+
17
+ #
18
+ # Creates a new Compete Object from the XML response of the compete API (as parsed by httparty).
19
+ # Details see http://developer.compete.com/
20
+ #
21
+ def initialize(xml)
22
+ @xml = xml
23
+ @domain = @xml['ci']['dmn']['nm']
24
+
25
+ # metrics
26
+ @metrics_ranking = to_integer(@xml['ci']['dmn']['metrics']['val']['uv']['ranking'])
27
+ if data_available?
28
+ @metrics_date_range = build_metrics_date_range()
29
+ end
30
+
31
+ # metrics
32
+ @metrics_count = to_integer(@xml['ci']['dmn']['metrics']['val']['uv']['count'])
33
+ @metrics_link = @xml['ci']['dmn']['metrics']['link']
34
+ @metrics_icon = @xml['ci']['dmn']['metrics']['icon']
35
+
36
+ # trust
37
+ @trust_level_value = @xml['ci']['dmn']['trust']['val']
38
+ @trust_level_link = @xml['ci']['dmn']['trust']['link']
39
+ @trust_level_icon = @xml['ci']['dmn']['trust']['icon']
40
+
41
+ end
42
+
43
+ # Lookup the compete info for a domain.
44
+ # the size argument is for the icon urls. VALID_SIZES specifies what you can give here.
45
+ # Make sure COMPETE_API_KEY is set before you call this method.
46
+ def self.for_domain(domain, size = 'small')
47
+ raise 'COMPETE_API_KEY is not defined' unless defined?(COMPETE_API_KEY)
48
+ raise "Unknown size '#{size}'" unless VALID_SIZES.include?(size)
49
+ info = get(API_URL, :query => {:d => domain, :ver => API_VERSION, :apikey => COMPETE_API_KEY, :size => size})
50
+ Compete.new(info)
51
+ end
52
+
53
+ # Is data available for this domain?
54
+ # If this method returns true
55
+ def data_available?
56
+ !!self.metrics_ranking
57
+ end
58
+
59
+ private
60
+
61
+ # Build a date range according to the year/month specified in the XML
62
+ # Assuming month is 0 indexed (rubys month is 1 indexed) but there is nothing in the deco about it...
63
+ def build_metrics_date_range()
64
+ month = to_integer(@xml['ci']['dmn']['metrics']['val']['mth'])
65
+ year = to_integer(@xml['ci']['dmn']['metrics']['val']['yr'])
66
+ from = Date.new(year, month + 1)
67
+ to = (from >> 1) - 1
68
+ (from..to)
69
+ end
70
+
71
+ # Metrics count contains ',' as separator...
72
+ def to_integer(value)
73
+ strip(value).gsub(',', '').to_i if value
74
+ end
75
+
76
+ # Strip all whitespace
77
+ def strip(value)
78
+ value.gsub(/\s/, '') if value
79
+ end
80
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class CompeteTest < Test::Unit::TestCase
4
+ should 'deal with unknown domain' do
5
+ file = File.dirname(__FILE__) + '/data/unknown.yml'
6
+ info = YAML::load_file(file)
7
+ compete = Compete.new(info)
8
+ assert(!compete.data_available?)
9
+ assert_equal('yellow', compete.trust_level_value)
10
+ assert(compete.trust_level_link)
11
+ assert(compete.trust_level_icon)
12
+ assert(!compete.metrics_link)
13
+ assert(compete.metrics_icon)
14
+ assert(!compete.metrics_ranking)
15
+ assert(!compete.metrics_date_range)
16
+ assert(!compete.metrics_count)
17
+ assert_equal('unknowndomain.something.notknow', compete.domain)
18
+ end
19
+
20
+ should 'find infos about known domain' do
21
+ file = File.dirname(__FILE__) + '/data/google.com.yml'
22
+ info = YAML::load_file(file)
23
+ compete = Compete.new(info)
24
+ assert(compete.data_available?)
25
+ assert_equal('green', compete.trust_level_value)
26
+ assert(compete.trust_level_link)
27
+ assert(compete.trust_level_icon)
28
+ assert(compete.metrics_link)
29
+ assert(compete.metrics_icon)
30
+ assert_equal(1, compete.metrics_ranking)
31
+ assert_equal(137630925, compete.metrics_count)
32
+ assert_equal((Date.new(2009, 4, 1)..Date.new(2009, 4, 30)), compete.metrics_date_range)
33
+ assert_equal('google.com', compete.domain)
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ ---
2
+ ci:
3
+ dmn:
4
+ trust:
5
+ val: green
6
+ icon: http://home.compete.com.edgesuite.net/site_media/images/icons/trust_green_16.gif
7
+ caption: Trust
8
+ link: http://toolbar.compete.com/trustgreen/google.com
9
+ rank:
10
+ val: "1"
11
+ icon: http://home.compete.com.edgesuite.net/site_media/images/icons/profile_3_16.gif
12
+ caption: Profile
13
+ link: http://toolbar.compete.com/siteprofile/google.com
14
+ metrics:
15
+ val:
16
+ mth: "\n 03\n "
17
+ uv:
18
+ ranking: "\n 1\n "
19
+ count: "\n 137,630,925\n "
20
+ yr: "\n 2009\n "
21
+ icon: http://home.compete.com.edgesuite.net/site_media/images/icons/profile_3_16.gif
22
+ caption: Profile
23
+ link: http://toolbar.compete.com/siteprofile/google.com
24
+ deals:
25
+ val: "0"
26
+ icon: http://home.compete.com.edgesuite.net/site_media/images/icons/deals_off_16.gif
27
+ caption: Deals
28
+ link:
29
+ nm: google.com
@@ -0,0 +1,29 @@
1
+ ---
2
+ ci:
3
+ dmn:
4
+ trust:
5
+ val: yellow
6
+ icon: http://home.compete.com.edgesuite.net/site_media/images/icons/trust_yellow_16.gif
7
+ caption: Trust
8
+ link: http://toolbar.compete.com/trustyellow/unknowndomain.something.notknow
9
+ rank:
10
+ val:
11
+ icon: http://home.compete.com.edgesuite.net/site_media/images/icons/profile_0_16.gif
12
+ caption: Profile
13
+ link:
14
+ metrics:
15
+ val:
16
+ mth:
17
+ uv:
18
+ ranking:
19
+ count:
20
+ yr:
21
+ icon: http://home.compete.com.edgesuite.net/site_media/images/icons/profile_0_16.gif
22
+ caption: Profile
23
+ link:
24
+ deals:
25
+ val: "0"
26
+ icon: http://home.compete.com.edgesuite.net/site_media/images/icons/deals_off_16.gif
27
+ caption: Deals
28
+ link:
29
+ nm: unknowndomain.something.notknow
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'compete'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compete
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: ruby
7
+ authors:
8
+ - simplificator
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-05-03 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.4.3
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description:
27
+ email: info@simplificator.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.rdoc
35
+ files:
36
+ - LICENSE
37
+ - README.rdoc
38
+ - Rakefile
39
+ - VERSION.yml
40
+ - lib/compete.rb
41
+ - test/compete_test.rb
42
+ - test/data/google.com.yml
43
+ - test/data/unknown.yml
44
+ - test/test_helper.rb
45
+ homepage: http://github.com/simplificator/compete
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.7.2
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: API for http://developer.compete.com/
72
+ test_files:
73
+ - test/compete_test.rb
74
+ - test/test_helper.rb
75
+ has_rdoc: