Sha256: 1518b54a0e005a6dc57c632c3ff4f0ea45434f7624f0eceb0e73aa526902bc1e

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

module FayeShard

  class Shard

    attr_accessor :configuration

    #  Creates a shard instance and assigns the config to it.
    #
    #  * <tt>config</tt>:: Config to use.
    #
    def initialize(config)
      self.configuration = config.with_indifferent_access
    end

    #  Returns URL for client.
    #
    #  * <tt>https</tt>:: Specifies whether to use SSL connection or not
    #
    def url(https = false)
      secured = (configuration['secured'] || false) & https
      port = secured ? configuration["secured_port"] : configuration["port"]
      "http#{secured ? 's' : ''}://#{configuration["host"]}:#{port}/faye"
    end

    #  Returns default client JS url
    #
    #  * <tt>https</tt>:: Specifies whether to use SSL connection or not
    #
    def js_url(https = false)
      url(https) + '.js'
    end

    #  Local url, needed for RoR <-> Faye communication
    #
    def local_url
      host = configuration["local_host"] || configuration["host"]
      "http://#{host}:#{configuration["port"]}/faye"
    end

    #  Pushes data to a Faye shard
    #
    #  * <tt>channel</tt>:: User's channel
    #  * <tt>data</tt>::    Data to push
    #  * <tt>ext</tt>::     Faye extensions, eg. auth_token
    #
    def push(channel, data, ext = {})
      uri = URI.parse(self.local_url)
      http = Net::HTTP.new(uri.host, uri.port)
      req = Net::HTTP::Post.new uri.path
      req.set_form_data('message' => {'channel' => channel, 'data' => data, 'ext' => ext}.to_json)
      http.request req
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faye_shards-0.1.2 lib/faye_shard/shard.rb
faye_shards-0.1.1 lib/faye_shard/shard.rb