analytics-ruby 0.6.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -15,11 +15,7 @@ Gem::Specification.new do |spec|
15
15
  spec.homepage = 'https://github.com/segmentio/analytics-ruby'
16
16
  spec.license = 'MIT'
17
17
 
18
- spec.add_dependency 'faraday', ['>= 0.8']
19
- spec.add_dependency 'faraday_middleware', ['>= 0.8', '< 0.10']
20
- spec.add_dependency 'multi_json', ['~> 1.0']
21
-
22
- # Ruby 1.8 requires json for faraday_middleware
18
+ # Ruby 1.8 requires json
23
19
  spec.add_dependency 'json', ['~> 1.7'] if RUBY_VERSION < "1.9"
24
20
 
25
21
  spec.add_development_dependency('rake')
@@ -26,11 +26,11 @@ module AnalyticsRuby
26
26
 
27
27
  @queue = Queue.new
28
28
  @secret = options[:secret]
29
- @max_queue_size = options[:max_queue_size] || AnalyticsRuby::Defaults::Queue::MAX_SIZE
29
+ @max_queue_size = options[:max_queue_size] || Defaults::Queue::MAX_SIZE
30
30
 
31
31
  check_secret
32
32
 
33
- @consumer = AnalyticsRuby::Consumer.new @queue, @secret, options
33
+ @consumer = Consumer.new @queue, @secret, options
34
34
  @thread = ConsumerThread.new { @consumer.run }
35
35
 
36
36
  at_exit do
@@ -23,7 +23,7 @@ module AnalyticsRuby
23
23
 
24
24
  @queue = queue
25
25
  @secret = secret
26
- @batch_size = options[:batch_size] || AnalyticsRuby::Defaults::Queue::BATCH_SIZE
26
+ @batch_size = options[:batch_size] || Defaults::Queue::BATCH_SIZE
27
27
  @on_error = options[:on_error] || Proc.new { |status, error| }
28
28
 
29
29
  @current_batch = []
@@ -54,7 +54,7 @@ module AnalyticsRuby
54
54
  end
55
55
  }
56
56
 
57
- req = AnalyticsRuby::Request.new
57
+ req = Request.new
58
58
  res = req.post @secret, @current_batch
59
59
  @on_error.call res.status, res.error unless res.status == 200
60
60
  @mutex.synchronize {
@@ -3,9 +3,10 @@ module AnalyticsRuby
3
3
  module Defaults
4
4
 
5
5
  module Request
6
- BASE_URL = 'https://api.segment.io' unless defined? AnalyticsRuby::Defaults::Request::BASE_URL
6
+ HOST = 'api.segment.io' unless defined? AnalyticsRuby::Defaults::Request::HOST
7
+ PORT = 443 unless defined? AnalyticsRuby::Defaults::Request::PORT
7
8
  PATH = '/v1/import' unless defined? AnalyticsRuby::Defaults::Request::PATH
8
- SSL = { :verify => false } unless defined? AnalyticsRuby::Defaults::Request::SSL
9
+ SSL = true unless defined? AnalyticsRuby::Defaults::Request::SSL
9
10
  HEADERS = { :accept => 'application/json' } unless defined? AnalyticsRuby::Defaults::Request::HEADERS
10
11
  RETRIES = 4 unless defined? AnalyticsRuby::Defaults::Request::RETRIES
11
12
  BACKOFF = 30.0 unless defined? AnalyticsRuby::Defaults::Request::BACKOFF
@@ -1,9 +1,9 @@
1
1
 
2
2
  require 'analytics-ruby/defaults'
3
3
  require 'analytics-ruby/response'
4
- require 'analytics-ruby/json'
5
- require 'faraday'
6
- require 'faraday_middleware'
4
+ require 'net/http'
5
+ require 'net/https'
6
+ require 'json'
7
7
 
8
8
  module AnalyticsRuby
9
9
 
@@ -12,19 +12,20 @@ module AnalyticsRuby
12
12
  # public: Creates a new request object to send analytics batch
13
13
  #
14
14
  def initialize(options = {})
15
-
16
- options[:url] ||= AnalyticsRuby::Defaults::Request::BASE_URL
17
- options[:ssl] ||= AnalyticsRuby::Defaults::Request::SSL
18
- options[:headers] ||= AnalyticsRuby::Defaults::Request::HEADERS
19
- @path = options[:path] || AnalyticsRuby::Defaults::Request::PATH
20
- @retries = options[:retries] || AnalyticsRuby::Defaults::Request::RETRIES
21
- @backoff = options[:backoff] || AnalyticsRuby::Defaults::Request::BACKOFF
22
-
23
- @conn = Faraday.new options do |faraday|
24
- faraday.request :json
25
- faraday.response :json, :content_type => /\bjson$/
26
- faraday.adapter Faraday.default_adapter
27
- end
15
+ options[:host] ||= Defaults::Request::HOST
16
+ options[:port] ||= Defaults::Request::PORT
17
+ options[:ssl] ||= Defaults::Request::SSL
18
+ options[:headers] ||= Defaults::Request::HEADERS
19
+ @path = options[:path] || Defaults::Request::PATH
20
+ @retries = options[:retries] || Defaults::Request::RETRIES
21
+ @backoff = options[:backoff] || Defaults::Request::BACKOFF
22
+
23
+ http = Net::HTTP.new(options[:host], options[:port])
24
+ http.use_ssl = options[:ssl]
25
+ http.read_timeout = 8
26
+ http.open_timeout = 4
27
+
28
+ @http = http
28
29
  end
29
30
 
30
31
  # public: Posts the secret and batch of messages to the API.
@@ -35,27 +36,26 @@ module AnalyticsRuby
35
36
  status, error = nil, nil
36
37
  remaining_retries = @retries
37
38
  backoff = @backoff
38
-
39
+ headers = { 'Content-Type' => 'application/json', 'accept' => 'application/json' }
39
40
  begin
40
- res = @conn.post do |req|
41
- req.options[:timeout] = 8
42
- req.options[:open_timeout] = 3
43
- req.url(@path)
44
- req.body = AnalyticsRuby::JSON::dump :secret => secret, :batch => batch
45
- end
46
- status = res.status
47
- error = res.body["error"]
41
+ payload = JSON.generate :secret => secret, :batch => batch
42
+ res = @http.request(Net::HTTP::Post.new(@path, headers), payload)
43
+ status = res.code.to_i
44
+ body = JSON.parse(res.body)
45
+ error = body["error"]
48
46
 
49
47
  rescue Exception => err
48
+ puts "err: #{err}"
50
49
  status = -1
51
50
  error = "Connection error: #{err}"
51
+ puts "retries: #{remaining_retries}"
52
52
  unless (remaining_retries -=1).zero?
53
53
  sleep(backoff)
54
54
  retry
55
55
  end
56
56
  end
57
57
 
58
- AnalyticsRuby::Response.new status, error
58
+ Response.new status, error
59
59
  end
60
60
  end
61
61
  end
@@ -1,3 +1,3 @@
1
1
  module AnalyticsRuby
2
- VERSION = '0.6.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -24,7 +24,7 @@ describe AnalyticsRuby::Consumer do
24
24
 
25
25
  it 'should not error if the endpoint is unreachable' do
26
26
 
27
- Faraday::Connection.any_instance.stub(:post).and_raise(Exception)
27
+ Net::HTTP.any_instance.stub(:post).and_raise(Exception)
28
28
 
29
29
  queue = Queue.new
30
30
  queue << {}
@@ -33,7 +33,7 @@ describe AnalyticsRuby::Consumer do
33
33
 
34
34
  queue.should be_empty
35
35
 
36
- Faraday::Connection.any_instance.unstub(:post)
36
+ Net::HTTP.any_instance.unstub(:post)
37
37
  end
38
38
 
39
39
  it 'should execute the error handler if the request is invalid' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: analytics-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,62 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-20 00:00:00.000000000 Z
12
+ date: 2014-03-13 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: faraday
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0.8'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0.8'
30
- - !ruby/object:Gem::Dependency
31
- name: faraday_middleware
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0.8'
38
- - - <
39
- - !ruby/object:Gem::Version
40
- version: '0.10'
41
- type: :runtime
42
- prerelease: false
43
- version_requirements: !ruby/object:Gem::Requirement
44
- none: false
45
- requirements:
46
- - - ! '>='
47
- - !ruby/object:Gem::Version
48
- version: '0.8'
49
- - - <
50
- - !ruby/object:Gem::Version
51
- version: '0.10'
52
- - !ruby/object:Gem::Dependency
53
- name: multi_json
54
- requirement: !ruby/object:Gem::Requirement
55
- none: false
56
- requirements:
57
- - - ~>
58
- - !ruby/object:Gem::Version
59
- version: '1.0'
60
- type: :runtime
61
- prerelease: false
62
- version_requirements: !ruby/object:Gem::Requirement
63
- none: false
64
- requirements:
65
- - - ~>
66
- - !ruby/object:Gem::Version
67
- version: '1.0'
68
14
  - !ruby/object:Gem::Dependency
69
15
  name: rake
70
16
  requirement: !ruby/object:Gem::Requirement
@@ -103,14 +49,13 @@ executables: []
103
49
  extensions: []
104
50
  extra_rdoc_files: []
105
51
  files:
52
+ - analytics-ruby-0.6.0.gem
106
53
  - analytics-ruby.gemspec
107
54
  - Gemfile
108
- - Gemfile.lock
109
55
  - History.md
110
56
  - lib/analytics-ruby/client.rb
111
57
  - lib/analytics-ruby/consumer.rb
112
58
  - lib/analytics-ruby/defaults.rb
113
- - lib/analytics-ruby/json.rb
114
59
  - lib/analytics-ruby/request.rb
115
60
  - lib/analytics-ruby/response.rb
116
61
  - lib/analytics-ruby/util.rb
data/Gemfile.lock DELETED
@@ -1,35 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- analytics-ruby (0.5.4)
5
- faraday (>= 0.8)
6
- faraday_middleware (>= 0.8, < 0.10)
7
- multi_json (~> 1.0)
8
-
9
- GEM
10
- remote: http://rubygems.org/
11
- specs:
12
- diff-lcs (1.2.4)
13
- faraday (0.8.8)
14
- multipart-post (~> 1.2.0)
15
- faraday_middleware (0.9.0)
16
- faraday (>= 0.7.4, < 0.9)
17
- multi_json (1.7.7)
18
- multipart-post (1.2.0)
19
- rake (10.1.0)
20
- rspec (2.14.1)
21
- rspec-core (~> 2.14.0)
22
- rspec-expectations (~> 2.14.0)
23
- rspec-mocks (~> 2.14.0)
24
- rspec-core (2.14.4)
25
- rspec-expectations (2.14.0)
26
- diff-lcs (>= 1.1.3, < 2.0)
27
- rspec-mocks (2.14.2)
28
-
29
- PLATFORMS
30
- ruby
31
-
32
- DEPENDENCIES
33
- analytics-ruby!
34
- rake
35
- rspec
@@ -1,28 +0,0 @@
1
- require 'multi_json'
2
-
3
- module AnalyticsRuby
4
-
5
- # JSON Wrapper module adapted from
6
- # https://github.com/stripe/stripe-ruby/blob/master/lib/stripe/json.rb
7
- #
8
- # .dump was added in MultiJson 1.3
9
- module JSON
10
- if MultiJson.respond_to? :dump
11
- def self.dump(*args)
12
- MultiJson.dump(*args)
13
- end
14
-
15
- def self.load(*args)
16
- MultiJson.load(*args)
17
- end
18
- else
19
- def self.dump(*args)
20
- MultiJson.encode(*args)
21
- end
22
-
23
- def self.load(*args)
24
- MultiJson.decode(*args)
25
- end
26
- end
27
- end
28
- end