Class: KktixEvent::KktixApi

Inherits:
Object
  • Object
show all
Defined in:
lib/kktix-api/kktix_api.rb

Overview

Service for retriving KKTIX events

Class Method Summary collapse

Class Method Details

.events(slug: nil) ⇒ Object



12
13
14
15
16
# 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) ⇒ Object



25
26
27
28
# 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

.hash_s_to_sym(obj) ⇒ Object



32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



61
62
63
64
65
66
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



54
55
56
57
58
59
# 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

.search(query, start_at: Date.today) ⇒ Object



18
19
20
21
22
23
# 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