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 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.2'
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
@@ -1 +1,10 @@
1
- require 'bundler/gem_tasks'
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc "Run all RSpec tests"
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
10
+ task :test => [:spec]
@@ -41,5 +41,4 @@ Gem::Specification.new do |s|
41
41
  s.add_dependency 'rest-client', '~> 1.6.7'
42
42
  s.add_dependency 'json', '~> 1.7.0'
43
43
  s.add_dependency 'jruby-openssl' if RUBY_PLATFORM == 'java'
44
- s.add_dependency 'openssl' if RUBY_PLATFORM == 'ruby'
45
44
  end
@@ -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(value, tag=nil)
17
- post(@url, jsonify_rules(value, tag))
15
+ def add(input, tag=nil)
16
+ post(@url, gen_json_payload(input, tag))
18
17
  end
19
18
 
20
- def delete(value, tag=nil)
21
- post("#@url?_method=delete", jsonify_rules(value, tag))
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 jsonify_rules(values, tag=nil)
32
- rules = nil
33
- if values.instance_of?(Array)
34
- rules = values.collect { |o| o.instance_of?(Rule) ? o : Rule.new(o, tag) }
35
- elsif values.instance_of?(Rule)
36
- rules = [values]
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
@@ -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
- if contains_stop_word?(@value) || too_long?(@value) || contains_negated_or?(@value) || too_many_positive_terms?(@value) || contains_empty_source?(@value)
14
- return false
15
- end
16
- true
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 as_json
20
- as_hash.to_json
32
+ def to_json
33
+ to_hash.to_json
21
34
  end
22
35
 
23
- def as_hash
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
- as_json
43
+ to_json
31
44
  end
32
45
 
33
46
  protected
34
- def contains_stop_word?(value)
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?(value)
40
- value.size > 1024
52
+ def too_long?
53
+ @value.size > 1024
41
54
  end
42
55
 
43
- def contains_negated_or?(value)
44
- !value[/\-\w+ OR/].nil?
56
+ def contains_negated_or?
57
+ !@value[/\-\w+ OR/].nil?
45
58
  end
46
59
 
47
- def too_many_positive_terms?(value)
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?(value)
52
- !value[/source\:\s/].nil?
64
+ def contains_empty_source?
65
+ !@value[/source\:\s/].nil?
53
66
  end
54
67
  end
55
68
  end
@@ -1,3 +1,3 @@
1
1
  module GnipRule
2
- VERSION = '0.3'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -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 '#initialize' do
10
+ describe '.new' do
11
11
  it 'should convert XML URLs to JSON' do
12
- subject.url.should == base_url
12
+ GnipRule::Client.new(xml_base_url, 'username', 'password').url.should == base_url
13
13
  end
14
- end
15
14
 
16
- describe '#add' do
17
- it 'should POST rule to the given URL with given credentials' do
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 '#delete' do
33
- it 'should POST rule json to a URL with given credentials' do
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
- describe '#list' do
42
- it 'should GET a URL with given credentials' do
43
- stub_request(:get, base_url_with_auth).
44
- to_return(:status => 200, :body => '{"rules":[{"value":"foo","tag":"baz"},{"value":"bar","tag":"baz"}]}')
45
- rules = subject.list()
46
- rules.size.should == 2
47
- rules.map { |r| r.valid?.should == true }
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
- it 'should raise an error if Gnip returns HTTP error' do
51
- stub_request(:get, base_url).
52
- with(:body => '{"rules":[{"value":"value","tag":"tag"}]}').
53
- to_return(:status => 401, :body => 'Error message', :headers => {})
54
- lambda { subject.list() }.should raise_error
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
- describe '#jsonify_rules' do
59
- it 'should JSONify Strings' do
60
- json = subject.jsonify_rules('foo', 'bar')
61
- json.should == '{"rules":[{"value":"foo","tag":"bar"}]}'
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
- it 'should JSONify Rules' do
65
- json = subject.jsonify_rules(GnipRule::Rule.new('baz', 'foo'))
66
- json.should == '{"rules":[{"value":"baz","tag":"foo"}]}'
67
- end
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
- it 'should JSONify an Array of Strings with a tag' do
70
- json = subject.jsonify_rules(['foo', 'bar'], 'baz')
71
- json.should == '{"rules":[{"value":"foo","tag":"baz"},{"value":"bar","tag":"baz"}]}'
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
- it 'should JSONify an Array of Rules' do
75
- rule1 = GnipRule::Rule.new('baz', 'foo')
76
- rule2 = GnipRule::Rule.new('bar', 'thing')
77
- json = subject.jsonify_rules([rule1, rule2])
78
- json.should == '{"rules":[{"value":"baz","tag":"foo"},{"value":"bar","tag":"thing"}]}'
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
@@ -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 == nil
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
- rule = GnipRule::Rule.new('an value')
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.valid?.should == false
22
+ rule.should_not be_valid
24
23
  end
25
24
  it 'should consider empty sources to be invalid' do
26
- rule = GnipRule::Rule.new('foo source: bar')
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
- rule = GnipRule::Rule.new('-iphone OR ipad')
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
- rule = GnipRule::Rule.new('one two three four five six seven eight nine ten eleven')
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
- rule = GnipRule::Rule.new('"foo the bar" baz')
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
- rule = GnipRule::Rule.new('value')
43
- rule.valid?.should == true
37
+ GnipRule::Rule.new('value').should be_valid
44
38
  end
45
39
  end
46
40
 
47
- describe '#as_json' do
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.as_json.should == '{"value":"foo"}'
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.as_json.should == '{"value":"foo","tag":"bar"}'
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
@@ -3,3 +3,5 @@ Bundler.setup
3
3
 
4
4
  require 'rspec'
5
5
  require 'webmock/rspec'
6
+
7
+ WebMock.disable_net_connect!
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: '0.3'
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-02 00:00:00.000000000 Z
12
+ date: 2012-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake