gnip-rule 0.3 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -2
- data/Rakefile +10 -1
- data/gnip-rule.gemspec +0 -1
- data/lib/gnip-rule/client.rb +10 -15
- data/lib/gnip-rule/rule.rb +31 -18
- data/lib/gnip-rule/version.rb +1 -1
- data/spec/gnip-rule/client_spec.rb +63 -54
- data/spec/gnip-rule/rule_spec.rb +18 -17
- data/spec/spec-helper.rb +2 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -5,7 +5,7 @@ This gem simplifies the effort to add/delete/list rules using the [Gnip Rules AP
|
|
5
5
|
`gem install gnip-rule` or add the following to your `Gemfile`:
|
6
6
|
|
7
7
|
```ruby
|
8
|
-
gem 'gnip-rule', '~> 0.
|
8
|
+
gem 'gnip-rule', '~> 0.3.1'
|
9
9
|
```
|
10
10
|
|
11
11
|
## Usage
|
@@ -28,7 +28,10 @@ rules.delete(['foo', 'bar'])
|
|
28
28
|
rules.delete(GnipRule::Rule.new('value', 'tag'))
|
29
29
|
|
30
30
|
# Get all rules
|
31
|
-
rules.list()
|
31
|
+
rules_list = rules.list()
|
32
|
+
rules_list.each { |rule| rule.valid? }
|
33
|
+
jsonified = rules_list.map { |rule| rule.as_json `}
|
34
|
+
rules_tagged_foo = rules_list.select { |rule| rule.tag == 'foo' }
|
32
35
|
```
|
33
36
|
|
34
37
|
## License
|
data/Rakefile
CHANGED
data/gnip-rule.gemspec
CHANGED
data/lib/gnip-rule/client.rb
CHANGED
@@ -4,7 +4,6 @@ require 'gnip-rule/rule'
|
|
4
4
|
|
5
5
|
module GnipRule
|
6
6
|
class Client
|
7
|
-
|
8
7
|
attr_reader :url, :username, :password
|
9
8
|
|
10
9
|
def initialize(url, username, password)
|
@@ -13,12 +12,12 @@ module GnipRule
|
|
13
12
|
@password = password
|
14
13
|
end
|
15
14
|
|
16
|
-
def add(
|
17
|
-
post(@url,
|
15
|
+
def add(input, tag=nil)
|
16
|
+
post(@url, gen_json_payload(input, tag))
|
18
17
|
end
|
19
18
|
|
20
|
-
def delete(
|
21
|
-
post("#@url?_method=delete",
|
19
|
+
def delete(input, tag=nil)
|
20
|
+
post("#@url?_method=delete", gen_json_payload(input, tag))
|
22
21
|
end
|
23
22
|
|
24
23
|
def list()
|
@@ -28,16 +27,12 @@ module GnipRule
|
|
28
27
|
JSON.parse(response.to_s)['rules'].collect { |o| Rule.new(o['value'], o['tag']) }
|
29
28
|
end
|
30
29
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
else
|
38
|
-
rules = [Rule.new(values, tag)]
|
39
|
-
end
|
40
|
-
{:rules => rules.collect(&:as_hash)}.to_json
|
30
|
+
def gen_json_payload(input, tag=nil)
|
31
|
+
input = [input] unless input.respond_to? :collect
|
32
|
+
{:rules => input.collect { |i|
|
33
|
+
raise 'Input must be convertable to GnipRule::Rule' unless i.respond_to? :to_rule
|
34
|
+
i.to_rule(tag).to_hash
|
35
|
+
}}.to_json
|
41
36
|
end
|
42
37
|
|
43
38
|
protected
|
data/lib/gnip-rule/rule.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
+
class String
|
4
|
+
def to_rule(tag=nil)
|
5
|
+
GnipRule::Rule.new(self, tag)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
3
9
|
module GnipRule
|
4
10
|
class Rule
|
5
11
|
attr_accessor :value, :tag
|
@@ -10,46 +16,53 @@ module GnipRule
|
|
10
16
|
end
|
11
17
|
|
12
18
|
def valid?
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
19
|
+
return false if too_long?
|
20
|
+
return false if too_many_positive_terms?
|
21
|
+
return false if contains_stop_word?
|
22
|
+
return false if contains_empty_source?
|
23
|
+
return false if contains_negated_or?
|
24
|
+
return true
|
25
|
+
end
|
26
|
+
|
27
|
+
# NOTE: Unused variable for consistency with other to_rule impls
|
28
|
+
def to_rule(_=nil)
|
29
|
+
self
|
17
30
|
end
|
18
31
|
|
19
|
-
def
|
20
|
-
|
32
|
+
def to_json
|
33
|
+
to_hash.to_json
|
21
34
|
end
|
22
35
|
|
23
|
-
def
|
36
|
+
def to_hash
|
24
37
|
obj = {:value => @value}
|
25
38
|
obj[:tag] = @tag unless @tag.nil?
|
26
39
|
obj
|
27
40
|
end
|
28
41
|
|
29
42
|
def to_s
|
30
|
-
|
43
|
+
to_json
|
31
44
|
end
|
32
45
|
|
33
46
|
protected
|
34
|
-
def contains_stop_word?
|
47
|
+
def contains_stop_word?
|
35
48
|
stop_words = %W(a an and at but by com from http https if in is it its me my or rt the this to too via we www you)
|
36
|
-
(stop_words & value.gsub(/\"[^\"]*\"/, '').split(/\s/)).size > 0
|
49
|
+
(stop_words & @value.gsub(/\"[^\"]*\"/, '').split(/\s/)).size > 0
|
37
50
|
end
|
38
51
|
|
39
|
-
def too_long?
|
40
|
-
value.size > 1024
|
52
|
+
def too_long?
|
53
|
+
@value.size > 1024
|
41
54
|
end
|
42
55
|
|
43
|
-
def contains_negated_or?
|
44
|
-
|
56
|
+
def contains_negated_or?
|
57
|
+
!@value[/\-\w+ OR/].nil?
|
45
58
|
end
|
46
59
|
|
47
|
-
def too_many_positive_terms?
|
48
|
-
value.scan(/\b\w+|\"[\-\s\w]+\"\b/).size > 10
|
60
|
+
def too_many_positive_terms?
|
61
|
+
@value.scan(/\b\w+|\"[\-\s\w]+\"\b/).size > 10
|
49
62
|
end
|
50
63
|
|
51
|
-
def contains_empty_source?
|
52
|
-
|
64
|
+
def contains_empty_source?
|
65
|
+
!@value[/source\:\s/].nil?
|
53
66
|
end
|
54
67
|
end
|
55
68
|
end
|
data/lib/gnip-rule/version.rb
CHANGED
@@ -3,79 +3,88 @@ require 'spec-helper'
|
|
3
3
|
require 'gnip-rule/client'
|
4
4
|
|
5
5
|
describe GnipRule::Client do
|
6
|
+
let(:xml_base_url) { 'https://api.gnip.com:443/accounts/foo/publishers/twitter/streams/track/prod/rules.xml' }
|
6
7
|
let(:base_url) { 'https://api.gnip.com:443/accounts/foo/publishers/twitter/streams/track/prod/rules.json' }
|
7
8
|
let(:base_url_with_auth) { 'https://username:password@api.gnip.com:443/accounts/foo/publishers/twitter/streams/track/prod/rules.json' }
|
8
|
-
subject { GnipRule::Client.new(base_url, 'username', 'password') }
|
9
9
|
|
10
|
-
describe '
|
10
|
+
describe '.new' do
|
11
11
|
it 'should convert XML URLs to JSON' do
|
12
|
-
|
12
|
+
GnipRule::Client.new(xml_base_url, 'username', 'password').url.should == base_url
|
13
13
|
end
|
14
|
-
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
stub_request(:post, base_url_with_auth).
|
19
|
-
with(:body => '{"rules":[{"value":"value","tag":"tag"}]}').
|
20
|
-
to_return(:status => 200, :body => '', :headers => {})
|
21
|
-
subject.add('value', 'tag')
|
15
|
+
it 'should not munch JSON URLs' do
|
16
|
+
GnipRule::Client.new(base_url, 'username', 'password').url.should == base_url
|
22
17
|
end
|
23
18
|
|
24
|
-
it 'should raise an error if Gnip returns HTTP error' do
|
25
|
-
stub_request(:post, base_url_with_auth).
|
26
|
-
with(:body => '{"rules":[{"value":"value","tag":"tag"}]}').
|
27
|
-
to_return(:status => 401, :body => 'Error message', :headers => {})
|
28
|
-
lambda { subject.add('value', 'tag') }.should raise_error
|
29
|
-
end
|
30
19
|
end
|
31
20
|
|
32
|
-
describe '
|
33
|
-
|
34
|
-
stub_request(:post, "#{base_url_with_auth}?_method=delete").
|
35
|
-
with(:body => '{"rules":[{"value":"value","tag":"tag"}]}').
|
36
|
-
to_return(:status => 200, :body => '', :headers => {})
|
37
|
-
subject.delete('value', 'tag')
|
38
|
-
end
|
39
|
-
end
|
21
|
+
describe 'instance' do
|
22
|
+
subject { GnipRule::Client.new(base_url, 'username', 'password') }
|
40
23
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
24
|
+
describe '#add' do
|
25
|
+
it 'should POST rule to the given URL with given credentials' do
|
26
|
+
stub_request(:post, base_url_with_auth).
|
27
|
+
with(:body => '{"rules":[{"value":"value","tag":"tag"}]}').
|
28
|
+
to_return(:status => 200, :body => '', :headers => {})
|
29
|
+
subject.add('value', 'tag')
|
30
|
+
end
|
49
31
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
32
|
+
it 'should raise an error if Gnip returns HTTP error' do
|
33
|
+
stub_request(:post, base_url_with_auth).
|
34
|
+
with(:body => '{"rules":[{"value":"value","tag":"tag"}]}').
|
35
|
+
to_return(:status => 401, :body => 'Error message', :headers => {})
|
36
|
+
lambda { subject.add('value', 'tag') }.should raise_error
|
37
|
+
end
|
55
38
|
end
|
56
|
-
end
|
57
39
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
40
|
+
describe '#delete' do
|
41
|
+
it 'should POST rule json to a URL with given credentials' do
|
42
|
+
stub_request(:post, "#{base_url_with_auth}?_method=delete").
|
43
|
+
with(:body => '{"rules":[{"value":"value","tag":"tag"}]}').
|
44
|
+
to_return(:status => 200, :body => '', :headers => {})
|
45
|
+
subject.delete('value', 'tag')
|
46
|
+
end
|
62
47
|
end
|
63
48
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
49
|
+
describe '#list' do
|
50
|
+
it 'should GET a URL with given credentials' do
|
51
|
+
stub_request(:get, base_url_with_auth).
|
52
|
+
to_return(:status => 200, :body => '{"rules":[{"value":"foo","tag":"baz"},{"value":"bar","tag":"baz"}]}')
|
53
|
+
rules = subject.list()
|
54
|
+
rules.size.should == 2
|
55
|
+
rules.map { |r| r.valid?.should == true }
|
56
|
+
end
|
68
57
|
|
69
|
-
|
70
|
-
|
71
|
-
|
58
|
+
it 'should raise an error if Gnip returns HTTP error' do
|
59
|
+
stub_request(:get, base_url).
|
60
|
+
with(:body => '{"rules":[{"value":"value","tag":"tag"}]}').
|
61
|
+
to_return(:status => 401, :body => 'Error message', :headers => {})
|
62
|
+
lambda { subject.list() }.should raise_error
|
63
|
+
end
|
72
64
|
end
|
73
65
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
66
|
+
describe '#jsonify_rules' do
|
67
|
+
it 'should JSONify Strings' do
|
68
|
+
json = subject.gen_json_payload('foo', 'bar')
|
69
|
+
json.should == '{"rules":[{"value":"foo","tag":"bar"}]}'
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should JSONify Rules' do
|
73
|
+
json = subject.gen_json_payload(GnipRule::Rule.new('baz', 'foo'))
|
74
|
+
json.should == '{"rules":[{"value":"baz","tag":"foo"}]}'
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should JSONify an Array of Strings with a tag' do
|
78
|
+
json = subject.gen_json_payload(['foo', 'bar'], 'baz')
|
79
|
+
json.should == '{"rules":[{"value":"foo","tag":"baz"},{"value":"bar","tag":"baz"}]}'
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should JSONify an Array of Rules' do
|
83
|
+
rule1 = GnipRule::Rule.new('baz', 'foo')
|
84
|
+
rule2 = GnipRule::Rule.new('bar', 'thing')
|
85
|
+
json = subject.gen_json_payload([rule1, rule2])
|
86
|
+
json.should == '{"rules":[{"value":"baz","tag":"foo"},{"value":"bar","tag":"thing"}]}'
|
87
|
+
end
|
79
88
|
end
|
80
89
|
end
|
81
90
|
end
|
data/spec/gnip-rule/rule_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe GnipRule::Rule do
|
|
6
6
|
describe '#initialize' do
|
7
7
|
it 'should allow an optional tag' do
|
8
8
|
rule = GnipRule::Rule.new('value')
|
9
|
-
rule.tag.should
|
9
|
+
rule.tag.should be_nil
|
10
10
|
|
11
11
|
rule_with_tag = GnipRule::Rule.new('value', 'tag')
|
12
12
|
rule_with_tag.tag.should == 'tag'
|
@@ -15,43 +15,44 @@ describe GnipRule::Rule do
|
|
15
15
|
|
16
16
|
describe '#valid?' do
|
17
17
|
it 'should consider rules with "stop words" to be invalid' do
|
18
|
-
|
19
|
-
rule.valid?.should == false
|
18
|
+
GnipRule::Rule.new('an value').should_not be_valid
|
20
19
|
end
|
21
20
|
it 'should consider rules that are too long to be invalid' do
|
22
21
|
rule = GnipRule::Rule.new('01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789')
|
23
|
-
rule.
|
22
|
+
rule.should_not be_valid
|
24
23
|
end
|
25
24
|
it 'should consider empty sources to be invalid' do
|
26
|
-
|
27
|
-
rule.valid?.should == false
|
25
|
+
GnipRule::Rule.new('foo source: bar').should_not be_valid
|
28
26
|
end
|
29
27
|
it 'should consider negative ORs to be invalid' do
|
30
|
-
|
31
|
-
rule.valid?.should == false
|
28
|
+
GnipRule::Rule.new('-iphone OR ipad').should_not be_valid
|
32
29
|
end
|
33
30
|
it 'should consider rules with more than 10 positive terms invalid' do
|
34
|
-
|
35
|
-
rule.valid?.should == false
|
31
|
+
GnipRule::Rule.new('one two three four five six seven eight nine ten eleven').should_not be_valid
|
36
32
|
end
|
37
33
|
it 'should consider stop words within quotes to be valid' do
|
38
|
-
|
39
|
-
rule.valid?.should == true
|
34
|
+
GnipRule::Rule.new('"foo the bar" baz').should be_valid
|
40
35
|
end
|
41
36
|
it 'should consider rules without "stop words" to be valid' do
|
42
|
-
|
43
|
-
rule.valid?.should == true
|
37
|
+
GnipRule::Rule.new('value').should be_valid
|
44
38
|
end
|
45
39
|
end
|
46
40
|
|
47
|
-
describe '#
|
41
|
+
describe '#to_json' do
|
48
42
|
it 'should omit tag if tag not defined' do
|
49
43
|
rule = GnipRule::Rule.new('foo')
|
50
|
-
rule.
|
44
|
+
rule.to_json.should == '{"value":"foo"}'
|
51
45
|
end
|
52
46
|
it 'should add tag if defined' do
|
53
47
|
rule = GnipRule::Rule.new('foo', 'bar')
|
54
|
-
rule.
|
48
|
+
rule.to_json.should == '{"value":"foo","tag":"bar"}'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#to_rule' do
|
53
|
+
it 'should return itself' do
|
54
|
+
rule = GnipRule::Rule.new('foo', 'bar')
|
55
|
+
rule.to_rule.should == rule
|
55
56
|
end
|
56
57
|
end
|
57
58
|
end
|
data/spec/spec-helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gnip-rule
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|