datasift 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README.md CHANGED
@@ -18,6 +18,10 @@ If you're using the source you'll need to install the dependencies.
18
18
 
19
19
  sudo gem install yajl-ruby rest-client
20
20
 
21
+ The library will use SSL connections by default. While we recommend using SSL
22
+ you may disable it if required by passing false as the third parameter when
23
+ creating a user, or by calling user.enableSSL(false) on the user object.
24
+
21
25
  Simple example
22
26
  --------------
23
27
 
@@ -52,6 +56,12 @@ more details.
52
56
  Changelog
53
57
  ---------
54
58
 
59
+ * v.1.4.0 Added SSL support (2012-05-15)
60
+
61
+ This is enabled by default and can be disabled by passing false as the third
62
+ parameter to the User constructor, or calling enableSSL(false) on the User
63
+ object.
64
+
55
65
  * v.1.3.1 Exposed compile failures when getting the stream hash (2012-04-20)
56
66
 
57
67
  * v.1.3.0 Improved error handling (2012-03-08)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.1
1
+ 1.4.0
data/config.yml CHANGED
@@ -1,2 +1,2 @@
1
- username: 3ft9
2
- api_key: 01af49102292e7cb67fef7eee30b81c2
1
+ username: YOUR_USERNAME_HERE
2
+ api_key: YOUR_API_KEY_HERE
data/datasift.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
17
17
 
18
18
  s.add_runtime_dependency('rest-client', '~> 1.6.3')
19
- s.add_runtime_dependency('yajl-ruby', '~> 0.8.2')
19
+ s.add_runtime_dependency('yajl-ruby', '~> 1.1.0')
20
20
  s.add_development_dependency('rdoc', '~> 0')
21
21
  s.add_development_dependency('shoulda', '~> 2.11.3')
22
22
  s.add_development_dependency('rspec', '~> 2.6.0')
@@ -27,9 +27,9 @@ module DataSift
27
27
  # * +params+ - The parameters to be passed along with the request.
28
28
  # * +username+ - The username for the Auth header
29
29
  # * +api_key+ - The API key for the Auth header
30
- def call(username, api_key, endpoint, params = {}, user_agent = 'DataSiftPHP/0.0')
30
+ def call(username, api_key, endpoint, params = {}, user_agent = 'DataSiftPHP/0.0', ssl = true)
31
31
  # Build the full endpoint URL
32
- url = 'http://' + User::API_BASE_URL + endpoint
32
+ url = 'http' + (ssl ? 's' : '') + '://' + User::API_BASE_URL + endpoint
33
33
 
34
34
  retval = {
35
35
  'response_code' => 500,
@@ -47,6 +47,7 @@ module DataSift
47
47
 
48
48
  # Parse the JSON response
49
49
  retval['data'] = Yajl::Parser.parse(res)
50
+
50
51
  # Rate limit headers
51
52
  if (res.headers[:x_ratelimit_limit])
52
53
  retval['rate_limit'] = res.headers[:x_ratelimit_limit]
@@ -45,7 +45,7 @@ module DataSift
45
45
  # * +params+ - The parameters to be passed along with the request.
46
46
  # * +username+ - The username for the Auth header
47
47
  # * +api_key+ - The API key for the Auth header
48
- def call(username, api_key, endpoint, params = {}, user_agent = 'DataSiftPHP/0.0')
48
+ def call(username, api_key, endpoint, params = {}, user_agent = 'DataSiftPHP/0.0', use_ssl = true)
49
49
  if !@response
50
50
  raise StandardError, 'Expected response not set in mock object'
51
51
  end
@@ -85,9 +85,10 @@ module DataSift
85
85
  onStop(@stop_reason)
86
86
  end
87
87
 
88
+ private
89
+
88
90
  def reconnect()
89
- uri = URI.parse('http://' + User::STREAM_BASE_URL + @definition.hash +
90
- '?username=' + CGI.escape(@user.username) + '&api_key=' + CGI.escape(@user.api_key))
91
+ uri = URI.parse('http' + (@user.use_ssl ? 's' : '') + '://' + User::STREAM_BASE_URL + @definition.hash)
91
92
 
92
93
  user_agent = @user.getUserAgent()
93
94
 
@@ -95,6 +96,7 @@ module DataSift
95
96
  request << "Host: #{uri.host}\r\n"
96
97
  request << "User-Agent: #{user_agent}\r\n"
97
98
  request << "Accept: */*\r\n"
99
+ request << "Auth: #{@user.username}:#{@user.api_key}\r\n"
98
100
  request << "\r\n"
99
101
 
100
102
  connection_delay = 0
@@ -107,7 +109,13 @@ module DataSift
107
109
  sleep(connection_delay) if connection_delay > 0
108
110
 
109
111
  begin
110
- @socket = TCPSocket.new(uri.host, uri.port)
112
+ @raw_socket = TCPSocket.new(uri.host, uri.port)
113
+ if @user.use_ssl
114
+ @socket = OpenSSL::SSL::SSLSocket.new(@raw_socket)
115
+ @socket.connect
116
+ else
117
+ @socket = @raw_socket
118
+ end
111
119
 
112
120
  @socket.write(request)
113
121
  @response_head = {}
@@ -130,10 +138,12 @@ module DataSift
130
138
  end
131
139
  end
132
140
 
133
- if @response_head[:code] == 200
141
+ if @response_head[:code].nil?
142
+ raise StreamError, 'Socket connection refused'
143
+ elsif @response_head[:code] == 200
134
144
  # Success!
135
145
  @state = StreamConsumer::STATE_RUNNING
136
- elsif @response_head[:code] >= 400 && @response_head[:code] < 500 && @response_head[:code] != 420
146
+ elsif @response_head[:code] >= 399 && @response_head[:code] < 500 && @response_head[:code] != 420
137
147
  line = ''
138
148
  while !@socket.eof? && line.length < 10
139
149
  line = @socket.gets
@@ -153,20 +163,21 @@ module DataSift
153
163
  raise StreamError, 'Connection refused: ' + @response_head[:code] + ' ' + @response_head[:msg]
154
164
  end
155
165
  end
156
- #rescue
157
- # if connection_delay == 0
158
- # connection_delay = 1
159
- # elsif connection_delay <= 16
160
- # connection_delay += 1
161
- # else
162
- # raise StreamError, 'Connection failed due to a network error'
163
- # end
166
+ rescue
167
+ if connection_delay == 0
168
+ connection_delay = 1
169
+ elsif connection_delay <= 16
170
+ connection_delay += 1
171
+ else
172
+ raise StreamError, 'Connection failed due to a network error'
173
+ end
164
174
  end
165
175
  end while @state != StreamConsumer::STATE_RUNNING
166
176
  end
167
177
 
168
178
  def disconnect()
169
179
  @socket.close if !@socket.nil? and !@socket.closed?
180
+ @raw_socket.close if !@raw_socket.nil? and !@raw_socket.closed?
170
181
  end
171
182
 
172
183
  end
data/lib/DataSift/user.rb CHANGED
@@ -19,11 +19,11 @@ module DataSift
19
19
  # provides factory methods for all of the functionality in the API.
20
20
  #
21
21
  class User
22
- USER_AGENT = 'DataSiftRuby/1.1.0';
22
+ USER_AGENT = 'DataSiftRuby/' + File.open(File.dirname(File.dirname(File.dirname(__FILE__))) + '/VERSION').first;
23
23
  API_BASE_URL = 'api.datasift.com/';
24
24
  STREAM_BASE_URL = 'stream.datasift.com/';
25
25
 
26
- attr_reader :username, :api_key, :rate_limit, :rate_limit_remaining, :api_client
26
+ attr_reader :username, :api_key, :rate_limit, :rate_limit_remaining, :api_client, :use_ssl
27
27
 
28
28
  # Constructor. A username and API key are required when constructing an
29
29
  # instance of this class.
@@ -31,7 +31,7 @@ module DataSift
31
31
  #
32
32
  # * +username+ - The user's username
33
33
  # * +api_key+ - The user's API key
34
- def initialize(username, api_key)
34
+ def initialize(username, api_key, use_ssl = true)
35
35
  username.strip!
36
36
  api_key.strip!
37
37
 
@@ -41,6 +41,7 @@ module DataSift
41
41
  @api_key = api_key
42
42
  @rate_limit = -1;
43
43
  @rate_limit_remaining = -1
44
+ @use_ssl = use_ssl
44
45
  end
45
46
 
46
47
  # Creates and returns a definition object.
@@ -87,6 +88,11 @@ module DataSift
87
88
  @api_client = client
88
89
  end
89
90
 
91
+ # Sets whether to use SSL for API and stream communication
92
+ def enableSSL(use_ssl = true)
93
+ @use_ssl = use_ssl
94
+ end
95
+
90
96
  # Make a call to a DataSift API endpoint.
91
97
  # === Parameters
92
98
  #
@@ -97,7 +103,7 @@ module DataSift
97
103
  @api_client = ApiClient.new()
98
104
  end
99
105
 
100
- res = @api_client.call(@username, @api_key, endpoint, params)
106
+ res = @api_client.call(@username, @api_key, endpoint, params, getUserAgent(), @use_ssl)
101
107
 
102
108
  # Set up the return value
103
109
  retval = res['data']
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datasift
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 3
9
- - 1
10
- version: 1.3.1
8
+ - 4
9
+ - 0
10
+ version: 1.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - DataSift
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-20 00:00:00 Z
18
+ date: 2012-05-15 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rest-client
@@ -41,12 +41,12 @@ dependencies:
41
41
  requirements:
42
42
  - - ~>
43
43
  - !ruby/object:Gem::Version
44
- hash: 59
44
+ hash: 19
45
45
  segments:
46
+ - 1
47
+ - 1
46
48
  - 0
47
- - 8
48
- - 2
49
- version: 0.8.2
49
+ version: 1.1.0
50
50
  type: :runtime
51
51
  version_requirements: *id002
52
52
  - !ruby/object:Gem::Dependency
@@ -106,6 +106,7 @@ extra_rdoc_files: []
106
106
 
107
107
  files:
108
108
  - .gitignore
109
+ - Gemfile
109
110
  - LICENSE
110
111
  - README.md
111
112
  - Rakefile