Sha256: b07e9e7cc84964276907e7599fa477fe08ba152b2f31f04a6240ca08fcfb14cb

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

module Bot
  module Message
    class Base
      attr_accessor :message

      def initialize(to, options=false)
        raise NotImplementedError
      end

      def as_json(options=nil)
        self.message.to_h
      end

      def method_missing(m, *args, &block)
        if m =~ /.+=/
          args.each do |arg|
            self.message[m.to_s.gsub('=', '')] = arg
          end
        else
          self.message[m.to_s]
        end
      end

    protected
      def suggested_responses=(suggested_responses)
        case suggested_responses
        when Array
          self.message['keyboards'] = [{
            'type' => 'suggested',
            'responses' => build_suggested_responses(suggested_responses)
          }]
        when Hash
          self.message.merge!(suggested_responses)
        end
      end

      def build_suggested_responses(suggested_responses)
        suggested_responses.map do |response|
          case response
          when String
            {
              'type' => 'text',
              'body' => response
            }
          when Hash
            response
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bot-0.0.46 lib/bot/messages/base.rb
bot-0.0.45 lib/bot/messages/base.rb
bot-0.0.44 lib/bot/messages/base.rb
bot-0.0.43 lib/bot/messages/base.rb
bot-0.0.42 lib/bot/messages/base.rb
bot-0.0.41 lib/bot/messages/base.rb
bot-0.0.40 lib/bot/messages/base.rb
bot-0.0.39 lib/bot/messages/base.rb