chopper 0.0.1

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.
@@ -0,0 +1,3 @@
1
+ .rvmrc
2
+ *.gem
3
+ *.sw?
@@ -0,0 +1,60 @@
1
+ # Chopper
2
+
3
+ Chop your strings into pieces like a pro! Chopper will satisfy all of your string cleaving needs!
4
+
5
+ ## Install
6
+
7
+ $ gem install chopper
8
+
9
+ ## Chop Suey
10
+
11
+ Chopper helps you chop your strings into smaller pieces. You can:
12
+
13
+ * Chop a book into paragraphs
14
+ * Chop a 312 page manifesto on how the government secretely puts computer chips in your brain into tweets
15
+
16
+ The full list of methods added to string (see http://rubydoc.info/gems/chopper for all of the detailed documentation):
17
+
18
+ * String#paragraphs
19
+ * String#tweets
20
+
21
+ ## Tweets
22
+
23
+ You can break up a string into 140 character chunks using the `#tweets` method on `String` instances. It will break on the nearest word boundary
24
+ (instead of chopping the tweet in the middle of a word). It also allows you to choose the delimeter - it defaults to `...`
25
+
26
+ my_tweet =
27
+ "This tweet is over the 140 character limit, unfortunately. " +
28
+ "Now I'll have to break the tweet up into smaller tweets. " +
29
+ "Well, that's just great. Gosh Darnit! Arrrghhhhh!"
30
+
31
+ my_tweet.tweets
32
+ #==> [
33
+ "This tweet is over the 140 character limit, unfortunately. Now I'll have to break the tweet up into smaller tweets. Well, that's just ...",
34
+ "great. Gosh Darnit! Arrrghhhhh!"
35
+ ]
36
+
37
+ my_tweet.tweets :delimeter => "... (more)"
38
+ #==> [
39
+ "This tweet is over the 140 character limit, unfortunately. Now I'll have to break the tweet up into smaller tweets. Well, that's ... (more)",
40
+ "just great. Gosh Darnit! Arrrghhhhh!"
41
+ ]
42
+
43
+ my_tweet.each_tweet do |tweet|
44
+ p tweet
45
+ end
46
+
47
+ ### @reply tweets
48
+
49
+ Chopper will detect @reply tweets (tweets that start with a twitter handle), and will prepend the handle to all tweets:
50
+
51
+ string = "@moonmaster9000 this is a really long tweet. can you believe how long it is? i hope it's not too long, because if it goes over 140 characters, then I can't tweet it. Or can I?"
52
+ string.tweets
53
+ #==> [
54
+ "@moonmaster9000 this is a really long tweet. can you believe how long it is? i hope it's not too long, because if it goes over 140 ...",
55
+ "@moonmaster9000 characters, then I can't tweet it. Or can I?"
56
+ ]
57
+
58
+ ## Public Domain
59
+
60
+ This software is committed to the public domain. No license. No copyright. DO ANYTHING!
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "chopper"
3
+ s.version = File.read "VERSION"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Matt Parker"]
7
+ s.date = %q{2011-02-23}
8
+ s.description = %q{Chop up a string into paragraphs or tweets}
9
+ s.email = %q{moonmaster9000@gmail.com}
10
+ s.extra_rdoc_files = [
11
+ "README.markdown"
12
+ ]
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+
17
+ s.homepage = %q{http://github.com/moonmaster9000/chopper}
18
+ s.require_paths = ["lib"]
19
+ s.rubygems_version = %q{1.5.0}
20
+ s.summary = %q{Turn a single string into an array of strings. Supports paragraphs and tweets.}
21
+
22
+ s.add_development_dependency(%q<cucumber>, ["~> 0.10.0"])
23
+ s.add_development_dependency("rspec", ["~> 2.4.0"])
24
+ end
@@ -0,0 +1,12 @@
1
+ Feature: Breaking up a string into paragraphs
2
+ As a programmer
3
+ I want to be able to break a string into an array of paragraphs
4
+ So that I can do something with those paragraphs
5
+
6
+ Scenario: Breaking up a string into an array of paragraphs with #paragraphs
7
+ Given a string containing a single paragraph
8
+ When I call the #paragraphs method
9
+ Then I should get an array containing the string itself
10
+ Given a string containing multiple paragraphs
11
+ When I call the #paragraphs method
12
+ Then I should get an array containing all the paragraphs in the string
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift './lib'
2
+ require 'rspec'
3
+ require 'chopper'
@@ -0,0 +1,21 @@
1
+ Given /^a string containing a single paragraph$/ do
2
+ @string = "blah"
3
+ end
4
+
5
+ When /^I call the \#paragraphs method$/ do
6
+ @paragraphs = @string.paragraphs
7
+ end
8
+
9
+ Then /^I should get an array containing the string itself$/ do
10
+ @paragraphs.length.should == 1
11
+ @paragraphs.first.should == @string
12
+ end
13
+
14
+ Given /^a string containing multiple paragraphs$/ do
15
+ @string = "blah 1\nblah 2\n\nblah 3\n"
16
+ end
17
+
18
+ Then /^I should get an array containing all the paragraphs in the string$/ do
19
+ @paragraphs.length.should == 3
20
+ @paragraphs.should == ["blah 1", "blah 2", "blah 3"]
21
+ end
@@ -0,0 +1,44 @@
1
+ Given /^a string representing a tweet longer than 140 words$/ do
2
+ @tweet_string = "this is a really long tweet. can you believe how long it is? i hope it's not too long, because if it goes over 140 characters, then I can't tweet it. Or can I?"
3
+ end
4
+
5
+ When /^I call the \#tweets method on it$/ do
6
+ @tweets = @tweet_string.tweets
7
+ puts @tweets.inspect
8
+ end
9
+
10
+ Then /^I should get an array of strings breaking up the original string into 140 character chunks$/ do
11
+ @tweets.all? {|t| t.length <= 140}.should be_true
12
+ end
13
+
14
+ When /^I call the \#tweets method on it with a custom delimeter$/ do
15
+ @delimiter = "... cont'd"
16
+ @tweets = @tweet_string.tweets :delimiter => @delimiter
17
+ puts "@tweets with custom delimiter = #{@tweets.inspect}"
18
+ end
19
+
20
+ Then /^I should get an array of string breakinup up the original string into 140 character chunks using my custom delimeter$/ do
21
+ Then "I should get an array of strings breaking up the original string into 140 character chunks"
22
+ @tweets[0..-2].all? {|t| t.match @delimiter}.should be_true
23
+ @tweets[-1].match(@delimiter).should_not be_true
24
+ end
25
+
26
+ Given /^an @reply tweet string that's longer than 140 character$/ do
27
+ @tweet_string = "@moonmaster9000 this is a really long tweet. can you believe how long it is? i hope it's not too long, because if it goes over 140 characters, then I can't tweet it. Or can I?"
28
+ end
29
+
30
+ Then /^each of those chunks should begin with the @reply$/ do
31
+ @tweets.all? {|t| t.match /^@moonmaster9000\ .+$/}.should be_true
32
+ end
33
+
34
+ Given /^a string representing a tweet less than 140 characters$/ do
35
+ @tweet_string = "short tweet!"
36
+ end
37
+
38
+ Then /^I should get back the tweet encapsulated in an array$/ do
39
+ @tweets.should == [@tweet_string]
40
+ end
41
+
42
+ Given /^a string representing an @reply tweet less than 140 characters$/ do
43
+ @tweet_string = "@moonmaster9000 short reply!"
44
+ end
@@ -0,0 +1,30 @@
1
+ Feature: Breaking up a string into tweets
2
+ As a programmer
3
+ I want to break up a string into tweets
4
+ So that I can tweet the string to twitter
5
+
6
+ Scenario: Breaking up a string into tweets when that string is already less than 140 character
7
+ Given a string representing a tweet less than 140 characters
8
+ When I call the #tweets method on it
9
+ Then I should get back the tweet encapsulated in an array
10
+
11
+ Scenario: Breaking up a string into tweets
12
+ Given a string representing a tweet longer than 140 words
13
+ When I call the #tweets method on it
14
+ Then I should get an array of strings breaking up the original string into 140 character chunks
15
+
16
+ Scenario: Specifying the delimiter to use when a string is broken up into tweet chunks
17
+ Given a string representing a tweet longer than 140 words
18
+ When I call the #tweets method on it with a custom delimeter
19
+ Then I should get an array of string breakinup up the original string into 140 character chunks using my custom delimeter
20
+
21
+ Scenario: Breaking up an @reply into tweets when that @reply is already less than 140 character
22
+ Given a string representing an @reply tweet less than 140 characters
23
+ When I call the #tweets method on it
24
+ Then I should get back the tweet encapsulated in an array
25
+
26
+ Scenario: Recognizing an @reply tweet, and putting the @reply on all chunks
27
+ Given an @reply tweet string that's longer than 140 character
28
+ When I call the #tweets method on it
29
+ Then I should get an array of strings breaking up the original string into 140 character chunks
30
+ And each of those chunks should begin with the @reply
@@ -0,0 +1,2 @@
1
+ require 'chopper/paragraphs'
2
+ require 'chopper/tweets'
@@ -0,0 +1,5 @@
1
+ class String
2
+ def paragraphs
3
+ split /\n+/
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ class String
2
+ def tweets options={}
3
+ return [self] if self.length <= (options[:max_length] || 140)
4
+ delimiter = options[:delimiter] || "..."
5
+ prepend = self.match(/^(@[^\s]+\ ).+$/)
6
+ prepend = prepend[1] if prepend
7
+ max_length = (options[:max_length] || 140) - delimiter.length
8
+ max_length -= prepend.length if prepend
9
+ string = self.dup
10
+ string = string[prepend.length..-1] if prepend
11
+ ts = []
12
+
13
+ until string.length <= max_length
14
+ i = max_length - 1
15
+ character = string[i..i]
16
+ while !character.match(/^\s$/) and i > 0
17
+ i -= 1
18
+ character = string[i..i]
19
+ end
20
+
21
+ i = i == 0 ? (max_length - 1) : i
22
+ t = string[0..i] + delimiter
23
+ t = prepend + t if prepend
24
+ ts << t
25
+ string = string[(i+1)..-1]
26
+ end
27
+
28
+ unless string.empty?
29
+ string = prepend + string if prepend
30
+ ts << string
31
+ end
32
+
33
+ ts
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chopper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Matt Parker
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-23 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: cucumber
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 55
30
+ segments:
31
+ - 0
32
+ - 10
33
+ - 0
34
+ version: 0.10.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 31
46
+ segments:
47
+ - 2
48
+ - 4
49
+ - 0
50
+ version: 2.4.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: Chop up a string into paragraphs or tweets
54
+ email: moonmaster9000@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README.markdown
61
+ files:
62
+ - .gitignore
63
+ - README.markdown
64
+ - VERSION
65
+ - chopper.gemspec
66
+ - features/paragraphs.feature
67
+ - features/setup/env.rb
68
+ - features/step_definitions/paragraph_steps.rb
69
+ - features/step_definitions/tweet_steps.rb
70
+ - features/tweets.feature
71
+ - lib/chopper.rb
72
+ - lib/chopper/paragraphs.rb
73
+ - lib/chopper/tweets.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/moonmaster9000/chopper
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.6.2
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Turn a single string into an array of strings. Supports paragraphs and tweets.
108
+ test_files:
109
+ - features/paragraphs.feature
110
+ - features/setup/env.rb
111
+ - features/step_definitions/paragraph_steps.rb
112
+ - features/step_definitions/tweet_steps.rb
113
+ - features/tweets.feature