Module: Pechkin::Generator
- Included in:
- PechkinAPI
- Defined in:
- lib/pechkin/api.rb
Overview
Generates all routes based on configuration
Instance Method Summary collapse
- #configure(config) ⇒ Object
- #create_chanel(channel, chanel_desc) ⇒ Object
- #create_chanels(chanels, bots) ⇒ Object
- #create_connector(bot, channel_name) ⇒ Object
-
#generate_endpoint(channel, message_name, message_desc) ⇒ Object
rubocop:disable Metrics/AbcSize.
Instance Method Details
#configure(config) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/pechkin/api.rb', line 7 def configure(config) base_path = config['base_path'] resource base_path do create_chanels(config['chanels'], config['bots']) end self end |
#create_chanel(channel, chanel_desc) ⇒ Object
41 42 43 44 45 |
# File 'lib/pechkin/api.rb', line 41 def create_chanel(channel, chanel_desc) chanel_desc['messages'].each do |, | generate_endpoint(channel, , ) end end |
#create_chanels(chanels, bots) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pechkin/api.rb', line 16 def create_chanels(chanels, bots) chanels.each do |chanel_name, chanel_desc| bot = bots[chanel_desc['bot']] connector = create_connector(bot, chanel_name) chat_ids = chanel_desc['chat_ids'] channel = Chanel.new(connector, chat_ids) channel.logger = logger resource chanel_name do create_chanel(channel, chanel_desc) end end end |
#create_connector(bot, channel_name) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pechkin/api.rb', line 30 def create_connector(bot, channel_name) case bot['connector'] when 'tg', 'telegram' TelegramConnector.new(bot['token']) when 'slack' SlackConnector.new(bot['token']) else raise 'Unknown connector ' + bot['connector'] + ' for ' + channel_name end end |
#generate_endpoint(channel, message_name, message_desc) ⇒ Object
rubocop:disable Metrics/AbcSize
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pechkin/api.rb', line 48 def generate_endpoint(channel, , ) params do # TODO: Can't extract this code to method because this block is # evaluated in separate scope (['filters'] || []).each do |field, filter| filter.match(%r{^/(.*)/$}) do |m| requires field.to_sym, type: String, regexp: Regexp.new(m[1]) end end end post do template = ['template'] opts = ['options'] || {} # Some services will send json, but without correct content-type, then # params will be parsed weirdely. So we try parse request body as json params = ensure_json(request.body.read, params) logger.info "Received message #{params.to_json}" logger.info "Will render template file #{template}" # If message description contains any variables will merge them with # received parameters. params = (['variables'] || {}).merge(params) channel.(template, params, opts) end # rubocop:enable Metrics/AbcSize end |