class NthuEvent::NthuEventApi
Service for all FB API calls
Constants
- BASE_URL
- EVENT_TYPE_URL
Public Class Methods
events(type: :admin, page: 0)
click to toggle source
# File lib/nthuevent/nthu_event_api.rb, line 21 def self.events(type: :admin, page: 0) type = EVENT_TYPE_URL[type.to_sym] events, totalpage = get_event_list("#{BASE_URL}#{type}-#{page}.php") return events if page.positive? # get all pages if page is zero (2..totalpage).each do |p| evnts, _total = get_event_list("#{BASE_URL}#{type}-#{p}.php") events += evnts end events end
Private Class Methods
get_event_content(url)
click to toggle source
# File lib/nthuevent/nthu_event_api.rb, line 60 def self.get_event_content(url) content_doc = Nokogiri::HTML(open(url)) content = content_doc.css('.ptcontent').text.strip content end
get_event_list(url)
click to toggle source
# File lib/nthuevent/nthu_event_api.rb, line 36 def self.get_event_list(url) doc = Nokogiri::HTML(open(url)) events = doc.css('div.h5').map do |node| date, link, title = parse_event_metadata(node) content = get_event_content(link) { title: title, content: content, url: link, date: date } end # get total page pagenum_node = doc.css('#navigate a.pagenum') totalpage = 0 totalpage = get_pagenum_from_url(pagenum_node.last.attr('href')) unless pagenum_node.nil? [events, totalpage] end
get_pagenum_from_url(url)
click to toggle source
# File lib/nthuevent/nthu_event_api.rb, line 68 def self.get_pagenum_from_url(url) match_data = url.match('-(\d+)\.php') Integer(match_data.captures[0]) end
parse_event_metadata(node)
click to toggle source
# File lib/nthuevent/nthu_event_api.rb, line 52 def self.parse_event_metadata(node) date = node.css('.date').text.gsub(/[\[\]\s]+/, '') event_node = node.css('.ptname a') link = URI.join(BASE_URL, event_node.attr('href').value) title = event_node.attr('title').value [date, link, title] end