Skip to content

Commit

Permalink
cleanup, removing faraday
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinfo committed Mar 13, 2014
1 parent 147d451 commit 207bbdd
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 67 deletions.
6 changes: 1 addition & 5 deletions analytics-ruby.gemspec
Expand Up @@ -15,11 +15,7 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/segmentio/analytics-ruby'
spec.license = 'MIT'

spec.add_dependency 'faraday', ['>= 0.8']
spec.add_dependency 'faraday_middleware', ['>= 0.8', '< 0.10']
spec.add_dependency 'multi_json', ['~> 1.0']

# Ruby 1.8 requires json for faraday_middleware
# Ruby 1.8 requires json
spec.add_dependency 'json', ['~> 1.7'] if RUBY_VERSION < "1.9"

spec.add_development_dependency('rake')
Expand Down
4 changes: 2 additions & 2 deletions lib/analytics-ruby/client.rb
Expand Up @@ -26,11 +26,11 @@ def initialize(options = {})

@queue = Queue.new
@secret = options[:secret]
@max_queue_size = options[:max_queue_size] || AnalyticsRuby::Defaults::Queue::MAX_SIZE
@max_queue_size = options[:max_queue_size] || Defaults::Queue::MAX_SIZE

check_secret

@consumer = AnalyticsRuby::Consumer.new @queue, @secret, options
@consumer = Consumer.new @queue, @secret, options
@thread = ConsumerThread.new { @consumer.run }

at_exit do
Expand Down
4 changes: 2 additions & 2 deletions lib/analytics-ruby/consumer.rb
Expand Up @@ -23,7 +23,7 @@ def initialize(queue, secret, options = {})

@queue = queue
@secret = secret
@batch_size = options[:batch_size] || AnalyticsRuby::Defaults::Queue::BATCH_SIZE
@batch_size = options[:batch_size] || Defaults::Queue::BATCH_SIZE
@on_error = options[:on_error] || Proc.new { |status, error| }

@current_batch = []
Expand Down Expand Up @@ -54,7 +54,7 @@ def flush
end
}

req = AnalyticsRuby::Request.new
req = Request.new
res = req.post @secret, @current_batch
@on_error.call res.status, res.error unless res.status == 200
@mutex.synchronize {
Expand Down
5 changes: 3 additions & 2 deletions lib/analytics-ruby/defaults.rb
Expand Up @@ -3,9 +3,10 @@ module AnalyticsRuby
module Defaults

module Request
BASE_URL = 'https://api.segment.io' unless defined? AnalyticsRuby::Defaults::Request::BASE_URL
HOST = 'api.segment.io' unless defined? AnalyticsRuby::Defaults::Request::HOST
PORT = 443 unless defined? AnalyticsRuby::Defaults::Request::PORT
PATH = '/v1/import' unless defined? AnalyticsRuby::Defaults::Request::PATH
SSL = { :verify => false } unless defined? AnalyticsRuby::Defaults::Request::SSL
SSL = true unless defined? AnalyticsRuby::Defaults::Request::SSL
HEADERS = { :accept => 'application/json' } unless defined? AnalyticsRuby::Defaults::Request::HEADERS
RETRIES = 4 unless defined? AnalyticsRuby::Defaults::Request::RETRIES
BACKOFF = 30.0 unless defined? AnalyticsRuby::Defaults::Request::BACKOFF
Expand Down
28 changes: 0 additions & 28 deletions lib/analytics-ruby/json.rb

This file was deleted.

52 changes: 26 additions & 26 deletions lib/analytics-ruby/request.rb
@@ -1,9 +1,9 @@

require 'analytics-ruby/defaults'
require 'analytics-ruby/response'
require 'analytics-ruby/json'
require 'faraday'
require 'faraday_middleware'
require 'net/http'
require 'net/https'
require 'json'

module AnalyticsRuby

Expand All @@ -12,19 +12,20 @@ class Request
# public: Creates a new request object to send analytics batch
#
def initialize(options = {})

options[:url] ||= AnalyticsRuby::Defaults::Request::BASE_URL
options[:ssl] ||= AnalyticsRuby::Defaults::Request::SSL
options[:headers] ||= AnalyticsRuby::Defaults::Request::HEADERS
@path = options[:path] || AnalyticsRuby::Defaults::Request::PATH
@retries = options[:retries] || AnalyticsRuby::Defaults::Request::RETRIES
@backoff = options[:backoff] || AnalyticsRuby::Defaults::Request::BACKOFF

@conn = Faraday.new options do |faraday|
faraday.request :json
faraday.response :json, :content_type => /\bjson$/
faraday.adapter Faraday.default_adapter
end
options[:host] ||= Defaults::Request::HOST
options[:port] ||= Defaults::Request::PORT
options[:ssl] ||= Defaults::Request::SSL
options[:headers] ||= Defaults::Request::HEADERS
@path = options[:path] || Defaults::Request::PATH
@retries = options[:retries] || Defaults::Request::RETRIES
@backoff = options[:backoff] || Defaults::Request::BACKOFF

http = Net::HTTP.new(options[:host], options[:port])
http.use_ssl = options[:ssl]
http.read_timeout = 8
http.open_timeout = 4

@http = http
end

# public: Posts the secret and batch of messages to the API.
Expand All @@ -35,27 +36,26 @@ def post(secret, batch)
status, error = nil, nil
remaining_retries = @retries
backoff = @backoff

headers = { 'Content-Type' => 'application/json', 'accept' => 'application/json' }
begin
res = @conn.post do |req|
req.options[:timeout] = 8
req.options[:open_timeout] = 3
req.url(@path)
req.body = AnalyticsRuby::JSON::dump :secret => secret, :batch => batch
end
status = res.status
error = res.body["error"]
payload = JSON.generate :secret => secret, :batch => batch
res = @http.request(Net::HTTP::Post.new(@path, headers), payload)
status = res.code.to_i
body = JSON.parse(res.body)
error = body["error"]

rescue Exception => err
puts "err: #{err}"
status = -1
error = "Connection error: #{err}"
puts "retries: #{remaining_retries}"
unless (remaining_retries -=1).zero?
sleep(backoff)
retry
end
end

AnalyticsRuby::Response.new status, error
Response.new status, error
end
end
end
4 changes: 2 additions & 2 deletions spec/consumer_spec.rb
Expand Up @@ -24,7 +24,7 @@

it 'should not error if the endpoint is unreachable' do

Faraday::Connection.any_instance.stub(:post).and_raise(Exception)
Net::HTTP.any_instance.stub(:post).and_raise(Exception)

queue = Queue.new
queue << {}
Expand All @@ -33,7 +33,7 @@

queue.should be_empty

Faraday::Connection.any_instance.unstub(:post)
Net::HTTP.any_instance.unstub(:post)
end

it 'should execute the error handler if the request is invalid' do
Expand Down

0 comments on commit 207bbdd

Please sign in to comment.