uk_ranking 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+
9
+ source :gemcutter
10
+
11
+ gem 'nokogiri'
12
+
13
+
14
+ group :development do
15
+ gem "rspec", "~> 2.3.0"
16
+ gem "bundler", "~> 1.0.0"
17
+ gem "jeweler", "~> 1.6.4"
18
+ gem "rcov", ">= 0"
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ remote: http://rubygems.org/
4
+ specs:
5
+ diff-lcs (1.1.3)
6
+ git (1.2.5)
7
+ jeweler (1.6.4)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ nokogiri (1.5.0)
12
+ rake (0.9.2.2)
13
+ rcov (0.9.11)
14
+ rspec (2.3.0)
15
+ rspec-core (~> 2.3.0)
16
+ rspec-expectations (~> 2.3.0)
17
+ rspec-mocks (~> 2.3.0)
18
+ rspec-core (2.3.1)
19
+ rspec-expectations (2.3.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.3.0)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ bundler (~> 1.0.0)
28
+ jeweler (~> 1.6.4)
29
+ nokogiri
30
+ rcov
31
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Stewart Dick
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,19 @@
1
+ = uk_ranking
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to uk_ranking
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Stewart Dick. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "uk_ranking"
18
+ gem.homepage = "http://github.com/stewd/uk_ranking"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Get Keyword Ranking for Google UK}
21
+ gem.description = %Q{A modification of keyword_ranking. To pull the Google UK Rank for a given keyword}
22
+ gem.email = "stewart@stewd.co.uk"
23
+ gem.authors = ["Stewart Dick"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "uk_ranking #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/uk_ranking.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+ require 'net/http'
4
+
5
+ class Array
6
+ def extract_options!
7
+ last.is_a?(::Hash) ? pop : {}
8
+ end
9
+ end
10
+
11
+ class Ranking
12
+ @@default_options = { :limit => 200, :res_per_page => 10, :supported_engines => [:bing, :google, :yahoo] } #careful, res_per_page doesn't work with more than 10 for all search engines!
13
+
14
+ attr_reader :options
15
+
16
+ def initialize(*options) # keyword, url, limit
17
+ self.options = options.extract_options!
18
+ end
19
+
20
+ def options=(options)
21
+ @options = @@default_options.merge(options)
22
+ @options[:url].gsub!(/(http:\/\/|https:\/\/)/, '')
23
+ @options[:url].gsub!(/^www/, '')
24
+ end
25
+
26
+ def from
27
+ validate_options
28
+ @finder ||= Finder.new
29
+ @finder.find(@options)
30
+ end
31
+
32
+ def method_missing(method, *args, &block)
33
+ engine = @@default_options[:supported_engines].find{ |name| ("from_" << name.to_s).match /#{method}/ }
34
+ if engine
35
+ @options.merge!({:engine => engine})
36
+ send("from")
37
+ else
38
+ super(method, *args, &block)
39
+ end
40
+ end
41
+
42
+ protected
43
+ def validate_options
44
+ raise "Keyword and site parameters must be Strings" unless @options[:keyword].is_a?(String) and @options[:url].is_a?(String)
45
+ raise "Engine should be 'bing', 'google' or 'yahoo'" unless @@default_options[:supported_engines].include?(@options[:engine].to_sym)
46
+ end
47
+
48
+ end
49
+
50
+ class Finder
51
+
52
+ def find(options) #keyword, url, limit, engine, res_per_page
53
+ options[:keyword].gsub!(/\s/, '+')
54
+ request_url, results_selector, cite_selector = case options[:engine].to_sym
55
+ when :bing
56
+ ["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&first=", '#wg0 > li', 'cite']
57
+ when :google
58
+ ["http://www.google.co.uk/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
59
+ when :yahoo
60
+ ["http://search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
61
+ end
62
+
63
+ count, rank = 0, nil
64
+
65
+ loop {
66
+ html_response = Net::HTTP.get_response(URI.parse("#{request_url}#{count}")).body
67
+ html_results = Nokogiri.parse(html_response).css(results_selector)
68
+ rank = html_results.index(html_results.detect{ |result| result.css(cite_selector).text.match Regexp.new(options[:url]) })
69
+
70
+ if count > options[:limit]
71
+ break
72
+ elsif rank
73
+ rank += count
74
+ break
75
+ end
76
+ count += options[:res_per_page]
77
+ }
78
+
79
+ rank ? rank.next : nil
80
+
81
+ end
82
+
83
+ end
84
+
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'uk_ranking'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,72 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ # describe "KeywordRanking" do
4
+ # it "fails" do
5
+ # fail "hey buddy, you should probably rename this file and start specing for real"
6
+ # end
7
+ # end
8
+ #
9
+ #
10
+ # # $LOAD_PATH << File.expand_path('../../lib' , __FILE__)
11
+ # #
12
+ # # require 'rubygems'
13
+ # # require 'bundler'
14
+ # # Bundler.setup(:development)
15
+ # # require 'keyword_ranking'
16
+
17
+ module FinderSpecHelper
18
+ class Finder
19
+ def find(keyword, site, limit, engine)
20
+ 0
21
+ end
22
+ end
23
+ end
24
+
25
+ module KeywordRanking
26
+
27
+ describe Ranking do
28
+
29
+ let (:options) { {:keyword => 'agile', :url => 'www.crowdint.com', :engine => 'google', :limit => 20} }
30
+ let(:ranking) { Ranking.new(FinderSpecHelper::Finder.new) }
31
+
32
+ context '#get ranking fails' do
33
+
34
+ it 'without parameters' do
35
+ lambda { ranking.get }.should raise_exception
36
+ end
37
+
38
+ it 'with unsupported engine' do
39
+ lambda { ranking.get(options.merge({:engine => 'mugle'})) }.should raise_exception
40
+ end
41
+
42
+ it 'exceeding results limit' do
43
+ lambda { ranking.get(options.merge({:limit => 201})) }.should raise_exception
44
+ end
45
+
46
+ it 'with bad keyword and url data types' do
47
+ lambda { ranking.get(options.merge({:keyword => 1000, :url => 1.2})) }.should raise_exception
48
+ end
49
+
50
+ it 'using malformed URLs'
51
+
52
+ end
53
+
54
+ context '#get ranking success' do
55
+
56
+ it 'with correct parameters on google' do
57
+ ranking.get(options).should equal 0
58
+ end
59
+
60
+ it 'with correct parameters on yahoo'
61
+
62
+ it 'with correct parameters on bing'
63
+
64
+ it 'should support multiple keywords'
65
+
66
+ it 'should support a symbol as an engine name'
67
+
68
+ end
69
+
70
+ end
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uk_ranking
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stewart Dick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-14 00:00:00.000000000 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ requirement: &2157628860 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2157628860
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &2157628320 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2157628320
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &2157627740 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2157627740
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &2157627080 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.4
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *2157627080
59
+ - !ruby/object:Gem::Dependency
60
+ name: rcov
61
+ requirement: &2157626480 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *2157626480
70
+ description: A modification of keyword_ranking. To pull the Google UK Rank for a given
71
+ keyword
72
+ email: stewart@stewd.co.uk
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files:
76
+ - LICENSE.txt
77
+ - README.rdoc
78
+ files:
79
+ - .document
80
+ - .rspec
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.rdoc
85
+ - Rakefile
86
+ - VERSION
87
+ - lib/uk_ranking.rb
88
+ - spec/spec_helper.rb
89
+ - spec/uk_ranking_spec.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/stewd/uk_ranking
92
+ licenses:
93
+ - MIT
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.6.2
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Get Keyword Ranking for Google UK
116
+ test_files: []