hyperfeed 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,18 @@
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
18
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hyperfeed.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Guilherme Kato
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,64 @@
1
+ # Hyperfeed
2
+
3
+ An adapter to plug feeds content on hypermedia engines
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hyperfeed'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hyperfeed
18
+
19
+ ## Usage
20
+
21
+
22
+ ``` ruby
23
+ #For content lists:
24
+
25
+ response = Hyperfeed::Client.at("http://contigo.abril.com.br/noticias.rss").get
26
+ puts response.hyperfeed.inspect
27
+
28
+ {:itens_por_pagina=>10,
29
+ :pagina_atual=>1,
30
+ :paginas_totais=>1,
31
+ :total_resultado=>10,
32
+ :resultado=>[{:id=>"http://contigo.abril.com.br/noticias.rss?resource_id=0",
33
+ :data=>"2013-04-04 12:38:00 -0300",
34
+ :titulo=>"Sabrina Sato, Rodrigo Faro e Thiago Martins se encontram em evento, em São Paulo",
35
+ :marca=>"Contigo",
36
+ :descricao=>"<p>\n\tUm time de estrelas se reuniu na ma...",
37
+ :tipo_recurso=>"feed", :link=>{:href=>"http://contigo.abril.com.br/noticias.rss?resource_id=0",
38
+ :rel=>"feed",
39
+ :type=>"application/json"}}]}
40
+
41
+
42
+
43
+ #For a content
44
+
45
+ response = Hyperfeed::Client.at("http://contigo.abril.com.br/noticias.rss?resource_id=7").get
46
+ puts response.hyperfeed.inspect
47
+
48
+ {:id=>"http://contigo.abril.com.br/noticias.rss?resource_id=7&resource_id=7",
49
+ :title=>"Ticiane Pinheiro vira boneca e mostra foto no Twitter",
50
+ :midia=>{:url=>"http://contigo.abril.com.br/resources/files/image/2013/4/132091110-ticiane-pinheiro-m.jpg?1365085327",
51
+ :type=>"image/jpeg"},
52
+ :data=>"2013-04-04 11:20:00 -0300",
53
+ :descricao=>"<p>\n\t<a href=\"/famosos/ticiane-pinheiro/apresentadora/\">Ticiane Pinheiro</a> ganhou um presente na man...",
54
+ :rel=>"materia",
55
+ :type=>"text/html"}}
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/hyperfeed.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hyperfeed/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "hyperfeed"
8
+ gem.version = Hyperfeed::VERSION
9
+ gem.authors = ["Guilherme Kato"]
10
+ gem.email = ["guilherme.kato@abril.com.br"]
11
+ gem.description = %q{An adapter to plug feeds content on hypermedia engines}
12
+ gem.summary = %q{An adapter to plug feeds content on hypermedia engines}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "http_monkey"
21
+ gem.add_dependency "nokogiri"
22
+ gem.add_development_dependency 'ruby-debug19'
23
+
24
+ end
25
+
@@ -0,0 +1,20 @@
1
+ module Hyperfeed
2
+ class Discover
3
+
4
+ include Hyperfeed::ResourceBuilder
5
+
6
+ def initialize(url, rss)
7
+ @url = url
8
+ @rss = rss
9
+ end
10
+
11
+ def hyperfeed
12
+ if @url =~ /resource_id=([0-9]+)/
13
+ hyperfeed = get_resource(@rss, @url, $1.to_i)
14
+ else
15
+ hyperfeed = retrieve_resources_list(@rss, @url, {})
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module Hyperfeed
2
+ class AdapterMiddleware
3
+
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ code, headers, body = @app.call(env)
10
+
11
+ url = env.uri.to_s
12
+ rss = Nokogiri::XML(body)
13
+
14
+ discover = Hyperfeed::Discover.new(url, rss)
15
+ HttpMonkey::Client::Response.inject_with(:hyperfeed, discover.hyperfeed)
16
+
17
+ [code, headers, body]
18
+ end
19
+
20
+ end
21
+ end
22
+
@@ -0,0 +1,72 @@
1
+ module Hyperfeed
2
+ module ResourceBuilder
3
+
4
+ def get_resource(rss, url, index)
5
+ item = rss.xpath("//item")[index]
6
+ {
7
+ :id => generate_id(url, index),
8
+ :title => item.xpath("title").inner_text.strip,
9
+ :midias => get_media(item),
10
+ :data => item.xpath("pubDate").inner_text,
11
+ :descricao => item.xpath("description").inner_text.strip,
12
+ :link => {
13
+ :href => item.xpath("link").inner_text,
14
+ :rel => "materia",
15
+ :type => "text/html"
16
+ }
17
+ }
18
+ end
19
+
20
+ def get_media(item)
21
+ media = ""
22
+ unless item.xpath("enclosure").nil?
23
+ media = {
24
+ :url => item.xpath("enclosure").attr("url").inner_text,
25
+ :type => item.xpath("enclosure").attr("type").inner_text
26
+ }
27
+ end
28
+ end
29
+
30
+ def retrieve_resources_list(rss, url, options={})
31
+ total = rss.xpath("//item").size
32
+ per_page = options[:per_page] || 10
33
+ page = options[:page] || 1
34
+ results = build_results(rss, url)
35
+
36
+ resource = {:itens_por_pagina => per_page,
37
+ :pagina_atual => page,
38
+ :paginas_totais => total/per_page,
39
+ :total_resultado => total,
40
+ :resultado => results
41
+ }
42
+ end
43
+
44
+ def generate_id(url, index)
45
+ char = url.include?("?") ? "&" : "?"
46
+ "#{url}#{char}resource_id=#{index}"
47
+ end
48
+
49
+ def build_results(rss, url)
50
+ results = []
51
+ rss.xpath("//item").each_with_index do |item, index|
52
+ id = generate_id(url, index)
53
+ results << {
54
+ :id => id,
55
+ :data => item.xpath("pubDate").inner_text,
56
+ :titulo => item.xpath("title").inner_text.strip,
57
+ :marca => item.xpath("source").inner_text,
58
+ :descricao => item.xpath("description").inner_text.strip,
59
+ :tipo_recurso => "feed",
60
+ :link => {
61
+ :href => id,
62
+ :rel => "feed",
63
+ :type => "application/json"
64
+ }
65
+ }
66
+ end
67
+ results
68
+ end
69
+
70
+ end
71
+ end
72
+
@@ -0,0 +1,3 @@
1
+ module Hyperfeed
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hyperfeed.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "hyperfeed/version"
2
+ require "http_monkey"
3
+ require "nokogiri"
4
+ require "json"
5
+
6
+ require "ruby-debug"
7
+
8
+ require "hyperfeed/resource_builder"
9
+ require "hyperfeed/discover"
10
+ require "hyperfeed/middleware/adapter_middleware"
11
+
12
+ class HttpMonkey::Client::Response
13
+ attr_accessor :hyperfeed
14
+
15
+ @@injections = {}
16
+ def self.inject_with(attribute, value)
17
+ @@injections[attribute] = value
18
+ end
19
+
20
+ def initialize(code, headers, body)
21
+ super
22
+ self.headers = HttpObjects::HeadersHash.new(headers)
23
+ after_initialize
24
+ end
25
+
26
+ protected
27
+
28
+ def after_initialize
29
+ @@injections.each do |attribute, value|
30
+ send("#{attribute}=", value)
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ Hyperfeed::Client = HttpMonkey.build do
37
+ middlewares.use Hyperfeed::AdapterMiddleware
38
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hyperfeed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guilherme Kato
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: http_monkey
16
+ requirement: &70347311578380 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70347311578380
25
+ - !ruby/object:Gem::Dependency
26
+ name: nokogiri
27
+ requirement: &70347311577860 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70347311577860
36
+ - !ruby/object:Gem::Dependency
37
+ name: ruby-debug19
38
+ requirement: &70347311577360 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70347311577360
47
+ description: An adapter to plug feeds content on hypermedia engines
48
+ email:
49
+ - guilherme.kato@abril.com.br
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - hyperfeed.gemspec
60
+ - lib/hyperfeed.rb
61
+ - lib/hyperfeed/discover.rb
62
+ - lib/hyperfeed/middleware/adapter_middleware.rb
63
+ - lib/hyperfeed/resource_builder.rb
64
+ - lib/hyperfeed/version.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.15
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: An adapter to plug feeds content on hypermedia engines
89
+ test_files: []