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

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_author(node) click to toggle source
# File lib/kktix-api/kktix_api.rb, line 61
def self.parse_author(node)
  author_node = node.css('.host')
  name = author_node.text.strip
  uri = author_node.css('> a').attr('href').value
  { name: name, uri: uri }
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