Sha256: 2d2a32ccc78d4728185d82583af4d25939bd2cef5b1a6dade7f303cb5f94093f

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

module Tinder

  # == Usage
  #
  #   campfire = Tinder::Campfire.new 'mysubdomain', :token => 'xyz'
  #
  #   room = campfire.create_room 'New Room', 'My new campfire room to test tinder'
  #   room.speak 'Hello world!'
  #   room.destroy
  #
  #   room = campfire.find_room_by_guest_hash 'abc123', 'John Doe'
  #   room.speak 'Hello world!'
  class Campfire
    attr_reader :connection

    # Create a new connection to the campfire account with the given +subdomain+.
    #
    # == Options:
    # * +:ssl+: use SSL for the connection, which is required if you have a Campfire SSL account.
    #           Defaults to false
    # * +:proxy+: a proxy URI. (e.g. :proxy => 'http://user:pass@example.com:8000')
    #
    #   c = Tinder::Campfire.new("mysubdomain", :ssl => true)
    def initialize(subdomain, options = {})
      @connection = Connection.new(subdomain, options)
    end

    # Get an array of all the available rooms
    # TODO: detect rooms that are full (no link)
    def rooms
      connection.get('/rooms.json')['rooms'].map do |room|
        Room.new(connection, room)
      end
    end

    # Find a campfire room by name
    def find_room_by_name(name)
      rooms.detect { |room| room.name == name }
    end

    # Find a campfire room by its guest hash
    def find_room_by_guest_hash(hash, name)
      rooms.detect { |room| room.guest_invite_code == hash }
    end

    # Creates and returns a new Room with the given +name+ and optionally a +topic+
    def create_room(name, topic = nil)
      connection.post('/rooms.json', { :room => { :name => name, :topic => topic } })
      find_room_by_name(name)
    end

    def find_or_create_room_by_name(name)
      find_room_by_name(name) || create_room(name)
    end

    # List the users that are currently chatting in any room
    def users
      rooms.map(&:users).flatten.compact.uniq.sort_by {|u| u[:name]}
    end

    # get the user info of the current user
    def me
      connection.get("/users/me.json")["user"]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tinder-1.4.4 lib/tinder/campfire.rb
tinder-1.4.3 lib/tinder/campfire.rb
tinder-1.4.2 lib/tinder/campfire.rb