bluebird 0.1.0 → 0.2.0

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
- NzdhMmI0OGJlM2U2ZjEwY2IxMzg0ZmJlZmNjOTNhYzg5MTQxOWY3Yg==
4
+ OWQ5YzRmNjg2NjhlNDRlZDk2NjZhNWM1MmMyOGUwZmRjZWI3NmUzMg==
5
5
  data.tar.gz: !binary |-
6
- YTA5YmQ1MjQxNWE5MzBhNjQwNzc5NzRiY2QyNzQ2MTEzNDZjZDY0YQ==
6
+ YjUxZjAwMDM0MjljNjE2NzQ2MTc1ZmNhYWMxYmZkM2QxZTQ3ZTVkYw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NTIxM2QzMGExZGYxNjY0ZTRhMmZkZDc5N2M1MDllODA2ZTliMjQzZWZiNzY4
10
- ZDczYzJkNjdkN2JkZTczNTI1OGQ3OTYyYTc1ZmY5OThiZjhkYjI1ODczNTAy
11
- OWU1YzIwYmJjM2M1ZTNjMmQ1OGMzNjkxZGFkZGMwZjI0YTEwMTU=
9
+ YThhNjgwNmYwZWJhMWYwZmRlMDNjY2JkN2I1ZmY2NDc4MjhhNTY4YWY0OTNk
10
+ NTcwOGVmNjRlM2I3MDNmNDE5ZDE2YWQ2OTBkOTc4ZTM0NTMxZjBlNjM0MzEz
11
+ ZDE4NjYyOGRlZDRmOTk3YWU3MTUyZTc1NTkwZDU0ZWRkYzg2ZjM=
12
12
  data.tar.gz: !binary |-
13
- NzFiYTYxY2M3ZmQ4YTlmNjZhNDE4MmUwZDQzMmYxMjUxMjM4MDRjMTI1Y2Ey
14
- MjIzYzU4MzE2ODU5ODJjM2UwYzUzYjRiMGQwYTgxZTk3ZTUxYjc1YzA2YTA2
15
- YTdlNTRiMGJhYmMyNzVmMmRiMWFhMDhiZmIwNjE0NzU0NDdhN2Q=
13
+ OWFlZGQ5MDdkMWY5MmFlNTg3OThkMWE5MzM2YTZhYWJlZmQ4MWQ0N2M0ZDBj
14
+ ZjE4YmMwOTZmYjgwMzE5MjMwYTY5M2VjYjU5OTI5ZThhYmI0MmYyN2ExN2Mz
15
+ OTgzNzUyYzlhODIwMWIwYjIwNDAwNDYwYmRhNDIwZGM3OGZiMGY=
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0 (March 13, 2014)
4
+
5
+ * Feature: via strategy added
6
+
7
+ ## 0.1.0 (March 4, 2014)
8
+
9
+ * initial release
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Bluebird
1
+ # Bluebird [![Gem Version](https://emrekutlu-test-1.appspot.com/gems/bluebird/version.png?right_bg=4183c4)](https://rubygems.org/gems/bluebird)
2
2
 
3
3
  Bluebird modifies your tweets, mostly to fit 140 characters.
4
4
  It uses "strategies". Every strategy makes its own modifications to the tweets.
@@ -41,7 +41,7 @@ end
41
41
 
42
42
  If you want to change defaults for Rails, create a file in <code>initializers</code> folder and copy the code above.
43
43
 
44
- By default bluebird runs three strategies in these order; <code>strip</code>, <code>squeeze</code> and <code>truncate</code>. For detailed information about each strategy, visit [Strategies](https://github.com/emrekutlu/bluebird/wiki/Strategies).
44
+ By default bluebird runs three strategies in these order; <code>strip</code>, <code>squeeze</code> and <code>truncate_text</code>. For detailed information about each strategy, visit [Strategies](https://github.com/emrekutlu/bluebird/wiki/Strategies).
45
45
 
46
46
  ## Usage
47
47
 
@@ -8,6 +8,7 @@ require 'bluebird/strategies/base'
8
8
  require 'bluebird/strategies/strip'
9
9
  require 'bluebird/strategies/squeeze'
10
10
  require 'bluebird/strategies/truncate_text'
11
+ require 'bluebird/strategies/via'
11
12
 
12
13
  module Bluebird
13
14
  class << self
@@ -1,6 +1,7 @@
1
1
  module Bluebird
2
2
  class Partial
3
3
 
4
+ attr_reader :partial_type
4
5
  attr_accessor :content, :prev_partial, :next_partial
5
6
 
6
7
  def initialize(content, partial_type)
@@ -0,0 +1,4 @@
1
+ require 'bluebird/strategies/via/strategy'
2
+ require 'bluebird/strategies/via/config'
3
+
4
+ Bluebird::Config.register(:via, Bluebird::Strategies::Via::Config)
@@ -0,0 +1,15 @@
1
+ module Bluebird
2
+ module Strategies
3
+ module Via
4
+ class Config
5
+
6
+ @prefix = 'via'
7
+
8
+ class << self
9
+ attr_accessor :prefix, :username
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,62 @@
1
+ module Bluebird
2
+ module Strategies
3
+ module Via
4
+ class Strategy < Bluebird::Strategies::Base
5
+ class << self
6
+
7
+ def run(tweet, config)
8
+ if run?(config.via.username)
9
+ add_separator(tweet)
10
+ tweet.add_partial(text(config.via), :via)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def run?(username)
17
+ username_exists?(username) ? true : raise('Bluebird: config.via.username is missing.')
18
+ end
19
+
20
+ def add_separator(tweet)
21
+ if add_separator?(tweet)
22
+ add_separator_partial?(tweet) ? add_separator_partial(tweet) : add_space_to_last_partial(tweet)
23
+ end
24
+ end
25
+
26
+ def text(via_config)
27
+ "#{via_config.prefix} @#{via_config.username}"
28
+ end
29
+
30
+ def username_exists?(username)
31
+ if username
32
+ username.strip!
33
+ !username.eql?('')
34
+ else
35
+ false
36
+ end
37
+ end
38
+
39
+ def add_separator_partial?(tweet)
40
+ !tweet.partials.last.text?
41
+ end
42
+
43
+ def add_separator?(tweet)
44
+ tweet.partials.length > 0
45
+ end
46
+
47
+ def add_separator_partial(tweet)
48
+ tweet.add_partial(' ', :text)
49
+ end
50
+
51
+ def add_space_to_last_partial(tweet)
52
+ last = tweet.partials.last
53
+ unless last.content.end_with?(' ')
54
+ last.content += ' '
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -7,7 +7,8 @@ module Bluebird
7
7
 
8
8
  def initialize(status, opts = {})
9
9
  @original_status = status
10
- @partials = extract_partials(status)
10
+ @partials = []
11
+ extract_partials(status)
11
12
  @media = opts[:media] if opts.has_key?(:media)
12
13
  end
13
14
 
@@ -39,6 +40,19 @@ module Bluebird
39
40
  partials.select { |partial| partial.cashtag? }
40
41
  end
41
42
 
43
+ def add_partial(content, partial_type)
44
+ if eligible_content?(content)
45
+ partial = Partial.new(content, partial_type)
46
+
47
+ if (last = partials.last)
48
+ last.next_partial = partial
49
+ partial.prev_partial = last
50
+ end
51
+
52
+ partials << partial
53
+ end
54
+ end
55
+
42
56
  private
43
57
 
44
58
  def total_partial_length
@@ -59,35 +73,19 @@ module Bluebird
59
73
  entities = extract_entities_with_indices(status, extract_url_without_protocol: true)
60
74
  length = status.char_length
61
75
  index = 0
62
- partials = []
63
76
 
64
77
  entities.each do |entity|
65
78
  first = entity[:indices].first
66
79
  last = entity[:indices].last
67
80
 
68
- add_partial(partials, status[index, first - index], :text)
69
- add_partial(partials, status[first, last - first], entity_type(entity))
81
+ add_partial(status[index, first - index], :text)
82
+ add_partial(status[first, last - first], entity_type(entity))
70
83
 
71
84
  index = last
72
85
  end
73
86
 
74
87
  if ending_partial?(index, length)
75
- add_partial(partials, status[index, length - index], :text)
76
- end
77
-
78
- partials
79
- end
80
-
81
- def add_partial(partials, content, partial_type)
82
- if eligible_content?(content)
83
- partial = Partial.new(content, partial_type)
84
-
85
- if (last = partials.last)
86
- last.next_partial = partial
87
- partial.prev_partial = last
88
- end
89
-
90
- partials << partial
88
+ add_partial(status[index, length - index], :text)
91
89
  end
92
90
  end
93
91
 
@@ -1,3 +1,3 @@
1
1
  module Bluebird
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bluebird::Strategies::Via::Strategy do
4
+
5
+ let!(:strategy) { Bluebird::Strategies::Via::Strategy }
6
+
7
+ describe '#username_exists?' do
8
+ context 'When username is nil' do
9
+ it 'returns false' do
10
+ expect(strategy.send(:username_exists?, nil)).to be_false
11
+ end
12
+ end
13
+ context "When username is ''" do
14
+ it 'returns false' do
15
+ expect(strategy.send(:username_exists?, '')).to be_false
16
+ end
17
+ end
18
+ context "When username is ' '" do
19
+ it 'returns false' do
20
+ expect(strategy.send(:username_exists?, ' ')).to be_false
21
+ end
22
+ end
23
+ context 'When username is iekutlu' do
24
+ it 'returns false' do
25
+ expect(strategy.send(:username_exists?, 'iekutlu')).to be_true
26
+ end
27
+ end
28
+ end
29
+
30
+ describe '#text' do
31
+ it "adds 'prefix' to the begining" do
32
+ config = Bluebird::Strategies::Via::Config
33
+ config.username = 'iekutlu'
34
+ config.prefix = 'via'
35
+ expect(strategy.send(:text, config).start_with?('via')).to be_true
36
+ end
37
+ it 'adds username to the end' do
38
+ config = Bluebird::Strategies::Via::Config
39
+ config.username = 'iekutlu'
40
+ expect(strategy.send(:text, config).end_with?('@iekutlu')).to be_true
41
+ end
42
+ end
43
+
44
+ describe '#add_separator?' do
45
+ context 'When tweet is not empty' do
46
+ it 'returns true' do
47
+ tweet = Bluebird::Tweet.new('Lorem ipsum')
48
+ expect(strategy.send(:add_separator?, tweet)).to be_true
49
+ end
50
+ end
51
+ context 'When tweet is empty' do
52
+ it 'returns false' do
53
+ tweet = Bluebird::Tweet.new('')
54
+ expect(strategy.send(:add_separator?, tweet)).to be_false
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#add_separator_partial' do
60
+ it "adds a new text partial whose content is ' '" do
61
+ tweet = Bluebird::Tweet.new('Lorem #ipsum')
62
+ strategy.send(:add_separator_partial, tweet)
63
+ last = tweet.partials.last
64
+ expect(last.text?).to be_true
65
+ expect(last.content).to eq ' '
66
+ end
67
+ end
68
+
69
+ describe '#add_space_to_last_partial' do
70
+ context 'When text partial has trailing space' do
71
+ it 'does not add trailing space' do
72
+ tweet = Bluebird::Tweet.new('Lorem ipsum ')
73
+ strategy.send(:add_space_to_last_partial, tweet)
74
+ expect(tweet.status).to eq 'Lorem ipsum '
75
+ end
76
+ end
77
+ context 'When text partial does not have trailing space' do
78
+ it 'adds trailing space' do
79
+ tweet = Bluebird::Tweet.new('Lorem ipsum')
80
+ strategy.send(:add_space_to_last_partial, tweet)
81
+ expect(tweet.status).to eq 'Lorem ipsum '
82
+ end
83
+ end
84
+ end
85
+ end
@@ -60,6 +60,13 @@ describe Bluebird::Tweet do
60
60
  end
61
61
 
62
62
  describe '#extract_partials' do
63
+
64
+ context 'When status is empty' do
65
+ it 'does not add any partial' do
66
+ tweet = Bluebird::Tweet.new('')
67
+ expect(tweet.partials.length).to eq 0
68
+ end
69
+ end
63
70
  it 'breaks tweet into partials' do
64
71
  tweet = Bluebird::Tweet.new('Lorem ipsum')
65
72
  expect(tweet.partials.length).to eq 1
@@ -206,4 +213,50 @@ describe Bluebird::Tweet do
206
213
  expect(partial.mention?).to be_true
207
214
  end
208
215
  end
216
+
217
+ describe '#add_partial' do
218
+
219
+ it 'sets the partial type' do
220
+ tweet = Bluebird::Tweet.new('Lorem ipsum dolor')
221
+ tweet.add_partial(' sit amet', :sit_amet)
222
+ expect(tweet.partials.last.partial_type).to be :sit_amet
223
+ end
224
+
225
+ context 'When there is no partials' do
226
+ it 'adds a new partial to the end of the tweet' do
227
+ tweet = Bluebird::Tweet.new('')
228
+ tweet.add_partial('Lorem ipsum', :text)
229
+ expect(tweet.partials.length).to be 1
230
+ partial = tweet.partials.first
231
+ expect(partial.prev_partial).to be nil
232
+ expect(partial.next_partial).to be nil
233
+ end
234
+ end
235
+
236
+ context 'When there is already one partial' do
237
+ it 'adds a new partial to the end of the tweet' do
238
+ tweet = Bluebird::Tweet.new('Lorem ipsum dolor')
239
+ expect(tweet.partials.length).to eq 1
240
+ tweet.add_partial(' sit amet', :text)
241
+ expect(tweet.partials.length).to eq 2
242
+ expect(tweet.partials.last.content).to eq ' sit amet'
243
+ end
244
+ it 'sets the next_partial of the first partial' do
245
+ tweet = Bluebird::Tweet.new('Lorem ipsum dolor')
246
+ first_partial = tweet.partials.first
247
+ tweet.add_partial(' sit amet', :sit_amet)
248
+ last_partial = tweet.partials.last
249
+ expect(first_partial.prev_partial).to be nil
250
+ expect(first_partial.next_partial).to be last_partial
251
+ end
252
+ it 'sets the prev_partial of the new partial' do
253
+ tweet = Bluebird::Tweet.new('Lorem ipsum dolor')
254
+ first_partial = tweet.partials.first
255
+ tweet.add_partial(' sit amet', :sit_amet)
256
+ last_partial = tweet.partials.last
257
+ expect(last_partial.prev_partial).to be first_partial
258
+ expect(last_partial.next_partial).to be nil
259
+ end
260
+ end
261
+ end
209
262
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bluebird
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - İ. Emre Kutlu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-04 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: twitter-text
@@ -74,6 +74,7 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - .gitignore
77
+ - CHANGELOG.md
77
78
  - Gemfile
78
79
  - LICENSE.txt
79
80
  - README.md
@@ -90,12 +91,16 @@ files:
90
91
  - lib/bluebird/strategies/truncate_text.rb
91
92
  - lib/bluebird/strategies/truncate_text/config.rb
92
93
  - lib/bluebird/strategies/truncate_text/strategy.rb
94
+ - lib/bluebird/strategies/via.rb
95
+ - lib/bluebird/strategies/via/config.rb
96
+ - lib/bluebird/strategies/via/strategy.rb
93
97
  - lib/bluebird/tweet.rb
94
98
  - lib/bluebird/version.rb
95
99
  - spec/spec_helper.rb
96
100
  - spec/strategies/squeeze/strategy_spec.rb
97
101
  - spec/strategies/strip/strategy_spec.rb
98
102
  - spec/strategies/truncate_text/strategy_spec.rb
103
+ - spec/strategies/via/strategy_spec.rb
99
104
  - spec/tweet_spec.rb
100
105
  homepage: http://github.com/emrekutlu/bluebird
101
106
  licenses:
@@ -126,4 +131,5 @@ test_files:
126
131
  - spec/strategies/squeeze/strategy_spec.rb
127
132
  - spec/strategies/strip/strategy_spec.rb
128
133
  - spec/strategies/truncate_text/strategy_spec.rb
134
+ - spec/strategies/via/strategy_spec.rb
129
135
  - spec/tweet_spec.rb