Sha256: 9d17d89da02e2582654cdf9087cb50e99bbeb2b9dd1d2f3a19112a47908631ce

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

module UI
  # https://developers.facebook.com/docs/messenger-platform/send-api-reference/quick-replies
  class QuickReplies
    def self.build(*replies)
      replies.map do |reply|
        case reply
        when Hash then build_from_hash(reply)
        when Array then build_from_array(reply)
        when String then build_from_string(reply)
        else
          raise ArgumentError, 'Arguments should be hashes or arrays of two'
        end
      end
    end

    def self.location
      [{ content_type: 'location' }]
    end

    private_class_method def self.build_from_hash(reply)
      unless reply.key?(:content_type)
        reply[:content_type] = 'text'
        raise ArgumentError, "type 'text' should have a payload" unless reply.key?(:payload)
      end
      reply
    end

    private_class_method def self.build_from_string(reply)
      build_from_array([reply, reply.upcase])
    end

    private_class_method def self.build_from_array(reply)
      error_msg = 'Only accepts arrays of two elements'
      raise ArgumentError, error_msg if reply.length != 2
      { content_type: 'text', title: reply[0].to_s, payload: reply[1].to_s }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubotnik-0.2.3 lib/ui/quick_replies.rb
rubotnik-0.2.2 lib/ui/quick_replies.rb
rubotnik-0.2.1 lib/ui/quick_replies.rb