Sha256: 4374b7fc779f953b743ee7ace0b8d815410e87814e8a49818dbd57bbf15fb896

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module Trumpet
  class Channel
    @@attributes = [
      :name,
      :receiver_id,
      :transmitter_id,
      :owner_id,
      :created_at,
      :updated_at
    ]
    
    attr_reader *@@attributes
    
    def self.create(options)
      Channel.new(Trumpet::Request.post('/channels', :parameters => options))
    end
    
    def self.find(name)
      Channel.new(Trumpet::Request.get("/channels/#{name}"))
    end
    
    def self.all
      Trumpet::Request.get('/channels').map { |attributes| Channel.new(attributes) }
    end
    
    def self.all_by_user(name)
      channels = Trumpet::Request.get("/users/#{name}/channels")
      channels.map { |attributes| Channel.new(attributes) }
    end
        
    def broadcast(message)
      !!Trumpet::Request.post("/channels/#{@name}/messages", :parameters => message.to_h, :parse_response => false)
    end
    
    def messages
      messages = Trumpet::Request.get("/channels/#{@name}/messages")
      messages.map { |attributes| Message.new(attributes) }
    end

    protected
    
      def initialize(attributes)
        @@attributes.each do |attr|
          self.instance_variable_set(:"@#{attr.to_s}", attributes[attr.to_s]) if attributes[attr.to_s]
        end
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trumpet-trumpet-0.0.10 lib/trumpet/channel.rb
trumpet-trumpet-0.0.8 lib/trumpet/channel.rb
trumpet-trumpet-0.0.9 lib/trumpet/channel.rb