bwapi 10.0.0.pre.516 → 10.0.0.pre.530

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MWY0Y2EyMjMxNDA0NjJiOGZjMDkwYmY3YmViYTdlNDliMzVmMGU3OA==
4
+ YzE4OTk0YmE0MTQ5OTY5MWZiMzY0NDhmNDQwMzg5ODk1NTk5Nzg2OA==
5
5
  data.tar.gz: !binary |-
6
- OGQ2NzAxZTFmMTEyNmMyNjhiMTE0NzNhOGU5ZTdiNzA2NWE5MDNmMw==
6
+ NzM1M2ViZTc0ZGJhMzI3ZWQ4NTY3MzQwMmI3NzM2ZjY1YWFjMzI5Nw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MzJkODcwZDBkZWRkNmNkOTc2NThjZWMwNTJmOTU3MjQyYzc5OTY3YTA2OWU4
10
- MDQyYWE3MDMzOTUxYmMzZjI2YWY2YTM4MjMwNjljNmVmMGNhYWVkNzU5Mjdj
11
- NTI1NGMyYTJjZjg2NjEzZjI5MWJkYzNhMTNhY2I2MmQ5MzNhZTY=
9
+ Y2NmYmZmODIyMzM2ZjUzNDJlZTcwZmUwYzA2MzI1MjY5Yzk3YmFlM2UwYWQy
10
+ ZWJmNDg3NDE1N2FiZDBmNDUxNGQyOGIxZDhkNzM5ODI4YzgwYWM1ODQyYjYy
11
+ MGY5YmRkY2U2MjE2YzhmZGE3OTUwMTNmYjc5NmU4NzRlODAxNWY=
12
12
  data.tar.gz: !binary |-
13
- YjkwMjk3ODRiYTlhMWU0YTM1ZDZlNjQ5MDU1MjFkNTM3YzhmY2FlZGVlODdh
14
- OWQ2ZmNlMmFlZGI0ZTdjYTA0YjE0N2Q4N2UyYjI3MmNiMTg2YTJhZTVmMmIz
15
- NzJlZjIwYWRiZDI2NjQ5YjA3Mjc2OGQwZDAxMjcwNjgzYzI2MmI=
13
+ YjY2YzQwZDhiN2RkM2QzNDg1NmVmMzFhZjQyMTY1NWExMzkyNWY4NTA1YmY3
14
+ Y2E1YzkwNjQ0NWMzNGNiNDk1N2ZmODFiMWM1MjBiMzNhODM1ZmVkZGZiMGY3
15
+ YWQ4OTAyMTY0MTU5NTMxYmZhNzFkZjVlNDRiZGVhYmNiNzE0NWY=
data/lib/bwapi/client.rb CHANGED
@@ -65,11 +65,19 @@ module BWAPI
65
65
  end
66
66
  end
67
67
 
68
+ # Check is access token has expired
69
+ #
70
+ # @return [Boolean] access token expiry status
71
+ def access_token_expired?
72
+ return true if @access_token.nil? || @access_token_expiry.nil?
73
+ seconds_until_access_token_expires <= 0
74
+ end
75
+
68
76
  # Check if user is authenicated
69
77
  #
70
78
  # @return [Boolean] Authenticated status
71
79
  def authenticated?
72
- @access_token ? true : false
80
+ (@access_token && !access_token_expired?) ? true : false
73
81
  end
74
82
 
75
83
  # Check if user is a brandwatch-application-client type
@@ -115,5 +123,14 @@ module BWAPI
115
123
  reset_connection
116
124
  @verify_ssl = value
117
125
  end
126
+
127
+ private
128
+
129
+ # Returns the number of seconds until the access token expires
130
+ #
131
+ # @return [Integer] seconds until expiry
132
+ def seconds_until_access_token_expires
133
+ DateTime.parse(@access_token_expiry).to_time.to_i - Time.now.to_i
134
+ end
118
135
  end
119
136
  end
@@ -61,7 +61,8 @@ module BWAPI
61
61
  # @param opts [Hash] options hash of parameters
62
62
  def oauth_request(opts = {})
63
63
  response = post('oauth/token', opts)
64
- self.access_token = response['access_token']
64
+ self.access_token = response['access_token']
65
+ self.access_token_expiry = Time.at(Time.now.to_i + response['expires_in']).iso8601
65
66
  self.refresh_token = response['refresh_token'] if application_client?
66
67
  response
67
68
  end
@@ -1,8 +1,8 @@
1
1
  module BWAPI
2
2
  # Configuration module
3
3
  module Configuration
4
- attr_accessor :access_token, :adapter, :api_endpoint, :client_id, :debug, :grant_type,
5
- :logger, :performance, :refresh_token, :user_agent, :username, :verify_ssl
4
+ attr_accessor :access_token, :access_token_expiry, :adapter, :api_endpoint, :client_id, :debug,
5
+ :grant_type, :logger, :performance, :refresh_token, :user_agent, :username, :verify_ssl
6
6
 
7
7
  attr_writer :client_secret, :password
8
8
 
@@ -11,6 +11,7 @@ module BWAPI
11
11
  def keys
12
12
  @keys ||= [
13
13
  :access_token,
14
+ :access_token_expiry,
14
15
  :adapter,
15
16
  :api_endpoint,
16
17
  :client_id,
data/lib/bwapi/default.rb CHANGED
@@ -26,8 +26,12 @@ module BWAPI
26
26
  ENV['BWAPI_ACCESS_TOKEN']
27
27
  end
28
28
 
29
+ def access_token_expiry
30
+ nil
31
+ end
32
+
29
33
  def adapter
30
- ENV['BWAPI_ADAPTER'] || ADAPTER
34
+ ENV['BWAPI_ADAPTER'].is_a?(String) ? ENV['BWAPI_ADAPTER'].to_sym : ADAPTER
31
35
  end
32
36
 
33
37
  def api_endpoint
@@ -54,7 +58,7 @@ module BWAPI
54
58
  end
55
59
 
56
60
  def debug
57
- ENV['BWAPI_DEBUG'] || false
61
+ ENV['BWAPI_DEBUG'] == 'true' ? true : false
58
62
  end
59
63
 
60
64
  def grant_type
@@ -86,7 +90,7 @@ module BWAPI
86
90
  end
87
91
 
88
92
  def verify_ssl
89
- ENV['BWAPI_VERIFY_SSL'] || false
93
+ ENV['BWAPI_VERIFY_SSL'] == 'true' ? true : false
90
94
  end
91
95
  end
92
96
  end
@@ -1,9 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  describe BWAPI::Client do
4
- before do
5
- BWAPI.reset
6
- end
4
+ before { BWAPI.reset }
7
5
 
8
6
  describe 'when called' do
9
7
  it 'should be a client class instance' do
@@ -12,16 +10,37 @@ describe BWAPI::Client do
12
10
  end
13
11
 
14
12
  describe '.authenticated?' do
15
- it 'returns true when authenticated' do
13
+ it 'returns false when not authenticated' do
16
14
  expect(BWAPI::Client.new.authenticated?).to eql(false)
17
15
  end
18
16
 
19
- it 'returns false when not authenticated' do
20
- bw = BWAPI::Client.new access_token: 'abcdef-ghijkl-123456-789012'
17
+ it 'returns true when authenticated' do
18
+ bw = BWAPI::Client.new access_token: 'abcdef-ghijkl-123456-789012', access_token_expiry: (Time.now + 3600).iso8601
21
19
  expect(bw.authenticated?).to eql(true)
22
20
  end
23
21
  end
24
22
 
23
+ describe '.access_token_expired?' do
24
+ it 'returns true if access_token is set to nil' do
25
+ expect(BWAPI::Client.new.access_token_expired?).to eql(true)
26
+ end
27
+
28
+ it 'returns true if access_token_expiry is set to nil' do
29
+ bw = BWAPI::Client.new access_token: 'abcdef-ghijkl-123456-789012'
30
+ expect(bw.access_token_expired?).to eql(true)
31
+ end
32
+
33
+ it 'returns true if access token has expired' do
34
+ bw = BWAPI::Client.new access_token: 'abcdef-ghijkl-123456-789012', access_token_expiry: (Time.now - 3600).iso8601
35
+ expect(bw.access_token_expired?).to eql(true)
36
+ end
37
+
38
+ it 'returns false if access token has not expired' do
39
+ bw = BWAPI::Client.new access_token: 'abcdef-ghijkl-123456-789012', access_token_expiry: (Time.now + 3600).iso8601
40
+ expect(bw.access_token_expired?).to eql(false)
41
+ end
42
+ end
43
+
25
44
  describe '.application_client?' do
26
45
  it 'returns true when client is a brandwatch-application-client' do
27
46
  bw = BWAPI::Client.new client_id: 'brandwatch-application-client'
@@ -45,167 +64,10 @@ describe BWAPI::Client do
45
64
  end
46
65
  end
47
66
 
48
- describe 'configuration' do
49
- describe 'instance variables' do
50
- it 'should create a api_endpoint instance variable' do
51
- expect(BWAPI::Client.new.respond_to?(:api_endpoint)).to eq(true)
52
- end
53
-
54
- it 'should create a user_agent instance variable' do
55
- expect(BWAPI::Client.new.respond_to?(:user_agent)).to eq(true)
56
- end
57
-
58
- it 'should create a adapter instance variable' do
59
- expect(BWAPI::Client.new.respond_to?(:adapter)).to eq(true)
60
- end
61
-
62
- it 'should create a username instance variable' do
63
- expect(BWAPI::Client.new.respond_to?(:username)).to eq(true)
64
- end
65
-
66
- it 'should create a password instance variable which cannot be accessed' do
67
- expect(BWAPI::Client.new.respond_to?(:password)).to eq(false)
68
- end
69
-
70
- it 'should create a grant_type instance variable' do
71
- expect(BWAPI::Client.new.respond_to?(:grant_type)).to eq(true)
72
- end
73
-
74
- it 'should create a access_token instance variable' do
75
- expect(BWAPI::Client.new.respond_to?(:access_token)).to eq(true)
76
- end
77
-
78
- it 'should create a refresh_token instance variable' do
79
- expect(BWAPI::Client.new.respond_to?(:refresh_token)).to eq(true)
80
- end
81
-
82
- it 'should create a client_id instance variable' do
83
- expect(BWAPI::Client.new.respond_to?(:client_id)).to eq(true)
84
- end
85
-
86
- it 'should create a client_secret instance variable which cannot accessed' do
87
- expect(BWAPI::Client.new.respond_to?(:client_secret)).to eq(false)
88
- end
89
-
90
- it 'should create a logger instance variable' do
91
- expect(BWAPI::Client.new.respond_to?(:logger)).to eq(true)
92
- end
93
- end
94
-
95
- describe 'configuration values' do
96
- describe 'default_adapter' do
97
- it 'should have a default default_adapter value' do
98
- expect(BWAPI::Client.new.adapter).to eql(:net_http)
99
- end
100
-
101
- it 'should allow a user to set a default_adapter value' do
102
- bw = BWAPI::Client.new adapter: 'custom_adapter'
103
- expect(bw.adapter).to eql('custom_adapter')
104
- end
105
- end
106
-
107
- describe 'user_agent' do
108
- it 'should have a default user_agent value' do
109
- expect(BWAPI::Client.new.user_agent).to eql("BWAPI Ruby Gem #{BWAPI::VERSION}")
110
- end
111
-
112
- it 'should allow a user to set a user_agent value' do
113
- bw = BWAPI::Client.new user_agent: 'custom_user_agent'
114
- expect(bw.user_agent).to eql('custom_user_agent')
115
- end
116
- end
117
-
118
- describe 'api_endpoint' do
119
- it 'should have a default api_endpoint value' do
120
- expect(BWAPI::Client.new.api_endpoint).to eql('https://newapi.brandwatch.com/')
121
- end
122
-
123
- it 'should allow a user to set a api_endpoint value' do
124
- bw = BWAPI::Client.new api_endpoint: 'http://newapi.custom.brandwatch.com'
125
- expect(bw.api_endpoint).to eql('http://newapi.custom.brandwatch.com')
126
- end
127
- end
128
-
129
- describe 'client_id' do
130
- it 'should have a default client_id value' do
131
- expect(BWAPI::Client.new.client_id).to eql('brandwatch-api-client')
132
- end
133
-
134
- it 'should allow a user to set a client_id value' do
135
- bw = BWAPI::Client.new client_id: 'custom_client_id'
136
- expect(bw.client_id).to eql('custom_client_id')
137
- end
138
- end
139
-
140
- describe 'username' do
141
- it 'should have a default value of nil for username' do
142
- expect(BWAPI::Client.new.username).to eql(nil)
143
- end
144
-
145
- it 'should allow a user to set a username value' do
146
- bw = BWAPI::Client.new username: 'jonathan@brandwatch.com'
147
- expect(bw.username).to eql('jonathan@brandwatch.com')
148
- end
149
- end
150
-
151
- describe 'password' do
152
- it 'should allow a user to set a password value' do
153
- bw = BWAPI::Client.new password: 'pa55w0rd'
154
- expect(bw.instance_eval { @password }).to eql('pa55w0rd')
155
- end
156
- end
157
-
158
- describe 'grant_type' do
159
- it 'should have a default value of api-password for grant_type' do
160
- expect(BWAPI::Client.new.grant_type).to eql('api-password')
161
- end
162
-
163
- it 'should allow a user to set a grant_type value' do
164
- bw = BWAPI::Client.new grant_type: 'custom_grant_type'
165
- expect(bw.grant_type).to eql('custom_grant_type')
166
- end
167
- end
168
-
169
- describe 'access_token' do
170
- it 'should have a default value of nil for access_token' do
171
- expect(BWAPI::Client.new.access_token).to eql(nil)
172
- end
173
-
174
- it 'should allow a user to set a access_token value' do
175
- bw = BWAPI::Client.new access_token: 'abcdef-ghijkl-123456-789012'
176
- expect(bw.access_token).to eql('abcdef-ghijkl-123456-789012')
177
- end
178
- end
179
-
180
- describe 'refresh_token' do
181
- it 'should have a default value of nil for refresh_token' do
182
- expect(BWAPI::Client.new.refresh_token).to eql(nil)
183
- end
184
-
185
- it 'should allow a user to set a refresh_token value' do
186
- bw = BWAPI::Client.new refresh_token: 'abcdef-ghijkl-123456-789012'
187
- expect(bw.refresh_token).to eql('abcdef-ghijkl-123456-789012')
188
- end
189
- end
190
-
191
- describe 'client_secret' do
192
- it 'should allow a user to set a client_secret value' do
193
- bw = BWAPI::Client.new client_secret: 'custom_client_secret'
194
- expect(bw.instance_eval { @client_secret }).to eql('custom_client_secret')
195
- end
196
- end
197
-
198
- describe 'logger' do
199
- it 'should have a default value of nil for logger' do
200
- expect(BWAPI::Client.new.logger).to eql(nil)
201
- end
202
-
203
- it 'should allow a user to set a logger value' do
204
- logger = Object.new
205
- bw = BWAPI::Client.new logger: logger
206
- expect(bw.logger).to eql(logger)
207
- end
208
- end
67
+ describe '.seconds_until_access_token_expires' do
68
+ it 'should return the number of seconds until the access token expires' do
69
+ bw = BWAPI::Client.new access_token_expiry: (Time.now + 3600).iso8601
70
+ expect(bw.send(:seconds_until_access_token_expires)).to eql(3600)
209
71
  end
210
72
  end
211
73
  end
@@ -0,0 +1,499 @@
1
+ require 'helper'
2
+ require 'logger'
3
+
4
+ describe BWAPI::Configuration do
5
+ let(:bwapi) { BWAPI::Client.new }
6
+
7
+ describe 'instance variables' do
8
+ it 'should create a access_token instance variable' do
9
+ expect(bwapi.respond_to?(:access_token)).to eq(true)
10
+ end
11
+
12
+ it 'should create a access_token_expiry instance variable' do
13
+ expect(bwapi.respond_to?(:access_token_expiry)).to eq(true)
14
+ end
15
+
16
+ it 'should create a adapter instance variable' do
17
+ expect(bwapi.respond_to?(:adapter)).to eq(true)
18
+ end
19
+
20
+ it 'should create a api_endpoint instance variable' do
21
+ expect(bwapi.respond_to?(:api_endpoint)).to eq(true)
22
+ end
23
+
24
+ it 'should create a client_id instance variable' do
25
+ expect(bwapi.respond_to?(:client_id)).to eq(true)
26
+ end
27
+
28
+ it 'should create a client_secret instance variable which cannot accessed' do
29
+ expect(bwapi.respond_to?(:client_secret)).to eq(false)
30
+ end
31
+
32
+ it 'should create a debug instance variable' do
33
+ expect(bwapi.respond_to?(:debug)).to eq(true)
34
+ end
35
+
36
+ it 'should create a grant_type instance variable' do
37
+ expect(bwapi.respond_to?(:grant_type)).to eq(true)
38
+ end
39
+
40
+ it 'should create a logger instance variable' do
41
+ expect(bwapi.respond_to?(:logger)).to eq(true)
42
+ end
43
+
44
+ it 'should create a password instance variable which cannot be accessed' do
45
+ expect(bwapi.respond_to?(:password)).to eq(false)
46
+ end
47
+
48
+ it 'should create a performance instance variable' do
49
+ expect(bwapi.respond_to?(:performance)).to eq(true)
50
+ end
51
+
52
+ it 'should create a refresh_token instance variable' do
53
+ expect(bwapi.respond_to?(:refresh_token)).to eq(true)
54
+ end
55
+
56
+ it 'should create a user_agent instance variable' do
57
+ expect(bwapi.respond_to?(:user_agent)).to eq(true)
58
+ end
59
+
60
+ it 'should create a username instance variable' do
61
+ expect(bwapi.respond_to?(:username)).to eq(true)
62
+ end
63
+
64
+ it 'should create a logger instance variable' do
65
+ expect(bwapi.respond_to?(:logger)).to eq(true)
66
+ end
67
+
68
+ it 'should create a verify_ssl instance variable' do
69
+ expect(bwapi.respond_to?(:verify_ssl)).to eq(true)
70
+ end
71
+ end
72
+
73
+ describe 'default and custom values' do
74
+ before { bwapi.reset }
75
+
76
+ describe 'access_token' do
77
+ it 'should have a default value of nil for access_token' do
78
+ expect(bwapi.access_token).to eql(nil)
79
+ end
80
+
81
+ it 'should allow a user to set a access_token value' do
82
+ bwapi.access_token = 'abcdef-ghijkl-123456-789012'
83
+ expect(bwapi.access_token).to eql('abcdef-ghijkl-123456-789012')
84
+ end
85
+ end
86
+
87
+ describe 'access_token_expiry' do
88
+ it 'should have a default value of nil for access_token_expiry' do
89
+ expect(bwapi.access_token_expiry).to eql(nil)
90
+ end
91
+
92
+ it 'should allow a user to set a access_token_expiry value' do
93
+ bwapi.access_token_expiry = '2015-01-08T16:13:03-08:00'
94
+ expect(bwapi.access_token_expiry).to eql('2015-01-08T16:13:03-08:00')
95
+ end
96
+ end
97
+
98
+ describe 'adapter' do
99
+ it 'should have a default adapter value' do
100
+ expect(bwapi.adapter).to eql(:net_http)
101
+ end
102
+
103
+ it 'should allow a user to set a default_adapter value' do
104
+ bwapi.adapter = 'custom_adapter'
105
+ expect(bwapi.adapter).to eql('custom_adapter')
106
+ end
107
+ end
108
+
109
+ describe 'api_endpoint' do
110
+ it 'should have a default api_endpoint value' do
111
+ expect(bwapi.api_endpoint).to eql('https://newapi.brandwatch.com/')
112
+ end
113
+
114
+ it 'should allow a user to set a api_endpoint value' do
115
+ bwapi.api_endpoint = 'http://newapi.custom.brandwatch.com'
116
+ expect(bwapi.api_endpoint).to eql('http://newapi.custom.brandwatch.com')
117
+ end
118
+ end
119
+
120
+ describe 'client_id' do
121
+ it 'should have a default client_id value' do
122
+ expect(bwapi.client_id).to eql('brandwatch-api-client')
123
+ end
124
+
125
+ it 'should allow a user to set a client_id value' do
126
+ bwapi.client_id = 'custom_client_id'
127
+ expect(bwapi.client_id).to eql('custom_client_id')
128
+ end
129
+ end
130
+
131
+ describe 'client_secret' do
132
+ it 'should allow a user to set a client_secret value' do
133
+ bwapi.client_secret = 'custom_client_secret'
134
+ expect(bwapi.instance_eval { @client_secret }).to eql('custom_client_secret')
135
+ end
136
+ end
137
+
138
+ describe 'debug' do
139
+ it 'should have a default debug value' do
140
+ expect(bwapi.debug).to eql(false)
141
+ end
142
+
143
+ it 'should allow a user to set a debug value' do
144
+ bwapi.debug = true
145
+ expect(bwapi.debug).to eql(true)
146
+ end
147
+ end
148
+
149
+ describe 'grant_type' do
150
+ it 'should have a default value of api-password for grant_type' do
151
+ expect(bwapi.grant_type).to eql('api-password')
152
+ end
153
+
154
+ it 'should allow a user to set a grant_type value' do
155
+ bwapi.grant_type = 'custom_grant_type'
156
+ expect(bwapi.grant_type).to eql('custom_grant_type')
157
+ end
158
+ end
159
+
160
+ describe 'logger' do
161
+ it 'should have a default value of nil for logger' do
162
+ expect(bwapi.logger).to eql(nil)
163
+ end
164
+
165
+ it 'should allow a user to set a logger value' do
166
+ logger = Logger.new(STDOUT)
167
+ bwapi.logger = logger
168
+ expect(bwapi.logger).to eql(logger)
169
+ end
170
+ end
171
+
172
+ describe 'password' do
173
+ it 'should allow a user to set a password value' do
174
+ bwapi.password = 'pa55w0rd'
175
+ expect(bwapi.instance_eval { @password }).to eql('pa55w0rd')
176
+ end
177
+ end
178
+
179
+ describe 'performance' do
180
+ it 'should have a default performance value' do
181
+ expect(bwapi.performance).to eql({})
182
+ end
183
+ end
184
+
185
+ describe 'refresh_token' do
186
+ it 'should have a default value of nil for refresh_token' do
187
+ expect(bwapi.refresh_token).to eql(nil)
188
+ end
189
+
190
+ it 'should allow a user to set a refresh_token value' do
191
+ bwapi.refresh_token = 'abcdef-ghijkl-123456-789012'
192
+ expect(bwapi.refresh_token).to eql('abcdef-ghijkl-123456-789012')
193
+ end
194
+ end
195
+
196
+ describe 'user_agent' do
197
+ it 'should have a default user_agent value' do
198
+ expect(bwapi.user_agent).to eql("BWAPI Ruby Gem #{BWAPI::VERSION}")
199
+ end
200
+
201
+ it 'should allow a user to set a user_agent value' do
202
+ bwapi.user_agent = 'custom_user_agent'
203
+ expect(bwapi.user_agent).to eql('custom_user_agent')
204
+ end
205
+ end
206
+
207
+ describe 'username' do
208
+ it 'should have a default value of nil for username' do
209
+ expect(bwapi.username).to eql(nil)
210
+ end
211
+
212
+ it 'should allow a user to set a username value' do
213
+ bwapi.username = 'jonathan@brandwatch.com'
214
+ expect(bwapi.username).to eql('jonathan@brandwatch.com')
215
+ end
216
+ end
217
+
218
+ describe 'verify_ssl' do
219
+ it 'should have a default verify_ssl value' do
220
+ expect(bwapi.verify_ssl).to eql(false)
221
+ end
222
+
223
+ it 'should allow a user to set a verify_ssl value' do
224
+ bwapi.verify_ssl = true
225
+ expect(bwapi.verify_ssl).to eql(true)
226
+ end
227
+ end
228
+ end
229
+
230
+ describe '.keys' do
231
+ it 'should return the correct list of keys' do
232
+ expect(BWAPI::Configuration.keys).to eql([
233
+ :access_token,
234
+ :access_token_expiry,
235
+ :adapter,
236
+ :api_endpoint,
237
+ :client_id,
238
+ :client_secret,
239
+ :connection_options,
240
+ :debug,
241
+ :grant_type,
242
+ :logger,
243
+ :password,
244
+ :performance,
245
+ :refresh_token,
246
+ :user_agent,
247
+ :username,
248
+ :verify_ssl
249
+ ])
250
+ end
251
+ end
252
+
253
+ describe '.configure' do
254
+ before do
255
+ bwapi.configure do |c|
256
+ c.access_token = '1234-5678-9010-1112'
257
+ c.access_token_expiry = '2015-01-08 14:09:16 -0800'
258
+ c.adapter = :test
259
+ c.api_endpoint = 'http://newapi.test.brandwatch.com'
260
+ c.client_id = 'brandwatch-test-client'
261
+ c.client_secret = 'test'
262
+ c.debug = true
263
+ c.grant_type = 'test-grant'
264
+ c.logger = Logger.new(STDOUT)
265
+ c.password = 'password123'
266
+ c.refresh_token = '1234-5678-9010-1112'
267
+ c.user_agent = 'Test User Agent'
268
+ c.username = 'testing@brandwatch.com'
269
+ c.verify_ssl = true
270
+ end
271
+ end
272
+
273
+ after { bwapi.reset }
274
+
275
+ it 'should set the configured access_token' do
276
+ expect(bwapi.access_token).to eql('1234-5678-9010-1112')
277
+ end
278
+
279
+ it 'should set the configured access_token_expiry' do
280
+ expect(bwapi.access_token_expiry).to eql('2015-01-08 14:09:16 -0800')
281
+ end
282
+
283
+ it 'should set the configured adapter' do
284
+ expect(bwapi.adapter).to eql(:test)
285
+ end
286
+
287
+ it 'should set the configured api_endpoint' do
288
+ expect(bwapi.api_endpoint).to eql('http://newapi.test.brandwatch.com')
289
+ end
290
+
291
+ it 'should set the configured client_id' do
292
+ expect(bwapi.client_id).to eql('brandwatch-test-client')
293
+ end
294
+
295
+ it 'should set the configured client_secret' do
296
+ expect(bwapi.instance_variable_get(:@client_secret)).to eql('test')
297
+ end
298
+
299
+ it 'should set the configured debug flag' do
300
+ expect(bwapi.debug).to eql(true)
301
+ end
302
+
303
+ it 'should set the configured grant type' do
304
+ expect(bwapi.grant_type).to eql('test-grant')
305
+ end
306
+
307
+ it 'should set the configured logger' do
308
+ expect(bwapi.logger).to be_an_instance_of Logger
309
+ end
310
+
311
+ it 'should set the configured password' do
312
+ expect(bwapi.instance_variable_get(:@password)).to eql('password123')
313
+ end
314
+
315
+ it 'should set the configured refresh_token' do
316
+ expect(bwapi.refresh_token).to eql('1234-5678-9010-1112')
317
+ end
318
+
319
+ it 'should set the configured user agent' do
320
+ expect(bwapi.user_agent).to eql('Test User Agent')
321
+ end
322
+
323
+ it 'should set the configured username' do
324
+ expect(bwapi.username).to eql('testing@brandwatch.com')
325
+ end
326
+
327
+ it 'should set the configured verify_ssl flag' do
328
+ expect(bwapi.verify_ssl).to eql(true)
329
+ end
330
+ end
331
+
332
+ describe '.reset' do
333
+ before { bwapi.reset }
334
+
335
+ it 'should reset the access token value' do
336
+ expect(bwapi.access_token).to eql(nil)
337
+ end
338
+
339
+ it 'should reset the access token expiry value' do
340
+ expect(bwapi.access_token_expiry).to eql(nil)
341
+ end
342
+
343
+ it 'should reset the adapter value' do
344
+ expect(bwapi.adapter).to eql(:net_http)
345
+ end
346
+
347
+ it 'should reset the api_endpoint value' do
348
+ expect(bwapi.api_endpoint).to eql('https://newapi.brandwatch.com/')
349
+ end
350
+
351
+ it 'should reset the client_id value' do
352
+ expect(bwapi.client_id).to eql('brandwatch-api-client')
353
+ end
354
+
355
+ it 'should reset the client_secret value' do
356
+ expect(bwapi.instance_variable_get(:@client_secret)).to eql(nil)
357
+ end
358
+
359
+ it 'should reset the connection value' do
360
+ expect(bwapi.instance_variable_get(:@connection)).to eql(nil)
361
+ end
362
+
363
+ it 'should reset the debug value' do
364
+ expect(bwapi.debug).to eql(false)
365
+ end
366
+
367
+ it 'should reset the grant_type value' do
368
+ expect(bwapi.grant_type).to eql('api-password')
369
+ end
370
+
371
+ it 'should reset the logger value' do
372
+ expect(bwapi.logger).to eql(nil)
373
+ end
374
+
375
+ it 'should reset the password value' do
376
+ expect(bwapi.instance_variable_get(:@password)).to eql(nil)
377
+ end
378
+
379
+ it 'should reset the performance value' do
380
+ expect(bwapi.performance).to eql({})
381
+ end
382
+
383
+ it 'should reset the refresh_token value' do
384
+ expect(bwapi.refresh_token).to eql(nil)
385
+ end
386
+
387
+ it 'should reset the user_agent value' do
388
+ expect(bwapi.user_agent).to eql('BWAPI Ruby Gem 10.0.0')
389
+ end
390
+
391
+ it 'should reset the username value' do
392
+ expect(bwapi.username).to eql(nil)
393
+ end
394
+
395
+ it 'should reset the verify_ssl value' do
396
+ expect(bwapi.verify_ssl).to eql(false)
397
+ end
398
+ end
399
+
400
+ describe '.destroy' do
401
+ before { bwapi.destroy }
402
+
403
+ it 'should reset the access token value' do
404
+ expect(bwapi.access_token).to eql(nil)
405
+ end
406
+
407
+ it 'should reset the access token expiry value' do
408
+ expect(bwapi.access_token_expiry).to eql(nil)
409
+ end
410
+
411
+ it 'should reset the adapter value' do
412
+ expect(bwapi.adapter).to eql(nil)
413
+ end
414
+
415
+ it 'should reset the api_endpoint value' do
416
+ expect(bwapi.api_endpoint).to eql(nil)
417
+ end
418
+
419
+ it 'should reset the client_id value' do
420
+ expect(bwapi.client_id).to eql(nil)
421
+ end
422
+
423
+ it 'should reset the client_secret value' do
424
+ expect(bwapi.instance_variable_get(:@client_secret)).to eql(nil)
425
+ end
426
+
427
+ it 'should reset the connection value' do
428
+ expect(bwapi.instance_variable_get(:@connection)).to eql(nil)
429
+ end
430
+
431
+ it 'should reset the debug value' do
432
+ expect(bwapi.debug).to eql(nil)
433
+ end
434
+
435
+ it 'should reset the grant_type value' do
436
+ expect(bwapi.grant_type).to eql(nil)
437
+ end
438
+
439
+ it 'should reset the logger value' do
440
+ expect(bwapi.logger).to eql(nil)
441
+ end
442
+
443
+ it 'should reset the password value' do
444
+ expect(bwapi.instance_variable_get(:@password)).to eql(nil)
445
+ end
446
+
447
+ it 'should reset the performance value' do
448
+ expect(bwapi.performance).to eql(nil)
449
+ end
450
+
451
+ it 'should reset the refresh_token value' do
452
+ expect(bwapi.refresh_token).to eql(nil)
453
+ end
454
+
455
+ it 'should reset the user_agent value' do
456
+ expect(bwapi.user_agent).to eql(nil)
457
+ end
458
+
459
+ it 'should reset the username value' do
460
+ expect(bwapi.username).to eql(nil)
461
+ end
462
+
463
+ it 'should reset the verify_ssl value' do
464
+ expect(bwapi.verify_ssl).to eql(nil)
465
+ end
466
+ end
467
+
468
+ describe '.options' do
469
+ before { bwapi.reset }
470
+
471
+ it 'should return the current configuration set' do
472
+ expect(bwapi.send(:options)).to eql(
473
+ access_token: nil,
474
+ access_token_expiry: nil,
475
+ adapter: :net_http,
476
+ api_endpoint: 'https://newapi.brandwatch.com/',
477
+ client_id: 'brandwatch-api-client',
478
+ client_secret: nil,
479
+ connection_options: {
480
+ headers: {
481
+ user_agent: 'BWAPI Ruby Gem 10.0.0'
482
+ },
483
+ request: {
484
+ params_encoder: Faraday::FlatParamsEncoder
485
+ }
486
+ },
487
+ debug: false,
488
+ grant_type: 'api-password',
489
+ logger: nil,
490
+ password: nil,
491
+ performance: {},
492
+ refresh_token: nil,
493
+ user_agent: 'BWAPI Ruby Gem 10.0.0',
494
+ username: nil,
495
+ verify_ssl: false
496
+ )
497
+ end
498
+ end
499
+ end
@@ -0,0 +1,242 @@
1
+ require 'helper'
2
+
3
+ describe BWAPI::Default do
4
+ describe 'constants' do
5
+ it 'should have an ADAPTER constant with a default value' do
6
+ expect(BWAPI::Default::ADAPTER).to eql(:net_http)
7
+ end
8
+
9
+ it 'should have an API_ENDPOINT constant with a default value' do
10
+ expect(BWAPI::Default::API_ENDPOINT).to eql('https://newapi.brandwatch.com/')
11
+ end
12
+
13
+ it 'should have an CLIENT_ID constant with a default value' do
14
+ expect(BWAPI::Default::CLIENT_ID).to eql('brandwatch-api-client')
15
+ end
16
+
17
+ it 'should have an GRANT_TYPE constant with a default value' do
18
+ expect(BWAPI::Default::GRANT_TYPE).to eql('api-password')
19
+ end
20
+
21
+ it 'should have an USER_AGENT constant with a default value' do
22
+ expect(BWAPI::Default::USER_AGENT).to eql("BWAPI Ruby Gem #{BWAPI::VERSION}")
23
+ end
24
+ end
25
+
26
+ describe '.options' do
27
+ it 'should return the configuration keys' do
28
+ expect(BWAPI::Default.options).to eql(
29
+ access_token: nil,
30
+ access_token_expiry: nil,
31
+ adapter: :net_http,
32
+ api_endpoint: 'https://newapi.brandwatch.com/',
33
+ client_id: 'brandwatch-api-client',
34
+ client_secret: nil,
35
+ connection_options: {
36
+ headers: {
37
+ user_agent: 'BWAPI Ruby Gem 10.0.0'
38
+ },
39
+ request: {
40
+ params_encoder: Faraday::FlatParamsEncoder
41
+ }
42
+ },
43
+ debug: false,
44
+ grant_type: 'api-password',
45
+ logger: nil,
46
+ password: nil,
47
+ performance: {},
48
+ refresh_token: nil,
49
+ user_agent: 'BWAPI Ruby Gem 10.0.0',
50
+ username: nil,
51
+ verify_ssl: false
52
+ )
53
+ end
54
+ end
55
+
56
+ describe '.access_token' do
57
+ after { ENV['BWAPI_ACCESS_TOKEN'] = nil }
58
+
59
+ it 'should return nil when no environmental variable is set' do
60
+ expect(BWAPI::Default.access_token).to eql(nil)
61
+ end
62
+
63
+ it 'should return the correct value when the environment variable is set' do
64
+ ENV['BWAPI_ACCESS_TOKEN'] = '1234-5678-9101-1234'
65
+ expect(BWAPI::Default.access_token).to eql('1234-5678-9101-1234')
66
+ end
67
+ end
68
+
69
+ describe '.access_token_expiry' do
70
+ it 'should return nil as default' do
71
+ expect(BWAPI::Default.access_token_expiry).to eql(nil)
72
+ end
73
+ end
74
+
75
+ describe '.adapter' do
76
+ after { ENV['BWAPI_ADAPTER'] = nil }
77
+
78
+ it 'should return the default value when no environmental variable is set' do
79
+ expect(BWAPI::Default.adapter).to eql(:net_http)
80
+ end
81
+
82
+ it 'should return the correct value when the environment variable is set' do
83
+ ENV['BWAPI_ADAPTER'] = 'test'
84
+ expect(BWAPI::Default.adapter).to eql(:test)
85
+ end
86
+ end
87
+
88
+ describe '.api_endpoint' do
89
+ after { ENV['BWAPI_API_ENDPOINT'] = nil }
90
+
91
+ it 'should return the default value when no environmental variable is set' do
92
+ expect(BWAPI::Default.api_endpoint).to eql('https://newapi.brandwatch.com/')
93
+ end
94
+
95
+ it 'should return the correct value when the environment variable is set' do
96
+ ENV['BWAPI_API_ENDPOINT'] = 'https://newapi.test.brandwatch.com'
97
+ expect(BWAPI::Default.api_endpoint).to eql('https://newapi.test.brandwatch.com')
98
+ end
99
+ end
100
+
101
+ describe '.client_id' do
102
+ after { ENV['BWAPI_CLIENT_ID'] = nil }
103
+
104
+ it 'should return the default value when no environmental variable is set' do
105
+ expect(BWAPI::Default.client_id).to eql('brandwatch-api-client')
106
+ end
107
+
108
+ it 'should return the correct value when the environment variable is set' do
109
+ ENV['BWAPI_CLIENT_ID'] = 'brandwatch-test-client'
110
+ expect(BWAPI::Default.client_id).to eql('brandwatch-test-client')
111
+ end
112
+ end
113
+
114
+ describe '.client_secret' do
115
+ after { ENV['BWAPI_CLIENT_SECRET'] = nil }
116
+
117
+ it 'should return nil as default' do
118
+ expect(BWAPI::Default.client_secret).to eql(nil)
119
+ end
120
+
121
+ it 'should return the correct value when the environment variable is set' do
122
+ ENV['BWAPI_CLIENT_SECRET'] = 'secret'
123
+ expect(BWAPI::Default.client_secret).to eql('secret')
124
+ end
125
+ end
126
+
127
+ describe '.connection_options' do
128
+ it 'should return the default hash values' do
129
+ expect(BWAPI::Default.connection_options).to eql(
130
+ headers: {
131
+ user_agent: 'BWAPI Ruby Gem 10.0.0'
132
+ },
133
+ request: {
134
+ params_encoder: Faraday::FlatParamsEncoder
135
+ }
136
+ )
137
+ end
138
+ end
139
+
140
+ describe '.debug' do
141
+ after { ENV['BWAPI_DEBUG'] = nil }
142
+
143
+ it 'should return false as default' do
144
+ expect(BWAPI::Default.debug).to eql(false)
145
+ end
146
+
147
+ it 'should return the correct value when the environment variable is set' do
148
+ ENV['BWAPI_DEBUG'] = 'true'
149
+ expect(BWAPI::Default.debug).to eql(true)
150
+ end
151
+ end
152
+
153
+ describe '.grant_type' do
154
+ after { ENV['BWAPI_GRANT_TYPE'] = nil }
155
+
156
+ it 'should return the default value when no environmental variable is set' do
157
+ expect(BWAPI::Default.grant_type).to eql('api-password')
158
+ end
159
+
160
+ it 'should return the correct value when the environment variable is set' do
161
+ ENV['BWAPI_GRANT_TYPE'] = 'test-grant'
162
+ expect(BWAPI::Default.grant_type).to eql('test-grant')
163
+ end
164
+ end
165
+
166
+ describe '.logger' do
167
+ it 'should return nil as default' do
168
+ expect(BWAPI::Default.logger).to eql(nil)
169
+ end
170
+ end
171
+
172
+ describe '.password' do
173
+ after { ENV['BWAPI_PASSWORD'] = nil }
174
+
175
+ it 'should return nil as default' do
176
+ expect(BWAPI::Default.password).to eql(nil)
177
+ end
178
+
179
+ it 'should return the correct value when the environment variable is set' do
180
+ ENV['BWAPI_PASSWORD'] = 'p455word'
181
+ expect(BWAPI::Default.password).to eql('p455word')
182
+ end
183
+ end
184
+
185
+ describe '.performance' do
186
+ it 'should return an empty hash as default' do
187
+ expect(BWAPI::Default.performance).to eql({})
188
+ end
189
+ end
190
+
191
+ describe '.refresh_token' do
192
+ after { ENV['BWAPI_REFRESH_TOKEN'] = nil }
193
+
194
+ it 'should return nil as default' do
195
+ expect(BWAPI::Default.refresh_token).to eql(nil)
196
+ end
197
+
198
+ it 'should return the correct value when the environment variable is set' do
199
+ ENV['BWAPI_REFRESH_TOKEN'] = '1234-5678-9101-1234'
200
+ expect(BWAPI::Default.refresh_token).to eql('1234-5678-9101-1234')
201
+ end
202
+ end
203
+
204
+ describe '.user_agent' do
205
+ after { ENV['BWAPI_USER_AGENT'] = nil }
206
+
207
+ it 'should return the default value when no environmental variable is set' do
208
+ expect(BWAPI::Default.user_agent).to eql("BWAPI Ruby Gem #{BWAPI::VERSION}")
209
+ end
210
+
211
+ it 'should return the correct value when the environment variable is set' do
212
+ ENV['BWAPI_USER_AGENT'] = 'Test User Agent'
213
+ expect(BWAPI::Default.user_agent).to eql('Test User Agent')
214
+ end
215
+ end
216
+
217
+ describe '.username' do
218
+ after { ENV['BWAPI_USERNAME'] = nil }
219
+
220
+ it 'should return nil as default' do
221
+ expect(BWAPI::Default.username).to eql(nil)
222
+ end
223
+
224
+ it 'should return the correct value when the environment variable is set' do
225
+ ENV['BWAPI_USERNAME'] = 'test@brandwatch.com'
226
+ expect(BWAPI::Default.username).to eql('test@brandwatch.com')
227
+ end
228
+ end
229
+
230
+ describe '.verify_ssl' do
231
+ after { ENV['BWAPI_VERIFY_SSL'] = nil }
232
+
233
+ it 'should return false as default' do
234
+ expect(BWAPI::Default.verify_ssl).to eql(false)
235
+ end
236
+
237
+ it 'should return the correct value when the environment variable is set' do
238
+ ENV['BWAPI_VERIFY_SSL'] = 'true'
239
+ expect(BWAPI::Default.verify_ssl).to eql(true)
240
+ end
241
+ end
242
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bwapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.0.0.pre.516
4
+ version: 10.0.0.pre.530
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Chrisp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-07 00:00:00.000000000 Z
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -243,6 +243,8 @@ files:
243
243
  - lib/bwapi/response/performance.rb
244
244
  - lib/bwapi/version.rb
245
245
  - spec/bwapi/client_spec.rb
246
+ - spec/bwapi/configuration_spec.rb
247
+ - spec/bwapi/default_spec.rb
246
248
  - spec/bwapi_spec.rb
247
249
  - spec/helper.rb
248
250
  homepage: https://github.com/jonathanchrisp/bwapi
@@ -271,5 +273,7 @@ specification_version: 4
271
273
  summary: Brandwatch API Wrapper
272
274
  test_files:
273
275
  - spec/bwapi/client_spec.rb
276
+ - spec/bwapi/configuration_spec.rb
277
+ - spec/bwapi/default_spec.rb
274
278
  - spec/bwapi_spec.rb
275
279
  - spec/helper.rb