Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MembersPage class #10

Merged
merged 3 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/members_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'scraped'
require_relative 'remove_session_from_url_decorator'

class MembersPage < Scraped::HTML
decorator Scraped::Response::Decorator::CleanUrls
decorator RemoveSessionFromUrlDecorator

field :member_urls do
noko.css('div#RESULTADOS_DIPUTADOS div.listado_1 ul li a/@href').map(&:text).uniq
end

field :next_page_url do
next_page_link and next_page_link.text
end

private

def next_page_link
noko.at_css('//div[@class = "paginacion"]//a[contains("Página Siguiente")]/@href')
end
end
15 changes: 15 additions & 0 deletions lib/remove_session_from_url_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true
require 'uri'

class RemoveSessionFromUrlDecorator < Scraped::Response::Decorator
# Remove session information from urls
def body
noko = Nokogiri::HTML(super)
noko.css('a[href*="_piref"]').each do |a|
uri = URI.parse(a[:href])
uri.query = uri.query.gsub(/_piref[\d_]+\./, '')
a[:href] = uri.to_s
end
noko.to_s
end
end
Loading