class Object

Constants

BASE_DIR
EXAMPLES_DIR

Public Instance Methods

analise(html) click to toggle source
# 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_pages() click to toggle source
# 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_urls() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/slack_resources/generator/event_api/fetch.rb, line 72
def force(clean)
  eval(clean)
rescue StandardError
  raise
end
parse(json) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_sample() click to toggle source
# 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