hyperfeed 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  An adapter to plug feeds content on hypermedia engines
4
4
 
5
+ THIS A PROOF OF CONCEPT, SO COWBOY MODE IS ON!
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -21,23 +23,23 @@ Or install it yourself as:
21
23
 
22
24
  ``` ruby
23
25
  #For content lists:
24
-
26
+
25
27
  response = Hyperfeed::Client.at("http://contigo.abril.com.br/noticias.rss").get
26
28
  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",
29
+
30
+ {:itens_por_pagina=>10,
31
+ :pagina_atual=>1,
32
+ :paginas_totais=>1,
33
+ :total_resultado=>10,
34
+ :resultado=>[{:id=>"http://contigo.abril.com.br/noticias.rss?resource_id=0",
35
+ :data=>"2013-04-04 12:38:00 -0300",
36
+ :titulo=>"Sabrina Sato, Rodrigo Faro e Thiago Martins se encontram em evento, em São Paulo",
37
+ :marca=>"Contigo",
38
+ :descricao=>"<p>\n\tUm time de estrelas se reuniu na ma...",
39
+ :tipo_recurso=>"feed", :link=>{:href=>"http://contigo.abril.com.br/noticias.rss?resource_id=0",
40
+ :rel=>"feed",
39
41
  :type=>"application/json"}}]}
40
-
42
+
41
43
 
42
44
 
43
45
  #For a content
@@ -45,13 +47,13 @@ Or install it yourself as:
45
47
  response = Hyperfeed::Client.at("http://contigo.abril.com.br/noticias.rss?resource_id=7").get
46
48
  puts response.hyperfeed.inspect
47
49
 
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",
50
+ {:id=>"http://contigo.abril.com.br/noticias.rss?resource_id=7&resource_id=7",
51
+ :title=>"Ticiane Pinheiro vira boneca e mostra foto no Twitter",
52
+ :midia=>{:url=>"http://contigo.abril.com.br/resources/files/image/2013/4/132091110-ticiane-pinheiro-m.jpg?1365085327",
53
+ :type=>"image/jpeg"},
54
+ :data=>"2013-04-04 11:20:00 -0300",
55
+ :descricao=>"<p>\n\t<a href=\"/famosos/ticiane-pinheiro/apresentadora/\">Ticiane Pinheiro</a> ganhou um presente na man...",
56
+ :rel=>"materia",
55
57
  :type=>"text/html"}}
56
58
  ```
57
59
 
data/Rakefile CHANGED
@@ -1 +1,17 @@
1
+ #!/usr/bin/env rake
2
+ require 'rubygems'
1
3
  require "bundler/gem_tasks"
4
+ require 'rake'
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
7
+
8
+ begin
9
+ require "bundler/gem_tasks"
10
+ require 'bundler/setup'
11
+ rescue LoadError => e
12
+ puts e
13
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
14
+ end
15
+
16
+ task :default => :spec
17
+ RSpec::Core::RakeTask.new(:spec)
data/hyperfeed.gemspec CHANGED
@@ -10,16 +10,17 @@ Gem::Specification.new do |gem|
10
10
  gem.email = ["guilherme.kato@abril.com.br"]
11
11
  gem.description = %q{An adapter to plug feeds content on hypermedia engines}
12
12
  gem.summary = %q{An adapter to plug feeds content on hypermedia engines}
13
- gem.homepage = ""
13
+ gem.homepage = "https://github.com/gkato/hyperfeed"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency "http_monkey"
21
- gem.add_dependency "nokogiri"
22
- gem.add_development_dependency 'ruby-debug19'
20
+ gem.add_dependency "http_monkey", "~> 0.0"
21
+ gem.add_dependency "nokogiri", "~> 1.5"
22
+ gem.add_dependency 'methodize'
23
23
 
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'fakeweb'
24
26
  end
25
-
@@ -1,67 +1,68 @@
1
+ require 'digest/md5'
2
+
1
3
  module Hyperfeed
2
4
  module ResourceBuilder
3
5
 
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
- }
6
+ def get_resource(feed, id)
7
+ item = feed.content.xpath("//item").find { |i| generate_id(i) == id }
8
+
9
+ resource = {:id => id}
10
+ item.children.reject{ |x| x.name == "text" }.each do |node|
11
+ if node.name == "enclosure"
12
+ resource[:enclosure] ||= []
13
+ resource[:enclosure] << get_media(node)
14
+ else
15
+ resource[node.name.downcase.to_sym] = node.inner_text.strip
16
+ end
17
+ end
18
+
19
+ resource.extend(Methodize)
18
20
  end
19
21
 
20
22
  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
- }
23
+ media = {
24
+ :url => item.attr("url").strip,
25
+ :type => item.attr("type").strip,
26
+ }
27
+ item.children.reject{ |x| x.name == "text" }.each do |node|
28
+ media[node.name.to_sym] = node.inner_text.strip
27
29
  end
30
+ media
28
31
  end
29
32
 
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)
33
+ def retrieve_resources_list(feed, options={})
34
+ total = feed.content.xpath("//item").size
35
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
- }
36
+ per_page = (options[:per_page] || 10).to_i
37
+ page = (options[:page] || 1).to_i
38
+
39
+ results = build_results(feed).each_slice(per_page).to_a
40
+
41
+ total_pages = results.size
42
+ results = results[page-1]
43
+
44
+ resource = {:per_page => per_page,
45
+ :current_page => page,
46
+ :total_pages => total_pages,
47
+ :total_results => total,
48
+ :result => results
49
+ }.extend(Methodize)
42
50
  end
43
51
 
44
- def generate_id(url, index)
45
- char = url.include?("?") ? "&" : "?"
46
- "#{url}#{char}resource_id=#{index}"
52
+ def generate_id(item)
53
+ link = (item.xpath("link") || item.xpath("guid")).inner_text
54
+ Digest::MD5.hexdigest(link)
47
55
  end
48
56
 
49
- def build_results(rss, url)
57
+ def build_results(feed)
50
58
  results = []
51
- rss.xpath("//item").each_with_index do |item, index|
52
- id = generate_id(url, index)
59
+ feed.content.xpath("//item").each do |item|
60
+ id = generate_id(item)
53
61
  results << {
54
62
  :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
- }
63
+ :pubdate => (item.xpath("pubDate").inner_text rescue ""),
64
+ :title => (item.xpath("title").inner_text.strip rescue ""),
65
+ :source => (item.xpath("source").inner_text rescue "")
65
66
  }
66
67
  end
67
68
  results
@@ -1,3 +1,3 @@
1
1
  module Hyperfeed
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/hyperfeed.rb CHANGED
@@ -1,38 +1,30 @@
1
1
  require "hyperfeed/version"
2
2
  require "http_monkey"
3
3
  require "nokogiri"
4
- require "json"
5
-
6
- require "ruby-debug"
7
4
 
8
5
  require "hyperfeed/resource_builder"
9
- require "hyperfeed/discover"
10
- require "hyperfeed/middleware/adapter_middleware"
11
-
12
- class HttpMonkey::Client::Response
13
- attr_accessor :hyperfeed
6
+ require "ostruct"
7
+ require 'methodize'
14
8
 
15
- @@injections = {}
16
- def self.inject_with(attribute, value)
17
- @@injections[attribute] = value
18
- end
9
+ class Hyperfeed::Client
10
+ include Hyperfeed::ResourceBuilder
19
11
 
20
- def initialize(code, headers, body)
21
- super
22
- self.headers = HttpObjects::HeadersHash.new(headers)
23
- after_initialize
24
- end
12
+ def initialize(url, options)
13
+ @options = options
25
14
 
26
- protected
15
+ response = HttpMonkey.at(url).get
16
+ raise "Error: #{response.code} - #{response.body}" unless response.code == 200
27
17
 
28
- def after_initialize
29
- @@injections.each do |attribute, value|
30
- send("#{attribute}=", value)
31
- end
18
+ content = Nokogiri::XML(response.body)
19
+ @feed = OpenStruct.new(:url => url, :content => content)
32
20
  end
33
21
 
34
- end
22
+ def self.at(url, options = {})
23
+ new(url, options)
24
+ end
35
25
 
36
- Hyperfeed::Client = HttpMonkey.build do
37
- middlewares.use Hyperfeed::AdapterMiddleware
26
+ def get(id = nil)
27
+ return retrieve_resources_list(@feed, @options) unless id
28
+ get_resource(@feed, id)
29
+ end
38
30
  end