gnip_api 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +16 -37
- data/lib/gnip_api/adapter.rb +42 -28
- data/lib/gnip_api/configuration.rb +4 -6
- data/lib/gnip_api/errors.rb +3 -0
- data/lib/gnip_api/gnip/gnip_data.rb +1 -1
- data/lib/gnip_api/gnip/system_message.rb +4 -0
- data/lib/gnip_api/power_track/buffer.rb +42 -0
- data/lib/gnip_api/power_track/rule.rb +77 -0
- data/lib/gnip_api/power_track/rule_validator.rb +6 -0
- data/lib/gnip_api/power_track/rules.rb +81 -0
- data/lib/gnip_api/power_track/stream.rb +122 -0
- data/lib/gnip_api/request.rb +8 -1
- data/lib/gnip_api/response.rb +21 -0
- data/lib/gnip_api/search.rb +108 -0
- data/lib/gnip_api/version.rb +1 -1
- data/lib/gnip_api.rb +5 -7
- data/spec/fixtures/rule_value_examples.json +27 -0
- data/spec/gnip_api/adapter_spec.rb +0 -87
- data/spec/gnip_api/configuration_spec.rb +0 -15
- data/spec/gnip_api/{apis/power_track → power_track}/buffer_spec.rb +2 -2
- data/spec/gnip_api/power_track/rule_spec.rb +89 -0
- data/spec/gnip_api/{apis/power_track → power_track}/rules_spec.rb +8 -8
- data/spec/gnip_api/power_track/stream_spec.rb +53 -0
- data/spec/gnip_api/response_spec.rb +27 -0
- data/spec/gnip_api/{apis/search_spec.rb → search_spec.rb} +9 -9
- data/spec/spec_helper.rb +4 -3
- metadata +20 -26
- data/lib/gnip_api/adapters/base_adapter.rb +0 -94
- data/lib/gnip_api/adapters/httparty_adapter.rb +0 -65
- data/lib/gnip_api/apis/power_track/buffer.rb +0 -38
- data/lib/gnip_api/apis/power_track/rule.rb +0 -33
- data/lib/gnip_api/apis/power_track/rule_validator.rb +0 -8
- data/lib/gnip_api/apis/power_track/rules.rb +0 -83
- data/lib/gnip_api/apis/power_track/stream.rb +0 -84
- data/lib/gnip_api/apis/search.rb +0 -109
- data/spec/gnip_api/adapters/base_adapter_spec.rb +0 -0
- data/spec/gnip_api/adapters/httparty_adapter_spec.rb +0 -0
- data/spec/gnip_api/apis/power_track/rule_spec.rb +0 -62
- data/spec/gnip_api/apis/power_track/stream_spec.rb +0 -93
- data/spec/lib/test_adapter.rb +0 -16
data/lib/gnip_api/response.rb
CHANGED
@@ -31,5 +31,26 @@ module GnipApi
|
|
31
31
|
end
|
32
32
|
return nil
|
33
33
|
end
|
34
|
+
|
35
|
+
def check_for_errors!
|
36
|
+
if ok?
|
37
|
+
GnipApi.logger.info "#{request_method} request to #{request_uri} returned with status #{status} OK"
|
38
|
+
GnipApi.logger.debug "Headers -> #{headers.inspect}"
|
39
|
+
GnipApi.logger.debug "Body -> #{body.inspect}"
|
40
|
+
GnipApi.logger.debug "Request headers -> #{request.headers.inspect}"
|
41
|
+
GnipApi.logger.debug "Request payload -> #{request.payload.inspect}"
|
42
|
+
else
|
43
|
+
error_message = error_message
|
44
|
+
GnipApi.logger.error "#{request_method} request to #{request_uri} returned with status #{status} FAIL"
|
45
|
+
GnipApi.logger.debug "Headers -> #{headers.inspect}"
|
46
|
+
GnipApi.logger.debug "Body -> #{body.inspect}"
|
47
|
+
GnipApi.logger.debug "Request headers -> #{request.headers.inspect}"
|
48
|
+
GnipApi.logger.debug "Request payload -> #{request.payload.inspect}"
|
49
|
+
raise GnipApi::Errors::Adapter::GnipSoftwareError.new error_message if status == 503
|
50
|
+
raise GnipApi::Errors::Adapter::RateLimitError.new error_message if status == 429
|
51
|
+
raise GnipApi::Errors::Adapter::RequestError.new error_message
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
34
55
|
end
|
35
56
|
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# Gnip Search Full Archive API
|
2
|
+
#
|
3
|
+
# Retrive counts with a provided rule
|
4
|
+
|
5
|
+
module GnipApi
|
6
|
+
class Search
|
7
|
+
attr_reader :adapter
|
8
|
+
|
9
|
+
def initialize params={}
|
10
|
+
@adapter = GnipApi::Adapter.new
|
11
|
+
@label = params[:label] || GnipApi.config.label
|
12
|
+
end
|
13
|
+
|
14
|
+
def activities options={}
|
15
|
+
required_options?(options)
|
16
|
+
payload = construct_activities_payload(options)
|
17
|
+
request = GnipApi::Request.new_post(activities_endpoint, payload)
|
18
|
+
data = adapter.post(request)
|
19
|
+
return parse_activities_response(data)
|
20
|
+
end
|
21
|
+
|
22
|
+
def counts options={}
|
23
|
+
required_options?(options)
|
24
|
+
payload = construct_counts_payload(options)
|
25
|
+
request = GnipApi::Request.new_post(count_endpoint, payload)
|
26
|
+
data = adapter.post(request)
|
27
|
+
return parse_counts_response(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def count_endpoint
|
32
|
+
GnipApi::Endpoints.search_counts(@label)
|
33
|
+
end
|
34
|
+
|
35
|
+
def activities_endpoint
|
36
|
+
GnipApi::Endpoints.search_activities(@label)
|
37
|
+
end
|
38
|
+
|
39
|
+
def required_options
|
40
|
+
[:rule]
|
41
|
+
end
|
42
|
+
|
43
|
+
def required_options? options
|
44
|
+
provided_options = required_options - options.keys
|
45
|
+
raise GnipApi::Errors::Search::MissingParameters.new(provided_options) if provided_options.any?
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_date date
|
49
|
+
return nil unless date
|
50
|
+
date.strftime('%Y%m%d%H%M')
|
51
|
+
end
|
52
|
+
|
53
|
+
def parse_counts_response data
|
54
|
+
parsed_data = GnipApi::JsonParser.new.parse(data)
|
55
|
+
result_set = parsed_data['results']
|
56
|
+
params = parsed_data['requestParameters']
|
57
|
+
result = {:results => [], :total_count => parsed_data['totalCount'], :next => parsed_data['next'], :request_parameters => {}}
|
58
|
+
result[:request_parameters][:bucket] = params['bucket'] if params['bucket']
|
59
|
+
result[:request_parameters][:from_date] = Time.parse("#{params['fromDate']}-0000") if params['fromDate']
|
60
|
+
result[:request_parameters][:to_date] = Time.parse("#{params['toDate']}-0000") if params['toDate']
|
61
|
+
result_set.each do |item|
|
62
|
+
result[:results] << {:time_period => Time.parse("#{item['timePeriod']}-0000"), :count => item['count']}
|
63
|
+
end
|
64
|
+
return result
|
65
|
+
end
|
66
|
+
|
67
|
+
def parse_activities_response data
|
68
|
+
parsed_data = GnipApi::JsonParser.new.parse(data)
|
69
|
+
result_set = parsed_data['results']
|
70
|
+
params = parsed_data['requestParameters']
|
71
|
+
result = {:results => [], :next => parsed_data['next'], :request_parameters => {}}
|
72
|
+
result[:request_parameters][:bucket] = params['bucket'] if params['bucket']
|
73
|
+
result[:request_parameters][:from_date] = Time.parse("#{params['fromDate']}-0000") if params['fromDate']
|
74
|
+
result[:request_parameters][:to_date] = Time.parse("#{params['toDate']}-0000") if params['toDate']
|
75
|
+
result[:request_parameters][:max_results] = params['maxResults'] if params['maxResult']
|
76
|
+
result_set.each do |item|
|
77
|
+
result[:results] << Gnip::Activity.new(item)
|
78
|
+
end
|
79
|
+
return result
|
80
|
+
end
|
81
|
+
|
82
|
+
def construct_counts_payload options
|
83
|
+
payload = {
|
84
|
+
:query => options[:rule].value,
|
85
|
+
:fromDate => parse_date(options[:from_date]),
|
86
|
+
:toDate => parse_date(options[:to_date]),
|
87
|
+
:bucket => options[:bucket],
|
88
|
+
:next => options[:next_token]
|
89
|
+
}
|
90
|
+
payload.delete_if{|k,v| v.nil?}
|
91
|
+
return payload.to_json
|
92
|
+
end
|
93
|
+
|
94
|
+
def construct_activities_payload options
|
95
|
+
payload = {
|
96
|
+
:query => options[:rule].value,
|
97
|
+
:fromDate => parse_date(options[:from_date]),
|
98
|
+
:toDate => parse_date(options[:to_date]),
|
99
|
+
:tag => options[:rule].tag,
|
100
|
+
:maxResults => options[:max_results],
|
101
|
+
:next => options[:next_token]
|
102
|
+
}
|
103
|
+
payload.delete_if{|k,v| v.nil?}
|
104
|
+
return payload.to_json
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
data/lib/gnip_api/version.rb
CHANGED
data/lib/gnip_api.rb
CHANGED
@@ -9,11 +9,11 @@ require "gnip_api/version"
|
|
9
9
|
require "gnip_api/errors"
|
10
10
|
require "gnip_api/configuration"
|
11
11
|
require "gnip_api/endpoints"
|
12
|
-
require "gnip_api/
|
13
|
-
require "gnip_api/
|
14
|
-
require "gnip_api/
|
15
|
-
require "gnip_api/
|
16
|
-
require "gnip_api/
|
12
|
+
require "gnip_api/power_track/stream"
|
13
|
+
require "gnip_api/power_track/rules"
|
14
|
+
require "gnip_api/power_track/buffer"
|
15
|
+
require "gnip_api/power_track/rule"
|
16
|
+
require "gnip_api/search"
|
17
17
|
require "gnip_api/gnip/message"
|
18
18
|
require "gnip_api/gnip/system_message"
|
19
19
|
require "gnip_api/gnip/twitter_compliance_message"
|
@@ -22,8 +22,6 @@ require "gnip_api/gnip/activity"
|
|
22
22
|
require "gnip_api/gnip/gnip_data"
|
23
23
|
require "gnip_api/gnip/url"
|
24
24
|
require "gnip_api/adapter"
|
25
|
-
require "gnip_api/adapters/base_adapter"
|
26
|
-
require "gnip_api/adapters/httparty_adapter"
|
27
25
|
require "gnip_api/response"
|
28
26
|
require "gnip_api/request"
|
29
27
|
require "gnip_api/json_parser"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"consistent":
|
3
|
+
[
|
4
|
+
"value",
|
5
|
+
"(value)",
|
6
|
+
"value OR (value)",
|
7
|
+
"(value OR (value))",
|
8
|
+
" (value OR value)",
|
9
|
+
" (value) OR value ",
|
10
|
+
"\"value\"",
|
11
|
+
" \"value\"",
|
12
|
+
" \"value\" ",
|
13
|
+
"\"value's\"",
|
14
|
+
"\"(('()\"",
|
15
|
+
"'value'","'(()('"
|
16
|
+
],
|
17
|
+
"inconsistent":
|
18
|
+
[
|
19
|
+
"value)",
|
20
|
+
"(value","value OR value)",
|
21
|
+
" (value",
|
22
|
+
" value)",
|
23
|
+
"\"value",
|
24
|
+
"'value",
|
25
|
+
")value"
|
26
|
+
]
|
27
|
+
}
|
@@ -14,95 +14,8 @@ describe GnipApi::Adapter do
|
|
14
14
|
expect(Proc.new{GnipApi::Adapter.new}).to raise_error(GnipApi::Errors::MissingCredentials)
|
15
15
|
end
|
16
16
|
|
17
|
-
it 'raises error if no adapter is selected' do
|
18
|
-
GnipApi.configure do |c|
|
19
|
-
c.user = 'somebody'
|
20
|
-
c.password = 'something'
|
21
|
-
c.account = 'lol'
|
22
|
-
c.adapter_class = nil
|
23
|
-
end
|
24
|
-
|
25
|
-
expect(Proc.new{GnipApi::Adapter.new}).to raise_error(GnipApi::Errors::MissingAdapter)
|
26
|
-
end
|
27
|
-
|
28
17
|
it 'does not raise error when credentials and adapter are present' do
|
29
18
|
configure_gem
|
30
19
|
expect(Proc.new{GnipApi::Adapter.new}).not_to raise_error
|
31
20
|
end
|
32
|
-
|
33
|
-
it 'uses adapter selected' do
|
34
|
-
configure_gem
|
35
|
-
GnipApi.configuration.adapter_class = TestAdapter
|
36
|
-
expect(GnipApi::Adapter.new.adapter.class).to eq(TestAdapter)
|
37
|
-
end
|
38
|
-
|
39
|
-
describe '#get' do
|
40
|
-
before do
|
41
|
-
configure_gem
|
42
|
-
@adapter = GnipApi::Adapter.new
|
43
|
-
@request = GnipApi::Request.new_get(@uri)
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'returns a body' do
|
47
|
-
result = @adapter.get @request
|
48
|
-
expect(result).to eq('get_result') # Defined at spec/lib/test_adapter.rb
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe '#post' do
|
53
|
-
before do
|
54
|
-
configure_gem
|
55
|
-
@adapter = GnipApi::Adapter.new
|
56
|
-
@request = GnipApi::Request.new_post(@uri, 'some_payload')
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'returns a body' do
|
60
|
-
result = @adapter.post @request
|
61
|
-
expect(result).to eq('post_result') # Defined at spec/lib/test_adapter.rb
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
describe '#delete' do
|
66
|
-
before do
|
67
|
-
configure_gem
|
68
|
-
@adapter = GnipApi::Adapter.new
|
69
|
-
@request = GnipApi::Request.new_delete(@uri, 'some_payload')
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'returns a body' do
|
73
|
-
result = @adapter.delete @request
|
74
|
-
expect(result).to eq('delete_result') # Defined at spec/lib/test_adapter.rb
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
describe '#check_response_for_errors' do
|
79
|
-
before{ configure_gem }
|
80
|
-
let(:adapter) { GnipApi::Adapter.new }
|
81
|
-
let(:request) { GnipApi::Request.new_get(@uri) }
|
82
|
-
|
83
|
-
context 'when is ok' do
|
84
|
-
let(:response) { GnipApi::Response.new(request, 200, 'good body', {}) }
|
85
|
-
it('does not throw exception') { expect(Proc.new{adapter.check_response_for_errors(response)}).not_to raise_error }
|
86
|
-
end
|
87
|
-
|
88
|
-
context 'when rate limited' do
|
89
|
-
let(:error_message){ {"error"=>{"message"=>"Exceeded rate limit", "sent"=>"2017-06-05T16:20:43+00:00", "transactionId"=>"00629e29000a91c1"}}.to_json }
|
90
|
-
let(:response) { GnipApi::Response.new(request, 429, error_message, {}) }
|
91
|
-
it('raises RateLimitError') { expect(Proc.new{adapter.check_response_for_errors(response)}).to raise_error(GnipApi::Errors::Adapter::RateLimitError) }
|
92
|
-
end
|
93
|
-
|
94
|
-
context 'when software error' do
|
95
|
-
let(:error_message){ {"error"=>{"message"=>"some server error", "sent"=>"2017-06-05T16:20:43+00:00", "transactionId"=>"00629e29000a91c1"}}.to_json }
|
96
|
-
let(:response) { GnipApi::Response.new(request, 503, error_message, {}) }
|
97
|
-
it('raises GnipSoftwareError') { expect(Proc.new{adapter.check_response_for_errors(response)}).to raise_error(GnipApi::Errors::Adapter::GnipSoftwareError) }
|
98
|
-
end
|
99
|
-
|
100
|
-
context 'when other error' do
|
101
|
-
let(:error_message){ {"error"=>{"message"=>"Exceeded rate limit", "sent"=>"2017-06-05T16:20:43+00:00", "transactionId"=>"00629e29000a91c1"}}.to_json }
|
102
|
-
let(:response) { GnipApi::Response.new(request, 400, error_message, {}) }
|
103
|
-
it('raises generic error') { expect(Proc.new{adapter.check_response_for_errors(response)}).to raise_error(GnipApi::Errors::Adapter::RequestError) }
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
107
|
-
|
108
21
|
end
|
@@ -1,20 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe GnipApi::Configuration do
|
4
|
-
describe 'OUTPUT_FORMATS' do
|
5
|
-
it 'includes :json' do
|
6
|
-
expect(GnipApi::Configuration::OUTPUT_FORMATS.include?(:json)).to eq(true)
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'includes :activity' do
|
10
|
-
expect(GnipApi::Configuration::OUTPUT_FORMATS.include?(:activity)).to eq(true)
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'includes :parsed_json' do
|
14
|
-
expect(GnipApi::Configuration::OUTPUT_FORMATS.include?(:parsed_json)).to eq(true)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
4
|
describe '#new' do
|
19
5
|
it 'has the default values' do
|
20
6
|
config = GnipApi::Configuration.new
|
@@ -22,7 +8,6 @@ describe GnipApi::Configuration do
|
|
22
8
|
expect(config.user).to eq(nil)
|
23
9
|
expect(config.password).to eq(nil)
|
24
10
|
expect(config.account).to eq(nil)
|
25
|
-
expect(config.adapter_class).to eq(GnipApi::Adapters::HTTPartyAdapter)
|
26
11
|
expect(config.logger).not_to eq(nil)
|
27
12
|
expect(config.source).to eq(nil)
|
28
13
|
expect(config.label).to eq(nil)
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe GnipApi::
|
3
|
+
describe GnipApi::PowerTrack::Buffer do
|
4
4
|
before do
|
5
|
-
@buffer = GnipApi::
|
5
|
+
@buffer = GnipApi::PowerTrack::Buffer.new(:terminator => '--')
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'has "--" as terminator char' do
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
def consistent_values
|
4
|
+
@consistent_values ||= JSON.parse(read_fixture('rule_value_examples.json'))['consistent']
|
5
|
+
end
|
6
|
+
|
7
|
+
def inconsistent_values
|
8
|
+
@inconsistent_values ||= JSON.parse(read_fixture('rule_value_examples.json'))['inconsistent']
|
9
|
+
end
|
10
|
+
|
11
|
+
describe GnipApi::PowerTrack::Rule do
|
12
|
+
subject(:rule_class){ GnipApi::PowerTrack::Rule }
|
13
|
+
before do
|
14
|
+
configure_gem
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'creates a rule' do
|
18
|
+
expect(Proc.new{rule_class.new}).not_to raise_error
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'rule' do
|
22
|
+
let(:value){ 'something' }
|
23
|
+
let(:tag){ 'tag' }
|
24
|
+
let(:rule) {rule_class.new(:value => value, :tag => tag) }
|
25
|
+
|
26
|
+
it('has a value'){ expect(rule.value).to eq(value) }
|
27
|
+
it('has a tag'){ expect(rule.tag).to eq(tag) }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#attributes' do
|
31
|
+
context 'with tag' do
|
32
|
+
let(:rule){ rule_class.new(:value => 'r1', :tag => 't1') }
|
33
|
+
let(:expected_attributes){ {:value => 'r1', :tag => 't1'} }
|
34
|
+
|
35
|
+
it('returns expected attributes'){ expect(rule.attributes).to eq(expected_attributes) }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'without tag' do
|
39
|
+
let(:rule){ rule_class.new(:value => 'r1') }
|
40
|
+
let(:expected_attributes){ {:value => 'r1'} }
|
41
|
+
|
42
|
+
it('returns expected attributes'){ expect(rule.attributes).to eq(expected_attributes) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#to_json' do
|
47
|
+
let(:rule) { rule_class.new(:value => 'value', :tag => 'tag') }
|
48
|
+
let(:json) { {:value => 'value', :tag => 'tag'}.to_json }
|
49
|
+
|
50
|
+
it('converts to json'){ expect(rule.to_json).to eq(json) }
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#consistent?' do
|
54
|
+
let(:rule) { rule_class.new }
|
55
|
+
|
56
|
+
context 'without raising exception' do
|
57
|
+
consistent_values.each do |value|
|
58
|
+
it "returns true for consistent value #{value}" do
|
59
|
+
rule.value = value
|
60
|
+
expect(rule.consistent?).to eq(true)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
inconsistent_values.each do |value|
|
65
|
+
it "returns false for inconsistent value #{value}" do
|
66
|
+
rule.value = value
|
67
|
+
expect(rule.consistent?).to eq(false)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when raising exception' do
|
73
|
+
consistent_values.each do |value|
|
74
|
+
it "returns nil for consistent #{value}" do
|
75
|
+
rule.value = value
|
76
|
+
expect(rule.consistent?(true)).to eq(nil)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
inconsistent_values.each do |value|
|
81
|
+
it "raises ArgumentError for inconsistent value #{value}" do
|
82
|
+
rule.value = value
|
83
|
+
expect(Proc.new{rule.consistent?(true)}).to raise_error(ArgumentError)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe GnipApi::
|
3
|
+
describe GnipApi::PowerTrack::Rules do
|
4
4
|
before do
|
5
|
-
@api = GnipApi::
|
6
|
-
@rules = [GnipApi::
|
7
|
-
GnipApi::
|
5
|
+
@api = GnipApi::PowerTrack::Rules.new(:source => 'source', :label => 'label')
|
6
|
+
@rules = [GnipApi::PowerTrack::Rule.new(:value => 'r1', :tag => 't1'),
|
7
|
+
GnipApi::PowerTrack::Rule.new(:value => 'r2')]
|
8
8
|
@json = {:rules => [{:value => 'r1', :tag => 't1'}, {:value => 'r2'}]}.to_json
|
9
9
|
end
|
10
10
|
|
11
11
|
describe '#parse_rules' do
|
12
|
-
it 'parses json rules to GnipApi::
|
12
|
+
it 'parses json rules to GnipApi::PowerTrack::Rule objects' do
|
13
13
|
parsed = @api.parse_rules(@json)
|
14
|
-
expect(parsed.first.class).to eq(GnipApi::
|
14
|
+
expect(parsed.first.class).to eq(GnipApi::PowerTrack::Rule)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -48,9 +48,9 @@ describe GnipApi::Apis::PowerTrack::Rules do
|
|
48
48
|
expect(result.size).to eq(2)
|
49
49
|
end
|
50
50
|
|
51
|
-
it 'returns GnipApi::
|
51
|
+
it 'returns GnipApi::PowerTrack::Rule objects' do
|
52
52
|
result = @api.list
|
53
|
-
expect(result.map(&:class).uniq).to eq([GnipApi::
|
53
|
+
expect(result.map(&:class).uniq).to eq([GnipApi::PowerTrack::Rule])
|
54
54
|
end
|
55
55
|
|
56
56
|
describe 'rules fetched' do
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GnipApi::PowerTrack::Stream do
|
4
|
+
before do
|
5
|
+
configure_gem
|
6
|
+
@stream = GnipApi::PowerTrack::Stream.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#process_entries' do
|
13
|
+
before do
|
14
|
+
@json = File.read('spec/fixtures/activities/real_activity.json')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'bulds a Message object from the json' do
|
18
|
+
message = @stream.process_entries([@json])
|
19
|
+
expect(message.first.class).to eq(Gnip::Activity)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns empty array if could not parse json' do
|
23
|
+
message = @stream.process_entries(['lol'])
|
24
|
+
expect(message).to eq([])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#parse_json' do
|
29
|
+
before do
|
30
|
+
@json = File.read('spec/fixtures/activities/real_activity.json')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'does not throw error with activity json' do
|
34
|
+
expect(Proc.new{@stream.parse_json(@json)}).not_to raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'parses json' do
|
38
|
+
parsed = @stream.parse_json @json
|
39
|
+
expect(parsed.class).to eq(Hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns nil if empty json is passed' do
|
43
|
+
parsed = @stream.parse_json('')
|
44
|
+
expect(parsed).to eq(nil)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns nil if invalid json is passed' do
|
48
|
+
parsed = @stream.parse_json('12,4.,x_VZxuv8{ak{f}}}}} {{ (d(s)aa(((aaaaa)aaa)')
|
49
|
+
expect(parsed).to eq(nil)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -29,4 +29,31 @@ describe GnipApi::Response do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
32
|
+
|
33
|
+
describe '#check_for_errors!' do
|
34
|
+
let(:request) { GnipApi::Request.new_get(@uri) }
|
35
|
+
context 'when is ok' do
|
36
|
+
let(:response) { GnipApi::Response.new(request, 200, 'good body', {}) }
|
37
|
+
it('does not throw exception') { expect(Proc.new{response.check_for_errors!}).not_to raise_error }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when rate limited' do
|
41
|
+
let(:error_message){ {"error"=>{"message"=>"Exceeded rate limit", "sent"=>"2017-06-05T16:20:43+00:00", "transactionId"=>"00629e29000a91c1"}}.to_json }
|
42
|
+
let(:response) { GnipApi::Response.new(request, 429, error_message, {}) }
|
43
|
+
it('raises RateLimitError') { expect(Proc.new{response.check_for_errors!}).to raise_error(GnipApi::Errors::Adapter::RateLimitError) }
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'when software error' do
|
47
|
+
let(:error_message){ {"error"=>{"message"=>"some server error", "sent"=>"2017-06-05T16:20:43+00:00", "transactionId"=>"00629e29000a91c1"}}.to_json }
|
48
|
+
let(:response) { GnipApi::Response.new(request, 503, error_message, {}) }
|
49
|
+
it('raises GnipSoftwareError') { expect(Proc.new{response.check_for_errors!}).to raise_error(GnipApi::Errors::Adapter::GnipSoftwareError) }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when other error' do
|
53
|
+
let(:error_message){ {"error"=>{"message"=>"Exceeded rate limit", "sent"=>"2017-06-05T16:20:43+00:00", "transactionId"=>"00629e29000a91c1"}}.to_json }
|
54
|
+
let(:response) { GnipApi::Response.new(request, 400, error_message, {}) }
|
55
|
+
it('raises generic error') { expect(Proc.new{response.check_for_errors!}).to raise_error(GnipApi::Errors::Adapter::RequestError) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
32
59
|
end
|
@@ -1,18 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe GnipApi::
|
3
|
+
describe GnipApi::Search do
|
4
4
|
describe '.new' do
|
5
5
|
it 'creates search instance' do
|
6
|
-
object = GnipApi::
|
6
|
+
object = GnipApi::Search.new
|
7
7
|
expect(object).not_to eq(nil)
|
8
|
-
expect(object.class).to eq(GnipApi::
|
8
|
+
expect(object.class).to eq(GnipApi::Search)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
describe '#counts' do
|
13
13
|
before do
|
14
14
|
@raw_response = File.read(fixture_path.join('search_api/search_counts_response.json'))
|
15
|
-
@api = GnipApi::
|
15
|
+
@api = GnipApi::Search.new
|
16
16
|
allow(@api.adapter).to receive(:post).and_return(@raw_response)
|
17
17
|
end
|
18
18
|
|
@@ -21,14 +21,14 @@ describe GnipApi::Apis::Search do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'performs request and parses response' do
|
24
|
-
rule = GnipApi::
|
24
|
+
rule = GnipApi::PowerTrack::Rule.new :value => 'lolcat OR madcat'
|
25
25
|
result = @api.counts :rule => rule
|
26
26
|
expect(result).not_to eq(nil)
|
27
27
|
end
|
28
28
|
|
29
29
|
describe 'result set' do
|
30
30
|
before do
|
31
|
-
rule = GnipApi::
|
31
|
+
rule = GnipApi::PowerTrack::Rule.new :value => 'lolcat OR madcat'
|
32
32
|
@result = @api.counts :rule => rule
|
33
33
|
end
|
34
34
|
|
@@ -65,7 +65,7 @@ describe GnipApi::Apis::Search do
|
|
65
65
|
describe '#activities' do
|
66
66
|
before do
|
67
67
|
@raw_response = File.read(fixture_path.join('search_api/search_activities_response.json'))
|
68
|
-
@api = GnipApi::
|
68
|
+
@api = GnipApi::Search.new
|
69
69
|
allow(@api.adapter).to receive(:post).and_return(@raw_response)
|
70
70
|
end
|
71
71
|
|
@@ -74,14 +74,14 @@ describe GnipApi::Apis::Search do
|
|
74
74
|
end
|
75
75
|
|
76
76
|
it 'performs request and parses response' do
|
77
|
-
rule = GnipApi::
|
77
|
+
rule = GnipApi::PowerTrack::Rule.new :value => 'lolcat OR madcat'
|
78
78
|
result = @api.activities :rule => rule
|
79
79
|
expect(result).not_to eq(nil)
|
80
80
|
end
|
81
81
|
|
82
82
|
describe 'result set' do
|
83
83
|
before do
|
84
|
-
rule = GnipApi::
|
84
|
+
rule = GnipApi::PowerTrack::Rule.new :value => 'lolcat OR madcat'
|
85
85
|
@result = @api.activities :rule => rule
|
86
86
|
end
|
87
87
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'gnip_api'
|
2
2
|
require 'pry'
|
3
|
-
require 'lib/test_adapter.rb'
|
4
3
|
require 'timecop'
|
5
4
|
|
6
5
|
def configure_gem
|
@@ -8,14 +7,16 @@ def configure_gem
|
|
8
7
|
config.user = "someone"
|
9
8
|
config.password = "lolcat"
|
10
9
|
config.account = "something"
|
11
|
-
config.adapter_class = TestAdapter
|
12
10
|
config.source = 'twitter'
|
13
11
|
config.label = 'prod'
|
14
12
|
config.debug = false
|
15
|
-
config.stream_output_format = :activity
|
16
13
|
end
|
17
14
|
end
|
18
15
|
|
19
16
|
def fixture_path
|
20
17
|
Pathname.new('spec/fixtures')
|
21
18
|
end
|
19
|
+
|
20
|
+
def read_fixture path
|
21
|
+
File.read(fixture_path.join(path))
|
22
|
+
end
|