keyword_ranking 1.0.2.1 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,19 @@
1
+ #Keyword Ranking
2
+
3
+ Use this gem to calculate the keyword ranking for *Bing*, *Yahoo* or *Google* as follows:
4
+
5
+ KeywordRanking::Ranking.new(:keyword => 'one keyword', :url => 'www.mydomain.com', :limit => 100).from_google
6
+
7
+ ##Note
8
+
9
+ * Only *:keyword* and *:url* are required
10
+ * You can search --> *.from_google*, *.from_yahoo*, *.from_bing*
11
+ * *:limit* by default is 200
12
+ * You can change the results per page value changing *:res_per_page* having in mind that not all the 3 engines support more than 10 results per page with this utility
13
+
14
+ More updates coming
15
+
16
+ ##TODO
17
+
18
+ * Improve Testing Code
19
+
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{keyword_ranking}
5
- s.version = "1.0.2.1"
5
+ s.version = "1.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
10
  s.description = %q{Create keyword rankings from the 3 major search engines}
11
11
  s.email = %q{whizkas@hotmail.com}
12
- s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib/keyword_ranking.rb"]
13
- s.files = ["CHANGELOG", "Gemfile", "LICENSE", "README", "Rakefile", "lib/keyword_ranking.rb", "spec/keyword_ranking_spec.rb", "spec/spec.opts", "keyword_ranking.gemspec"]
12
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.markdown", "lib/keyword_ranking.rb"]
13
+ s.files = ["CHANGELOG", "Gemfile", "LICENSE", "README.markdown", "Rakefile", "lib/keyword_ranking.rb", "spec/keyword_ranking_spec.rb", "spec/spec.opts", "keyword_ranking.gemspec"]
14
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"]
@@ -9,43 +9,57 @@ end
9
9
 
10
10
  module KeywordRanking
11
11
 
12
- RES_LIMIT = 200
13
- RES_PER_PAGE = 10 #careful, it doesn't work with more than 10 for all search engines!
14
12
 
15
13
  class Ranking
14
+ @@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!
16
15
 
17
- def initialize(finder = Finder.new)
18
- @finder = finder
16
+ attr_reader :options
17
+
18
+ def initialize(*options) # keyword, url, limit
19
+ self.options = options.extract_options!
20
+ end
21
+
22
+ def options=(options)
23
+ @options = @@default_options.merge(options)
24
+ @options[:url].gsub!(/(http:\/\/|https:\/\/)/, '')
25
+ @options[:url].gsub!(/^www/, '')
19
26
  end
20
27
 
21
- def get(*args) # keyword, url, engine, limit
22
- options = args.extract_options!
23
- limit = options[:limit] ? options[:limit].to_i : RES_LIMIT
24
- site_uri = options[:url].gsub(/(http:\/\/|https:\/\/)/, '').gsub(/^www/, '')
25
- validate_arguments options
26
- @finder.find(options[:keyword], site_uri, limit, options[:engine])
28
+ def from
29
+ validate_options
30
+ @finder ||= Finder.new
31
+ @finder.find(@options)
32
+ end
33
+
34
+ def method_missing(method, *args, &block)
35
+ engine = @@default_options[:supported_engines].find{ |name| ("from_" << name.to_s).match /#{method}/ }
36
+ if engine
37
+ @options.merge!({:engine => engine})
38
+ send("from")
39
+ else
40
+ super(method, *args, &block)
41
+ end
27
42
  end
28
43
 
29
44
  protected
30
- def validate_arguments(options)
31
- raise "Keyword and site parameters must be Strings" unless options[:keyword].is_a?(String) and options[:url].is_a?(String)
32
- raise "Limit of #{RES_LIMIT} results at most" if options[:limit] > RES_LIMIT
33
- raise "Engine should be 'bing', 'google' or 'yahoo'" unless [:bing, :google, :yahoo].include?(options[:engine].to_sym)
45
+ def validate_options
46
+ raise "Keyword and site parameters must be Strings" unless @options[:keyword].is_a?(String) and @options[:url].is_a?(String)
47
+ raise "Engine should be 'bing', 'google' or 'yahoo'" unless @@default_options[:supported_engines].include?(@options[:engine].to_sym)
34
48
  end
35
49
 
36
50
  end
37
51
 
38
52
  class Finder
39
53
 
40
- def find(keyword, site, limit, engine)
41
- keyword.gsub!(/\s/, '+')
42
- request_url, results_selector, cite_selector = case engine.to_sym
54
+ def find(options) #keyword, url, limit, engine, res_per_page
55
+ options[:keyword].gsub!(/\s/, '+')
56
+ request_url, results_selector, cite_selector = case options[:engine].to_sym
43
57
  when :bing
44
- ["http://www.bing.com/search?q=#{keyword}&count=#{RES_PER_PAGE}&first=", '#wg0 > li', 'cite']
58
+ ["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&first=", '#wg0 > li', 'cite']
45
59
  when :google
46
- ["http://www.google.com/search?q=#{keyword}&num=#{RES_PER_PAGE}&start=", '#ires > ol > li', 'cite']
60
+ ["http://www.google.com/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
47
61
  when :yahoo
48
- ["http://search.yahoo.com/search?p=#{keyword}&n=#{RES_PER_PAGE}&b=", '#web > ol > li', 'span']
62
+ ["http://search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
49
63
  end
50
64
 
51
65
  count, rank = 0, nil
@@ -53,15 +67,15 @@ module KeywordRanking
53
67
  loop {
54
68
  html_response = Net::HTTP.get_response(URI.parse("#{request_url}#{count}")).body
55
69
  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) })
70
+ rank = html_results.index(html_results.detect{ |result| result.css(cite_selector).text.match Regexp.new(options[:url]) })
57
71
 
58
- if count > limit
72
+ if count > options[:limit]
59
73
  break
60
74
  elsif rank
61
75
  rank += count
62
76
  break
63
77
  end
64
- count += RES_PER_PAGE
78
+ count += options[:res_per_page]
65
79
  }
66
80
 
67
81
  rank ? rank.next : nil
@@ -71,3 +85,4 @@ module KeywordRanking
71
85
  end
72
86
 
73
87
  end
88
+
metadata CHANGED
@@ -1,14 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keyword_ranking
3
3
  version: !ruby/object:Gem::Version
4
- hash: 85
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 0
9
- - 2
10
8
  - 1
11
- version: 1.0.2.1
9
+ version: "1.1"
12
10
  platform: ruby
13
11
  authors:
14
12
  - Luis Alberto Velasco
@@ -61,13 +59,13 @@ extensions: []
61
59
  extra_rdoc_files:
62
60
  - CHANGELOG
63
61
  - LICENSE
64
- - README
62
+ - README.markdown
65
63
  - lib/keyword_ranking.rb
66
64
  files:
67
65
  - CHANGELOG
68
66
  - Gemfile
69
67
  - LICENSE
70
- - README
68
+ - README.markdown
71
69
  - Rakefile
72
70
  - lib/keyword_ranking.rb
73
71
  - spec/keyword_ranking_spec.rb
data/README DELETED
@@ -1,18 +0,0 @@
1
- Keyword Ranking
2
-
3
- Use this gem to calculate the keyword ranking for Bing, Yahoo or Google as follows:
4
-
5
- KeywordRanking::Ranking.get(:keyword => 'one_keyword', :url => 'www.mydomain.com', :engine => :bing, :limit => 100)
6
-
7
- Note:
8
-
9
- * :engine can receive any of these values --> :google, :yahoo or :bing
10
- * :limit by default is 200 (defined in RES_LIMIT constant)
11
- * You can change the results per page desired changing the value of RES_PER_PAGE having in mind that not all the 3 engines support more than 10 results per page with this utility
12
-
13
- More updates coming
14
-
15
- TODO:
16
-
17
- * Testing code
18
-