Sha256: 4e3f23f03598a28d06e519d93b0c325bb961f85796338117d8741ec1782306df

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

# largely from https://github.com/aki017/slack-ruby-gem
require 'json-schema'
require 'erubis'

namespace :slack do
  namespace :real_time do
    namespace :api do
      desc 'Update API.'
      task update: [:git_update] do
        event_schema = JSON.parse(File.read('lib/slack/real_time/api/schema/event.json'))
        events = Dir.glob('lib/slack/web/api/slack-api-ref/events/**/*.json').each_with_object({}) do |path, result|
          name = File.basename(path, '.json')
          parsed = JSON.parse(File.read(path))
          JSON::Validator.validate(event_schema, parsed, insert_defaults: true)
          next if %w(message hello).include?(name)
          result[name] = parsed
        end
        # event_handlers.rb
        event_handlers_filename = 'lib/slack/real_time/event_handlers.rb'
        if File.exist?(event_handlers_filename)
          puts "Skipping #{event_handlers_filename}."
        else
          event_handlers_template = Erubis::Eruby.new(File.read('lib/slack/real_time/api/templates/event_handlers.erb'))
          File.write event_handlers_filename, event_handlers_template.result(events: events.keys)
        end
        # each event handler
        event_handler_template = Erubis::Eruby.new(File.read('lib/slack/real_time/api/templates/event_handler.erb'))
        events.each_pair do |name, json|
          event_handler_filename = "lib/slack/real_time/event_handlers/#{name}.rb"
          if File.exist?(event_handler_filename)
            puts "Skipping #{event_handler_filename}."
          else
            puts "Creating #{event_handler_filename}."
            rendered_event_handler = event_handler_template.result(
              name: json['name'],
              class_name: json['name'].split('_').each(&:capitalize!).join,
              desc: json['desc']
            )
            File.write event_handler_filename, rendered_event_handler
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack-ruby-client-0.6.0 lib/tasks/real_time.rake