bwapi 9.0.0.pre.349 → 9.0.0.pre.364

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +1 -1
  3. data/.rubocop.yml +3 -0
  4. data/.travis.yml +0 -3
  5. data/Gemfile +3 -3
  6. data/README.md +0 -1
  7. data/bin/bwapi +0 -1
  8. data/bwapi.gemspec +6 -9
  9. data/lib/bwapi.rb +16 -14
  10. data/lib/bwapi/client.rb +65 -16
  11. data/lib/bwapi/client/admin.rb +0 -2
  12. data/lib/bwapi/client/admin/become.rb +0 -2
  13. data/lib/bwapi/client/admin/search.rb +0 -2
  14. data/lib/bwapi/client/admin/sub_clients.rb +0 -2
  15. data/lib/bwapi/client/admin/users.rb +0 -2
  16. data/lib/bwapi/client/admin/users/sharing.rb +0 -2
  17. data/lib/bwapi/client/brandwatch.rb +0 -2
  18. data/lib/bwapi/client/brandwatch/become.rb +0 -2
  19. data/lib/bwapi/client/brandwatch/client_modules.rb +0 -1
  20. data/lib/bwapi/client/client.rb +0 -2
  21. data/lib/bwapi/client/command_center.rb +0 -2
  22. data/lib/bwapi/client/command_center/client.rb +0 -2
  23. data/lib/bwapi/client/command_center/displays.rb +0 -2
  24. data/lib/bwapi/client/command_center/displays/events.rb +0 -2
  25. data/lib/bwapi/client/command_center/displays/messages.rb +0 -1
  26. data/lib/bwapi/client/command_center/displays/scenes.rb +0 -2
  27. data/lib/bwapi/client/command_center/displays/screens.rb +0 -2
  28. data/lib/bwapi/client/command_center/displays/users.rb +0 -2
  29. data/lib/bwapi/client/command_center/displays/users/access.rb +0 -1
  30. data/lib/bwapi/client/command_center/scene_types.rb +0 -2
  31. data/lib/bwapi/client/command_center/themes.rb +0 -2
  32. data/lib/bwapi/client/command_center/users.rb +0 -2
  33. data/lib/bwapi/client/command_center/users/access.rb +0 -2
  34. data/lib/bwapi/client/error_codes.rb +0 -2
  35. data/lib/bwapi/client/filters.rb +0 -2
  36. data/lib/bwapi/client/languages.rb +0 -2
  37. data/lib/bwapi/client/locations.rb +0 -2
  38. data/lib/bwapi/client/logout.rb +0 -3
  39. data/lib/bwapi/client/me.rb +2 -11
  40. data/lib/bwapi/client/metrics.rb +0 -2
  41. data/lib/bwapi/client/oauth.rb +0 -3
  42. data/lib/bwapi/client/ping.rb +0 -2
  43. data/lib/bwapi/client/projects.rb +0 -2
  44. data/lib/bwapi/client/projects/bulkactions.rb +0 -2
  45. data/lib/bwapi/client/projects/bulkactions/author.rb +0 -2
  46. data/lib/bwapi/client/projects/bulkactions/mentions.rb +0 -2
  47. data/lib/bwapi/client/projects/categories.rb +0 -2
  48. data/lib/bwapi/client/projects/categories/copy.rb +0 -2
  49. data/lib/bwapi/client/projects/data.rb +0 -1
  50. data/lib/bwapi/client/projects/data/mentions.rb +0 -1
  51. data/lib/bwapi/client/projects/data/mentions/notes.rb +0 -2
  52. data/lib/bwapi/client/projects/data/volume.rb +0 -2
  53. data/lib/bwapi/client/projects/data_download.rb +0 -2
  54. data/lib/bwapi/client/projects/demographics.rb +0 -1
  55. data/lib/bwapi/client/projects/facebook_queries.rb +0 -2
  56. data/lib/bwapi/client/projects/group.rb +0 -1
  57. data/lib/bwapi/client/projects/group/author_group.rb +0 -2
  58. data/lib/bwapi/client/projects/group/location_group.rb +0 -1
  59. data/lib/bwapi/client/projects/group/site_group.rb +0 -1
  60. data/lib/bwapi/client/projects/queries.rb +0 -2
  61. data/lib/bwapi/client/projects/queries/backfill.rb +0 -1
  62. data/lib/bwapi/client/projects/queries/date_range.rb +0 -2
  63. data/lib/bwapi/client/projects/queries/mentions.rb +0 -1
  64. data/lib/bwapi/client/projects/query_groups.rb +0 -1
  65. data/lib/bwapi/client/projects/rules.rb +0 -1
  66. data/lib/bwapi/client/projects/rules/backfill.rb +0 -2
  67. data/lib/bwapi/client/projects/rules/copy.rb +0 -2
  68. data/lib/bwapi/client/projects/sharing.rb +0 -2
  69. data/lib/bwapi/client/projects/signals.rb +0 -2
  70. data/lib/bwapi/client/projects/stream_dashboards.rb +0 -2
  71. data/lib/bwapi/client/projects/stream_dashboards/streams.rb +0 -1
  72. data/lib/bwapi/client/projects/summary.rb +0 -2
  73. data/lib/bwapi/client/projects/tags.rb +0 -2
  74. data/lib/bwapi/client/projects/twitter_queries.rb +0 -2
  75. data/lib/bwapi/client/projects/users.rb +0 -2
  76. data/lib/bwapi/client/projects/workflow.rb +0 -2
  77. data/lib/bwapi/client/query_validation.rb +0 -2
  78. data/lib/bwapi/client/sso.rb +0 -2
  79. data/lib/bwapi/client/test_search.rb +0 -2
  80. data/lib/bwapi/client/user.rb +0 -2
  81. data/lib/bwapi/client/user/notifications.rb +0 -2
  82. data/lib/bwapi/configuration.rb +40 -56
  83. data/lib/bwapi/connection.rb +32 -14
  84. data/lib/bwapi/default.rb +91 -0
  85. data/lib/bwapi/error.rb +1 -3
  86. data/lib/bwapi/performance.rb +39 -0
  87. data/lib/bwapi/request.rb +6 -33
  88. data/lib/bwapi/response/error.rb +28 -0
  89. data/lib/bwapi/response/logger.rb +35 -0
  90. data/lib/bwapi/response/performance.rb +31 -0
  91. data/lib/bwapi/version.rb +0 -2
  92. data/spec/bwapi/client_spec.rb +55 -49
  93. data/spec/bwapi_spec.rb +1 -3
  94. data/spec/helper.rb +1 -4
  95. metadata +32 -47
  96. data/lib/bwapi/authentication.rb +0 -48
  97. data/lib/faraday/response/brandwatch_error.rb +0 -25
  98. data/spec/bwapi/authentication_spec.rb +0 -57
  99. data/spec/fixtures/.netrc +0 -3
@@ -0,0 +1,91 @@
1
+ require 'bwapi/version'
2
+ require 'bwapi/configuration'
3
+
4
+ require 'allotment'
5
+ require 'faraday'
6
+
7
+ module BWAPI
8
+ # Default module
9
+ module Default
10
+ ADAPTER = Faraday.default_adapter
11
+
12
+ API_ENDPOINT = 'https://newapi.brandwatch.com/'
13
+
14
+ CLIENT_ID = 'brandwatch-api-client'
15
+
16
+ USER_AGENT = "BWAPI Ruby Gem #{BWAPI::VERSION}".freeze
17
+
18
+ class << self
19
+ def options
20
+ Hash[BWAPI::Configuration.keys.map { |key| [key, send(key)] }]
21
+ end
22
+
23
+ def access_token
24
+ ENV['BWAPI_ACCESS_TOKEN']
25
+ end
26
+
27
+ def adapter
28
+ ENV['BWAPI_ADAPTER'] || ADAPTER
29
+ end
30
+
31
+ def api_endpoint
32
+ ENV['BWAPI_API_ENDPOINT'] || API_ENDPOINT
33
+ end
34
+
35
+ def client_id
36
+ ENV['BWAPI_CLIENT_ID'] || CLIENT_ID
37
+ end
38
+
39
+ def client_secret
40
+ ENV['BWAPI_CLIENT_SECRET']
41
+ end
42
+
43
+ def connection_options
44
+ {
45
+ headers: {
46
+ user_agent: user_agent
47
+ },
48
+ request: {
49
+ params_encoder: Faraday::FlatParamsEncoder
50
+ }
51
+ }
52
+ end
53
+
54
+ def debug
55
+ false
56
+ end
57
+
58
+ def grant_type
59
+ ENV['BWAPI_GRANT_TYPE']
60
+ end
61
+
62
+ def logger
63
+ nil
64
+ end
65
+
66
+ def password
67
+ ENV['BWAPI_PASSWORD']
68
+ end
69
+
70
+ def performance
71
+ Allotment.results
72
+ end
73
+
74
+ def refresh_token
75
+ ENV['BWAPI_REFRESH_TOKEN']
76
+ end
77
+
78
+ def user_agent
79
+ ENV['BWAPI_USER_AGENT'] || USER_AGENT
80
+ end
81
+
82
+ def username
83
+ ENV['BWAPI_USERNAME']
84
+ end
85
+
86
+ def verify_ssl
87
+ ENV['BWAPI_VERIFY_SSL']
88
+ end
89
+ end
90
+ end
91
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  module BWAPI
4
2
  # BW error class to capture BWAPI error responses
5
3
  class BWError < StandardError
@@ -91,7 +89,7 @@ module BWAPI
91
89
  # Raised when Brandwatch returns a 422 HTTP status code
92
90
  class UnprocessableEntity < BWError; end
93
91
 
94
- # Raised when Brandwatch returns a 429 HTTP status code
92
+ # Raised when Brandwatch returns a 429 HTTP status code
95
93
  class TooManyRequests < BWError; end
96
94
 
97
95
  # Raised when Brandwatch returns a 500 HTTP status code
@@ -0,0 +1,39 @@
1
+ require 'allotment'
2
+ require 'hashie'
3
+
4
+ module BWAPI
5
+ # Helper methods for performance
6
+ module Performance
7
+ def average_response_time
8
+ averages = []
9
+ performance.each_key { |key| averages << Allotment.results[key].average }
10
+ averages.average
11
+ end
12
+
13
+ def average_response_time_breakdown
14
+ results = []
15
+ performance.each do |key, value|
16
+ results << Hashie::Mash.new(path: key, average: Allotment.results[key].average, count: value.size)
17
+ end
18
+ results
19
+ end
20
+
21
+ def fastest_response
22
+ average_response_time_breakdown.sort_by { |result| result[:average] }.first
23
+ end
24
+
25
+ def response_paths
26
+ performance.keys
27
+ end
28
+
29
+ def slowest_response
30
+ average_response_time_breakdown.sort_by { |result| result[:average] }.reverse.first
31
+ end
32
+
33
+ def total_responses
34
+ responses = 0
35
+ performance.each_value { |value| responses += value.size }
36
+ responses
37
+ end
38
+ end
39
+ end
@@ -1,10 +1,10 @@
1
- # encoding: utf-8
2
-
3
1
  require 'faraday'
4
2
 
5
3
  module BWAPI
6
- # Request module to handle all requests to API
4
+ # Client class to create BWAPI instances
7
5
  module Request
6
+ RACK_BUILDER_CLASS = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
7
+
8
8
  # Perform a get request
9
9
  #
10
10
  # @param path [String] URL path to send request
@@ -52,33 +52,16 @@ module BWAPI
52
52
 
53
53
  private
54
54
 
55
- # Sets connection options
56
- def connection_options
57
- {
58
- headers: {
59
- authorization: access_token ? "bearer #{access_token}" : '',
60
- user_agent: user_agent
61
- },
62
- request: {
63
- params_encoder: Faraday::FlatParamsEncoder
64
- },
65
- url: api_endpoint,
66
- ssl: { verify: verify_ssl }
67
- }
68
- end
69
-
70
55
  # Perform a request
71
56
  #
72
- # @param method [String] Type of request path
57
+ # @param method [String] Type of request
73
58
  # @param path [String] URL path to send request
74
59
  # @param opts [Hash] Request parameters
75
60
  # @return [Hashie::Mash] Response
76
61
  def request(method, path, opts = {})
77
- response = connection(connection_options).send(method) do |r|
62
+ response = connection.send(method) do |r|
78
63
  case method
79
- when :get
80
- r.url path, opts
81
- when :delete
64
+ when :get, :delete
82
65
  r.url path, opts
83
66
  when :patch, :post, :put
84
67
  if opts.is_a?(Hash) && opts.key?(:force_urlencoded)
@@ -90,16 +73,6 @@ module BWAPI
90
73
  end
91
74
  end
92
75
  end
93
-
94
- if debug
95
- log.info "Connection options: #{connection_options}"
96
- log.info "Sending request type: #{method}"
97
- log.info "Request path: #{path}"
98
- log.info "Request url: #{api_endpoint}/#{path}"
99
- log.info "Request parameters: #{opts.to_json}" unless opts.nil?
100
- log.info "Response body: #{response.env[:body].to_json}" unless response.env[:body].nil?
101
- end
102
-
103
76
  response
104
77
  end
105
78
  end
@@ -0,0 +1,28 @@
1
+ require 'bwapi/error'
2
+ require 'faraday'
3
+
4
+ module BWAPI
5
+ module Response
6
+ # Brandwatch response middleware to handle errors
7
+ class Error < Faraday::Response::Middleware
8
+ ERROR_MAP = {
9
+ 400 => BWAPI::BadRequest,
10
+ 401 => BWAPI::Unauthorized,
11
+ 403 => BWAPI::Forbidden,
12
+ 404 => BWAPI::NotFound,
13
+ 406 => BWAPI::NotAcceptable,
14
+ 422 => BWAPI::UnprocessableEntity,
15
+ 429 => BWAPI::TooManyRequests,
16
+ 500 => BWAPI::InternalServerError,
17
+ 501 => BWAPI::NotImplemented,
18
+ 502 => BWAPI::BadGateway,
19
+ 503 => BWAPI::ServiceUnavailable
20
+ }
21
+
22
+ def on_complete(response)
23
+ key = response[:status].to_i
24
+ fail ERROR_MAP[key].new(response) if ERROR_MAP.key? key
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ require 'faraday'
2
+
3
+ module BWAPI
4
+ module Response
5
+ # Brandwatch response middleware to handle logging
6
+ class Logger < Faraday::Response::Middleware
7
+ def initialize(app, client)
8
+ super(app)
9
+ @client = client
10
+ end
11
+
12
+ def logger
13
+ @logger = @client.logger || begin
14
+ require 'logger'
15
+ ::Logger.new(STDOUT)
16
+ end
17
+ end
18
+
19
+ def call(env)
20
+ logger.info('Request') { "#{env.method.upcase} - #{env.url}" }
21
+ logger.debug('Request headers') { dump_output(env.request_headers) }
22
+ super
23
+ end
24
+
25
+ def on_complete(env)
26
+ logger.info('Response Status') { "#{env.status}" }
27
+ logger.debug('Response headers') { dump_output(env.response_headers) }
28
+ end
29
+
30
+ def dump_output(object)
31
+ object.map { |key, value| "#{key}: #{value.inspect}" }.join(', ')
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ require 'faraday'
2
+ require 'allotment'
3
+
4
+ module BWAPI
5
+ module Response
6
+ # Brandwatch response middleware to monitor performance
7
+ class Performance < Faraday::Response::Middleware
8
+ def initialize(app, client)
9
+ super(app)
10
+ @client = client
11
+ end
12
+
13
+ def logger
14
+ @logger = @client.logger || begin
15
+ require 'logger'
16
+ ::Logger.new(STDOUT)
17
+ end
18
+ end
19
+
20
+ def call(env)
21
+ @recording = ("#{env.method}#{env.url.path}").gsub!('/', '_')
22
+ Allotment.start(@recording)
23
+ super
24
+ end
25
+
26
+ def on_complete(_env)
27
+ logger.info "Response time: #{Allotment.stop(@recording)} seconds"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # BWAPI Version
4
2
  module BWAPI
5
3
  VERSION = '9.0.0'
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'helper'
4
2
 
5
3
  describe BWAPI::Client do
@@ -9,7 +7,41 @@ describe BWAPI::Client do
9
7
 
10
8
  describe 'when called' do
11
9
  it 'should be a client class instance' do
12
- BWAPI::Client.new.should be_an_instance_of BWAPI::Client
10
+ expect(BWAPI::Client.new).to be_an_instance_of BWAPI::Client
11
+ end
12
+ end
13
+
14
+ describe '.authenticated?' do
15
+ it 'returns true when authenticated' do
16
+ expect(BWAPI::Client.new.authenticated?).to eql(false)
17
+ end
18
+
19
+ it 'returns false when not authenticated' do
20
+ bw = BWAPI::Client.new access_token: 'abcdef-ghijkl-123456-789012'
21
+ expect(bw.authenticated?).to eql(true)
22
+ end
23
+ end
24
+
25
+ describe '.application_client?' do
26
+ it 'returns true when client is a brandwatch-application-client' do
27
+ bw = BWAPI::Client.new client_id: 'brandwatch-application-client'
28
+ expect(bw.application_client?).to eql(true)
29
+ end
30
+
31
+ it 'returns false when a client is not a brandwatch-application-client' do
32
+ expect(BWAPI::Client.new.application_client?).to eql(false)
33
+ end
34
+ end
35
+
36
+ describe '.api_client?' do
37
+ it 'returns true when client is a brandwatch-api-client' do
38
+ bw = BWAPI::Client.new client_id: 'brandwatch-api-client'
39
+ expect(bw.api_client?).to eql(true)
40
+ end
41
+
42
+ it 'returns false when a client is not a brandwatch-api-client' do
43
+ bw = BWAPI::Client.new client_id: 'brandwatch-application-client'
44
+ expect(bw.api_client?).to eql(false)
13
45
  end
14
46
  end
15
47
 
@@ -31,8 +63,8 @@ describe BWAPI::Client do
31
63
  expect(BWAPI::Client.new.respond_to?(:username)).to eq(true)
32
64
  end
33
65
 
34
- it 'should create a password instance variable' do
35
- expect(BWAPI::Client.new.respond_to?(:password)).to eq(true)
66
+ it 'should create a password instance variable which cannot be accessed' do
67
+ expect(BWAPI::Client.new.respond_to?(:password)).to eq(false)
36
68
  end
37
69
 
38
70
  it 'should create a grant_type instance variable' do
@@ -47,24 +79,16 @@ describe BWAPI::Client do
47
79
  expect(BWAPI::Client.new.respond_to?(:refresh_token)).to eq(true)
48
80
  end
49
81
 
50
- it 'should create a expires_in instance variable' do
51
- expect(BWAPI::Client.new.respond_to?(:expires_in)).to eq(true)
52
- end
53
-
54
82
  it 'should create a client_id instance variable' do
55
83
  expect(BWAPI::Client.new.respond_to?(:client_id)).to eq(true)
56
84
  end
57
85
 
58
- it 'should create a client_secret instance variable' do
59
- expect(BWAPI::Client.new.respond_to?(:client_secret)).to eq(true)
60
- end
61
-
62
- it 'should create a netrc instance variable' do
63
- expect(BWAPI::Client.new.respond_to?(:netrc)).to eq(true)
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)
64
88
  end
65
89
 
66
- it 'should create a netrc_file instance variable' do
67
- expect(BWAPI::Client.new.respond_to?(:netrc_file)).to eq(true)
90
+ it 'should create a logger instance variable' do
91
+ expect(BWAPI::Client.new.respond_to?(:logger)).to eq(true)
68
92
  end
69
93
  end
70
94
 
@@ -113,28 +137,6 @@ describe BWAPI::Client do
113
137
  end
114
138
  end
115
139
 
116
- describe 'netrc' do
117
- it 'should have a default netrc value' do
118
- expect(BWAPI::Client.new.netrc).to eql(false)
119
- end
120
-
121
- it 'should allow a user to set true for the netrc value' do
122
- bw = BWAPI::Client.new netrc: true, netrc_file: File.join(fixture_path, '.netrc')
123
- expect(bw.netrc).to eql(true)
124
- end
125
- end
126
-
127
- describe 'netrc_file' do
128
- it 'should have a default netrc value' do
129
- expect(BWAPI::Client.new.netrc_file).to eql("#{ENV['HOME'] + '/' + '.netrc'}")
130
- end
131
-
132
- it 'should allow a user to set a netrc_file value' do
133
- bw = BWAPI::Client.new netrc_file: 'example/.netrc'
134
- expect(bw.netrc_file).to eql('example/.netrc')
135
- end
136
- end
137
-
138
140
  describe 'username' do
139
141
  it 'should have a default value of nil for username' do
140
142
  expect(BWAPI::Client.new.username).to eql(nil)
@@ -147,13 +149,9 @@ describe BWAPI::Client do
147
149
  end
148
150
 
149
151
  describe 'password' do
150
- it 'should have a default value of nil for password' do
151
- expect(BWAPI::Client.new.password).to eql(nil)
152
- end
153
-
154
152
  it 'should allow a user to set a password value' do
155
153
  bw = BWAPI::Client.new password: 'pa55w0rd'
156
- expect(bw.password).to eql('pa55w0rd')
154
+ expect(bw.instance_eval { @password }).to eql('pa55w0rd')
157
155
  end
158
156
  end
159
157
 
@@ -191,13 +189,21 @@ describe BWAPI::Client do
191
189
  end
192
190
 
193
191
  describe 'client_secret' do
194
- it 'should have a default value of nil for client_secret' do
195
- expect(BWAPI::Client.new.client_secret).to eql(nil)
196
- end
197
-
198
192
  it 'should allow a user to set a client_secret value' do
199
193
  bw = BWAPI::Client.new client_secret: 'custom_client_secret'
200
- expect(bw.client_secret).to eql('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)
201
207
  end
202
208
  end
203
209
  end