feed_searcher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in feed_searcher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ryo Nakamura
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # FeedSearcher
2
+ Search RSS feed URLs from the given URL.
3
+
4
+
5
+ ## Installation
6
+ ```
7
+ $ gem install feed_searcher
8
+ ```
9
+
10
+
11
+ ## Usage
12
+ ```ruby
13
+ require "feed_searcher"
14
+ FeedSearcher.search("https://github.com/r7kamura/feed_searcher")
15
+ #=> ["https://github.com/r7kamura/feed_searcher/commits/master.atom"]
16
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "feed_searcher/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "feed_searcher"
8
+ spec.version = FeedSearcher::VERSION
9
+ spec.authors = ["Ryo Nakamura"]
10
+ spec.email = ["r7kamura@gmail.com"]
11
+ spec.description = "FeedSearcher searches RSS feed URLs from the given URL."
12
+ spec.summary = "Search RSS feed URLs from the given URL"
13
+ spec.homepage = "https://github.com/r7kamura/feed_searcher"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "mechanize", ">= 1.0.0"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", ">= 2.13.0"
25
+ spec.add_development_dependency "webmock"
26
+ end
@@ -0,0 +1,38 @@
1
+ require "feed_searcher/version"
2
+ require "feed_searcher/fetcher"
3
+ require "feed_searcher/page"
4
+ require "mechanize"
5
+
6
+ class FeedSearcher
7
+ Error = Class.new(StandardError)
8
+ InvalidResponseError = Class.new(Error)
9
+
10
+ def self.search(*args)
11
+ new(*args).search
12
+ end
13
+
14
+ attr_reader :options, :url
15
+
16
+ def initialize(url, options = {})
17
+ @url = url
18
+ @options = options
19
+ end
20
+
21
+ def search
22
+ if page.html?
23
+ page.feed_urls
24
+ else
25
+ raise InvalidResponseError
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def fetch
32
+ Fetcher.fetch(url, options)
33
+ end
34
+
35
+ def page
36
+ @page ||= fetch
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ class FeedSearcher
2
+ class Fetcher
3
+ def self.fetch(*args)
4
+ new(*args).fetch
5
+ end
6
+
7
+ attr_reader :options, :url
8
+
9
+ def initialize(url, options = {})
10
+ @url = url
11
+ @options = options
12
+ end
13
+
14
+ def fetch
15
+ Page.new(get)
16
+ end
17
+
18
+ private
19
+
20
+ def get
21
+ agent.get(url)
22
+ end
23
+
24
+ def agent
25
+ Mechanize.new.tap do |mechanize|
26
+ mechanize.open_timeout = options[:open_timeout] if options[:open_timeout]
27
+ mechanize.read_timeout = options[:read_timeout] if options[:read_timeout]
28
+ mechanize.user_agent = options[:user_agent] if options[:user_agent]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ class FeedSearcher
2
+ class Page
3
+ attr_reader :page
4
+
5
+ def initialize(page)
6
+ @page = page
7
+ end
8
+
9
+ def html?
10
+ page.is_a?(Mechanize::Page)
11
+ end
12
+
13
+ def feed_urls
14
+ feed_attributes.map {|attribute| attribute["href"] }
15
+ end
16
+
17
+ private
18
+
19
+ def feed_attributes
20
+ root.xpath("//link[@type='application/rss+xml' or @type='application/atom+xml']")
21
+ end
22
+
23
+ def root
24
+ page.root
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ class FeedSearcher
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe FeedSearcher do
4
+ describe ".search" do
5
+ let(:body) do
6
+ <<-EOF
7
+ <!DOCTYPE HTML>
8
+ <html>
9
+ <head>
10
+ <meta charset="UTF-8">
11
+ <link href="http://example.com/rss.atom" rel="alternate" title="example" type="application/atom+xml" />
12
+ <link href="http://example.com/rss.xml" rel="alternate" title="example" type="application/atom+xml" />
13
+ </head>
14
+ <body>
15
+ example
16
+ </body>
17
+ </html>
18
+ EOF
19
+ end
20
+
21
+ context "when response type is html" do
22
+ before do
23
+ stub_request(:get, "http://example.com/").to_return(
24
+ :headers => { "Content-Type" => "text/html" },
25
+ :body => body
26
+ )
27
+ end
28
+
29
+ it "returns feed URLs from given URL" do
30
+ FeedSearcher.search("http://example.com/").should == %w[
31
+ http://example.com/rss.atom
32
+ http://example.com/rss.xml
33
+ ]
34
+ end
35
+ end
36
+
37
+ context "when response type is not html" do
38
+ before do
39
+ stub_request(:get, "http://example.com/").to_return(:body => body)
40
+ end
41
+
42
+ it "raises FeedSearher::InvalidResponseError" do
43
+ expect { FeedSearcher.search("http://example.com/") }.
44
+ to raise_error(FeedSearcher::InvalidResponseError)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib/feed_searcher", __FILE__)
2
+ require "feed_searcher"
3
+ require "webmock/rspec"
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: feed_searcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryo Nakamura
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.13.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.13.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: webmock
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: FeedSearcher searches RSS feed URLs from the given URL.
95
+ email:
96
+ - r7kamura@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - feed_searcher.gemspec
107
+ - lib/feed_searcher.rb
108
+ - lib/feed_searcher/fetcher.rb
109
+ - lib/feed_searcher/page.rb
110
+ - lib/feed_searcher/version.rb
111
+ - spec/feed_searcher_spec.rb
112
+ - spec/spec_helper.rb
113
+ homepage: https://github.com/r7kamura/feed_searcher
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.23
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Search RSS feed URLs from the given URL
138
+ test_files:
139
+ - spec/feed_searcher_spec.rb
140
+ - spec/spec_helper.rb
141
+ has_rdoc: