simplificator-compete 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 pascalbetz
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,7 @@
1
+ = compete
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 pascalbetz. 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: 1
4
+ :patch: 0
data/lib/compete.rb ADDED
@@ -0,0 +1,69 @@
1
+ class Compete
2
+ include HTTParty
3
+ format :xml
4
+ # Valid size arguments for the for_domain call
5
+ VALID_SIZES = ['small', 'large']
6
+ API_URL = 'http://api.compete.com/fast-cgi/MI'
7
+ API_VERSION = 3
8
+
9
+ # readers for trust data
10
+ attr_reader(:trust_level_value, :trust_level_link, :trust_level_icon)
11
+ # readers for metric data
12
+ attr_reader(:metrics_link, :metrics_icon, :metrics_count, :metrics_ranking, :metrics_date_range)
13
+ # other readers
14
+ attr_reader(:domain, :xml)
15
+
16
+ #
17
+ # Creates a new Compete Object from XML response of the compete API.
18
+ # Details see http://developer.compete.com/
19
+ #
20
+ def initialize(xml)
21
+ @xml = xml
22
+ @domain = @xml['ci']['dmn']['nm']
23
+
24
+ # trust
25
+ @trust_level_value = @xml['ci']['dmn']['trust']['val']
26
+ @trust_level_link = @xml['ci']['dmn']['trust']['link']
27
+ @trust_level_icon = @xml['ci']['dmn']['trust']['icon']
28
+
29
+ # metrics
30
+ @metrics_link = @xml['ci']['dmn']['metrics']['link']
31
+ @metrics_icon = @xml['ci']['dmn']['metrics']['icon']
32
+ @metrics_ranking = to_integer(@xml['ci']['dmn']['metrics']['val']['uv']['ranking'])
33
+ @metrics_count = to_integer(@xml['ci']['dmn']['metrics']['val']['uv']['count'])
34
+ @metrics_date_range = build_metrics_date_range()
35
+ end
36
+
37
+ # Lookup the compete info for a domain.
38
+ # the size argument is for the icon urls. VALID_SIZES specifies what you can give here.
39
+ # Make sure COMPETE_API_KEY is set before you call this method.
40
+ def self.for_domain(domain, size = 'small')
41
+ raise 'COMPETE_API_KEY is not defined' unless defined?(COMPETE_API_KEY)
42
+ raise "Unknown size '#{size}'" unless VALID_SIZES.include?(size)
43
+ info = get(API_URL, :query => {:d => domain, :ver => API_VERSION, :apikey => COMPETE_API_KEY, :size => size})
44
+ Compete.new(info)
45
+ end
46
+
47
+ private
48
+
49
+ # Build a date range according to the year/month specified in the XML
50
+ # Assuming month is 0 indexed (rubys month is 1 indexed) but there is nothing in the deco about it...
51
+ def build_metrics_date_range()
52
+ month = to_integer(@xml['ci']['dmn']['metrics']['val']['mth'])
53
+ year = to_integer(@xml['ci']['dmn']['metrics']['val']['yr'])
54
+ from = Date.new(year, month + 1)
55
+ to = (from >> 1) - 1
56
+ (from..to)
57
+ end
58
+
59
+ # Metrics count contains ',' as separator...
60
+ def to_integer(value)
61
+ strip(value).gsub(',', '').to_i if value
62
+ end
63
+
64
+ # Strip all whitespace
65
+ def strip(value)
66
+ value.gsub(/\s/, '') if value
67
+ end
68
+
69
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class CompeteTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -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,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplificator-compete
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - simplificator
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.4.3
24
+ version:
25
+ description:
26
+ email: info@simplificator.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - LICENSE
36
+ - README.rdoc
37
+ - Rakefile
38
+ - VERSION.yml
39
+ - lib/compete.rb
40
+ - test/compete_test.rb
41
+ - test/test_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/simplificator/compete
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: API for http://developer.compete.com/
68
+ test_files:
69
+ - test/compete_test.rb
70
+ - test/test_helper.rb