class KktixEvent::KktixApi
Service for retriving KKTIX events
Public Class Methods
events(slug: nil)
click to toggle source
# File lib/kktix-api/kktix_api.rb, line 12 def self.events(slug: nil) response = HTTP.get(events_json_uri(slug: slug)).parse return hash_s_to_sym(response['entry']) if response.key?('entry') [] end
events_json_uri(slug: nil)
click to toggle source
# File lib/kktix-api/kktix_api.rb, line 25 def self.events_json_uri(slug: nil) return 'https://kktix.com/events.json' if slug.nil? "http://#{slug}.kktix.cc/events.json" end
search(query, start_at: Date.today)
click to toggle source
# File lib/kktix-api/kktix_api.rb, line 18 def self.search(query, start_at: Date.today) start_at = start_at.to_s uri = URI.escape("https://kktix.com/events?start_at=#{start_at}&search=#{query}") doc = Nokogiri::HTML(open(uri)) parse_search_result(doc) end
Private Class Methods
hash_s_to_sym(obj)
click to toggle source
# File lib/kktix-api/kktix_api.rb, line 32 def self.hash_s_to_sym(obj) return obj.map { |hash| hash_s_to_sym(hash) } if obj.class == Array obj.each_with_object({}) do |(k, v), memo| # if v.class == Array v = hash_s_to_sym(v) if v.class == Hash memo[k.to_sym] = v memo end end
parse_search_result(doc)
click to toggle source
# File lib/kktix-api/kktix_api.rb, line 42 def self.parse_search_result(doc) events = doc.css('ul.event-list > li').map do |node| title, url = parse_title node date = node.css('.date').text author = parse_author(node) summary = node.css('> .description').text { url: url, title: title, date: date, summary: summary, author: author } end events end
parse_title(node)
click to toggle source
# File lib/kktix-api/kktix_api.rb, line 54 def self.parse_title(node) title_node = node.css('> h2 > a') title = title_node.text.strip url = title_node.attr('href').value [title, url] end