Top Level Namespace

Defined Under Namespace

Modules: SlackResources

Constant Summary collapse

BASE_DIR =
Pathname('./resources/event_api')
EXAMPLES_DIR =
BASE_DIR.join('examples')

Instance Method Summary collapse

Instance Method Details

#analise(html) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 33

def analise(html)
  doc = Nokogiri.parse(html)
  [
    pick_response(doc),
    pick_compatibility(doc),
    pick_scopes(doc),
  ]
end

#event_api_pagesObject



27
28
29
30
31
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 27

def event_api_pages
  @event_api_pages ||= event_api_urls.map do |url|
    [url, url.split('/').pop, fetch(url)]
  end
end

#event_api_urlsObject



21
22
23
24
25
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 21

def event_api_urls
  base = 'https://api.slack.com/events/api'
  doc = Nokogiri.parse(fetch(base))
  doc.css('#api_main_content a.bold.block').map { |a| URI.join(base, a.attributes['href']).to_s }
end

#fetch(url) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 12

def fetch(url)
  tmp = Pathname('./tmp')
  path = tmp.join(url.tr(':', '_').tr('/', '_')).to_s

  return File.read(path) if File.exist?(path)

  RestClient.get(url).tap { |data| File.write(path, data) }
end

#force(clean) ⇒ Object



72
73
74
75
76
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 72

def force(clean)
  eval(clean)
rescue StandardError
  raise
end

#parse(json) ⇒ Object



65
66
67
68
69
70
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 65

def parse(json)
  clean = json.gsub(/\{[\s\n]*…[^}]*\},?/m, '{},').gsub(/\{[\s\n]*\.\.\.[^}]*\},?/m, '{},').gsub(%("3"\n), %("3",\n))
  JSON.parse(clean)
rescue StandardError
  force(clean)
end

#pick_compatibility(doc) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 47

def pick_compatibility(doc)
  doc.css('#api_main_content .col.span_2_of_3.small')[0]
     .content
     .gsub("\t\t", "\t")
     .split("\t")
     .select(&:present?)[1..-1]&.
    map(&:chomp) || []
end

#pick_response(doc) ⇒ Object



42
43
44
45
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 42

def pick_response(doc)
  response_raw = doc.css('#api_main_content pre')[0].content
  parse(response_raw)
end

#pick_scopes(doc) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 56

def pick_scopes(doc)
  doc.css('#api_main_content .col.span_1_of_3.small')[0]&.
    content
     .gsub("\t\t", "\t")
     .split("\t")
     .select(&:present?)[1..-1]&.
    map(&:chomp) || []
end

#write_response_sampleObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/slack_resources/generator/event_api/fetch.rb', line 78

def write_response_sample
  types = []
  on_event_api = []
  on_rtm = []
  all_scopes = []
  subscriptions = {}

  event_api_pages.each do |url, type, page|
    response, compatibility, scopes = analise(page)

    types << type
    on_event_api << type if compatibility.include?('Events API')
    on_rtm << type if compatibility.include?('RTM')

    all_scopes += scopes

    subscriptions[type] = {
      url: url,
      compatibility: compatibility,
      scopes: scopes,
    }
    File.write(EXAMPLES_DIR.join("#{type}.json"), JSON.pretty_generate(response))
  end

  File.write(BASE_DIR.join('meta.json'), JSON.pretty_generate({
                                                                types: types.uniq,
                                                                on_event_api: on_event_api.uniq,
                                                                on_rtm: on_rtm.uniq,
                                                                scopes: all_scopes.uniq,
                                                                subscriptions: subscriptions,
                                                              }))
end