Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
overwrite original sparks
Browse files Browse the repository at this point in the history
  • Loading branch information
tpitale committed Oct 1, 2013
1 parent 02dcc32 commit 0549581
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 234 deletions.
175 changes: 0 additions & 175 deletions lib/sparks-http.rb

This file was deleted.

101 changes: 43 additions & 58 deletions lib/sparks.rb
@@ -1,24 +1,23 @@
require 'logger'
require 'net/http/persistent'
require 'http'
require 'yajl'
require 'base64'

# sparks, a tiny Campfire library

# Usage:
# c = Sparks.new('subdomain', 'abc123')
# r = c.room "Room Name"
# c.say r["id"], "hi there"
# c.paste r["id"], "class Foo\nend"
# c = Sparks.new('subdomain', 'abc123')3
# r = c.room_named "Room Name"
# c.say r[:id], "hi there"
# c.paste r[:id], "class Foo\nend"
# c.watch(r[:id]) {|message| p message}
class Sparks
attr_reader :logger

def initialize subdomain, token, opts = {}
@base = URI("https://#{subdomain}.campfirenow.com")
@token = token
@logger = opts[:logger] || Logger.new(STDOUT)
@http = Net::HTTP::Persistent.new("sparks")
@http.ca_file = opts[:ca_file] if opts[:ca_file]
@http.verify_mode = opts[:verify_mode] if opts[:verify_mode]
@rooms ||= {}
end

Expand Down Expand Up @@ -77,32 +76,26 @@ def watch(id)
uri = URI("https://streaming.campfirenow.com") + "/room/#{id}/live.json"
logger.debug "Ready to stream from #{uri}"

request = Net::HTTP::Get.new(uri.path)
request.basic_auth @token, "x"
response = HTTP.with_headers(:authorization => authorization).stream.get(uri.to_s)

@http.request(uri, request) do |response|
logger.debug "Connected and streaming from room #{id}"
# connected! allow retries.
retries = 0
# connected! allow retries.
retries = 0

# Set up a Yajl stream parser
parser = Yajl::Parser.new(:symbolize_keys => true)
parser.on_parse_complete = -> hash { yield hash }
logger.debug "Connected and streaming from room #{id}"

# Feed chunks into the stream parser
response.read_body do |chunk|
# Campfire keepalive pings
next if chunk == " "
parser << chunk
end
# Set up a Yajl stream parser
parser = Yajl::Parser.new(:symbolize_keys => true)
parser.on_parse_complete = -> hash { yield hash }

# Feed chunks into the stream parser
response.body do |chunk|
# Campfire keepalive pings
next if chunk == " "
parser << chunk
end
rescue Yajl::ParseError, # Bad JSON in the response
SystemCallError, # All Errno errors
SocketError, # Errors from socket operations
Net::HTTP::Persistent::Error, # Timeout, SSL, or connection error
Net::HTTPBadResponse, # response wasn't 2xx
Net::HTTPHeaderSyntaxError, # response header issue
Net::ProtocolError => e # not http
SocketError # Errors from socket operations
# pass through errors if we haven't ever connected
raise e unless retries
# if we connected at least once, try, try, again
Expand All @@ -117,36 +110,24 @@ def req(uri, body = nil)
uri = @base + (uri + ".json") unless uri.is_a?(URI)
logger.debug "#{body ? 'POST' : 'GET'} #{uri}"

if body
request = Net::HTTP::Post.new(uri.path)
request.body = body unless body == :post
request = HTTP.with({
:content_type => "application/json",
:authorization => authorization
}).stream

retries ||= 0

response = if body
options = {}
options.merge!({:body => body}) unless body == :post

request.post(uri.to_s, options)
else
request = Net::HTTP::Get.new(uri.path)
request.get(uri.to_s)
end
request.content_type = "application/json"
request.basic_auth @token, "x"

retries ||= 0
response = @http.request(uri, request)
response.value # raises if response is not 2xx
parse_response(response)

rescue Net::HTTPRetriableError => e # response was 3xx
location = URI(response['location'])
logger.info "Request redirected to #{location}"
sleep 2
req(location, body)

rescue Net::HTTPServerException => e # response was 4xx
msg = "Authorization failed: HTTP #{response.code}"
msg << ": " << request.body if request.body && !request.body.empty?
raise msg

rescue SystemCallError, # All Errno errors
Net::HTTP::Persistent::Error, # Timeout, SSL, or connection error
Net::HTTPBadResponse, # response wasn't 2xx
Net::HTTPHeaderSyntaxError, # response header issue
Net::ProtocolError => e # not http
parse_response(response.body)
rescue SystemCallError # All Errno errors
# Retry if something goes wrong
retries += 1
logger.info "Request failed: #{e.class}: #{e.message}"
Expand All @@ -157,14 +138,18 @@ def req(uri, body = nil)

private

def authorization
"Basic: #{Base64.encode64([@token, 'x'].join(':'))}".strip
end

def parse_response(response)
if response.body.strip.empty?
if response.strip.empty?
true
else
Yajl::Parser.parse(response.body, :symbolize_keys => true)
Yajl::Parser.parse(response, :symbolize_keys => true)
end
rescue Yajl::ParseError
logger.debug "Couldn't parse #{res.inspect}: #{res.body.inspect}"
logger.debug "Couldn't parse #{response.inspect}"
{}
end

Expand Down
2 changes: 1 addition & 1 deletion sparks.gemspec
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]

gem.add_dependency "yajl-ruby", "~> 1.1"
gem.add_dependency "net-http-persistent", "~> 2.8"
gem.add_dependency "http", "~> 0.5"

gem.add_development_dependency "rake", "~> 10.0"
gem.add_development_dependency "bundler", "~> 1.2"
Expand Down

0 comments on commit 0549581

Please sign in to comment.