keyword_ranking 1.0.1 → 1.0.2.1

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/Gemfile CHANGED
@@ -5,5 +5,5 @@ gemspec
5
5
  group 'development' do
6
6
  gem 'echoe'
7
7
  gem 'rspec'
8
- gem 'nokogiri'
8
+ gem 'nokogiri' , ">= 1.4.3.1"
9
9
  end
data/README CHANGED
@@ -1,8 +1,8 @@
1
1
  Keyword Ranking
2
2
 
3
- Use this gem to calculate the keyword ranking in Bing, Yahoo or Google as follows:
3
+ Use this gem to calculate the keyword ranking for Bing, Yahoo or Google as follows:
4
4
 
5
- KeywordRanking::Ranking.instance.get(:keyword => 'one_keyword', :url => 'www.mydomain.com', :engine => :bing, :limit => 100)
5
+ KeywordRanking::Ranking.get(:keyword => 'one_keyword', :url => 'www.mydomain.com', :engine => :bing, :limit => 100)
6
6
 
7
7
  Note:
8
8
 
@@ -15,5 +15,4 @@ More updates coming
15
15
  TODO:
16
16
 
17
17
  * Testing code
18
- * GEM for Rails structure
19
18
 
@@ -2,16 +2,16 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{keyword_ranking}
5
- s.version = "1.0.1"
5
+ s.version = "1.0.2.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Luis Alberto Velasco"]
9
9
  s.date = %q{2010-08-17}
10
- s.description = %q{Create keyword rankings from the 3 major browsers}
10
+ s.description = %q{Create keyword rankings from the 3 major search engines}
11
11
  s.email = %q{whizkas@hotmail.com}
12
12
  s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib/keyword_ranking.rb"]
13
13
  s.files = ["CHANGELOG", "Gemfile", "LICENSE", "README", "Rakefile", "lib/keyword_ranking.rb", "spec/keyword_ranking_spec.rb", "spec/spec.opts", "keyword_ranking.gemspec"]
14
- s.homepage = %q{http://github.com/whizkas/keyword_ranking}
14
+ s.homepage = %q{http://github.com/crowdint/keyword_ranking}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Keyword_ranking", "--main", "README"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{keyword_ranking}
@@ -1,5 +1,5 @@
1
- require 'singleton'
2
1
  require 'nokogiri'
2
+ require 'net/http'
3
3
 
4
4
  class Array
5
5
  def extract_options!
@@ -8,32 +8,38 @@ class Array
8
8
  end
9
9
 
10
10
  module KeywordRanking
11
- class Ranking
12
11
 
13
- RES_LIMIT = 200
14
- RES_PER_PAGE = 10 #careful, it doesn't work with more than 10 for all search engines!
12
+ RES_LIMIT = 200
13
+ RES_PER_PAGE = 10 #careful, it doesn't work with more than 10 for all search engines!
14
+
15
+ class Ranking
15
16
 
16
- include Singleton
17
+ def initialize(finder = Finder.new)
18
+ @finder = finder
19
+ end
17
20
 
18
21
  def get(*args) # keyword, url, engine, limit
19
22
  options = args.extract_options!
20
23
  limit = options[:limit] ? options[:limit].to_i : RES_LIMIT
21
24
  site_uri = options[:url].gsub(/(http:\/\/|https:\/\/)/, '').gsub(/^www/, '')
22
25
  validate_arguments options
23
- find_ranking(options[:keyword], site_uri, limit, options[:engine])
26
+ @finder.find(options[:keyword], site_uri, limit, options[:engine])
24
27
  end
25
28
 
26
29
  protected
27
-
28
30
  def validate_arguments(options)
29
31
  raise "Keyword and site parameters must be Strings" unless options[:keyword].is_a?(String) and options[:url].is_a?(String)
30
32
  raise "Limit of #{RES_LIMIT} results at most" if options[:limit] > RES_LIMIT
31
33
  raise "Engine should be 'bing', 'google' or 'yahoo'" unless [:bing, :google, :yahoo].include?(options[:engine].to_sym)
32
34
  end
33
35
 
34
- def find_ranking(keyword, site, limit, engine)
36
+ end
37
+
38
+ class Finder
39
+
40
+ def find(keyword, site, limit, engine)
35
41
  keyword.gsub!(/\s/, '+')
36
- request_url, results_container, cite_container = case engine.to_sym
42
+ request_url, results_selector, cite_selector = case engine.to_sym
37
43
  when :bing
38
44
  ["http://www.bing.com/search?q=#{keyword}&count=#{RES_PER_PAGE}&first=", '#wg0 > li', 'cite']
39
45
  when :google
@@ -41,21 +47,27 @@ module KeywordRanking
41
47
  when :yahoo
42
48
  ["http://search.yahoo.com/search?p=#{keyword}&n=#{RES_PER_PAGE}&b=", '#web > ol > li', 'span']
43
49
  end
50
+
44
51
  count, rank = 0, nil
52
+
45
53
  loop {
46
- results = Nokogiri.parse(%x{ curl -A Mozilla "#{request_url}#{count}" }).css(results_container)
47
- rank = results.index(results.detect{ |result| result.css(cite_container).text.match Regexp.new(site) })
48
- if count > RES_LIMIT
54
+ html_response = Net::HTTP.get_response(URI.parse("#{request_url}#{count}")).body
55
+ html_results = Nokogiri.parse(html_response).css(results_selector)
56
+ rank = html_results.index(html_results.detect{ |result| result.css(cite_selector).text.match Regexp.new(site) })
57
+
58
+ if count > limit
49
59
  break
50
- elsif rank
51
- rank = count + rank
60
+ elsif rank
61
+ rank += count
52
62
  break
53
63
  end
54
64
  count += RES_PER_PAGE
55
65
  }
56
- rank.try(:next)
57
- end
66
+
67
+ rank ? rank.next : nil
68
+
69
+ end
58
70
 
59
71
  end
60
- end
61
72
 
73
+ end
@@ -5,16 +5,59 @@ require 'bundler'
5
5
  Bundler.setup(:development)
6
6
  require 'keyword_ranking'
7
7
 
8
+ module FinderSpecHelper
9
+ class Finder
10
+ def find(keyword, site, limit, engine)
11
+ 0
12
+ end
13
+ end
14
+ end
15
+
8
16
  module KeywordRanking
9
17
 
10
18
  describe Ranking do
11
19
 
12
- describe "#start" do
13
- it "creates a ranking instance" do
14
- Ranking.instance.should_not be(nil)
20
+ let (:options) { {:keyword => 'agile', :url => 'www.crowdint.com', :engine => 'google', :limit => 20} }
21
+ let(:ranking) { Ranking.new(FinderSpecHelper::Finder.new) }
22
+
23
+ context '#get ranking fails' do
24
+
25
+ it 'without parameters' do
26
+ lambda { ranking.get }.should raise_exception
27
+ end
28
+
29
+ it 'with unsupported engine' do
30
+ lambda { ranking.get(options.merge({:engine => 'mugle'})) }.should raise_exception
31
+ end
32
+
33
+ it 'exceeding results limit' do
34
+ lambda { ranking.get(options.merge({:limit => 201})) }.should raise_exception
35
+ end
36
+
37
+ it 'with bad keyword and url data types' do
38
+ lambda { ranking.get(options.merge({:keyword => 1000, :url => 1.2})) }.should raise_exception
15
39
  end
40
+
41
+ it 'using malformed URLs'
42
+
16
43
  end
17
44
 
45
+ context '#get ranking success' do
46
+
47
+ it 'with correct parameters on google' do
48
+ ranking.get(options).should equal 0
49
+ end
50
+
51
+ it 'with correct parameters on yahoo'
52
+
53
+ it 'with correct parameters on bing'
54
+
55
+ it 'should support multiple keywords'
56
+
57
+ it 'should support a symbol as an engine name'
58
+
59
+ end
60
+
18
61
  end
19
62
 
20
63
  end
data/spec/spec.opts CHANGED
@@ -2,4 +2,3 @@
2
2
  --format progress
3
3
  --loadby mtime
4
4
  --reverse
5
- --drb
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keyword_ranking
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 85
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
+ - 2
9
10
  - 1
10
- version: 1.0.1
11
+ version: 1.0.2.1
11
12
  platform: ruby
12
13
  authors:
13
14
  - Luis Alberto Velasco
@@ -51,7 +52,7 @@ dependencies:
51
52
  version: 0.9.26
52
53
  type: :development
53
54
  version_requirements: *id002
54
- description: Create keyword rankings from the 3 major browsers
55
+ description: Create keyword rankings from the 3 major search engines
55
56
  email: whizkas@hotmail.com
56
57
  executables: []
57
58
 
@@ -73,7 +74,7 @@ files:
73
74
  - spec/spec.opts
74
75
  - keyword_ranking.gemspec
75
76
  has_rdoc: true
76
- homepage: http://github.com/whizkas/keyword_ranking
77
+ homepage: http://github.com/crowdint/keyword_ranking
77
78
  licenses: []
78
79
 
79
80
  post_install_message: