twitter_poster 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
19
+ *~
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Victor
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # TwitterAutoPoster
2
+ With this gem you can post text files to twitter.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'twitter_auto_poster'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install twitter_auto_poster
17
+
18
+ ## Usage
19
+
20
+ ## An example of using this gem to work from the command line:
21
+ user=User.new(
22
+ "consumer_key",
23
+ "consumer_secret",
24
+ "access_token",
25
+ "access_token_secret",
26
+ "login", "password")
27
+ response="work"
28
+ while response!="-exit"
29
+ puts ASKING_FOR_COMMAND
30
+ command=gets.chomp
31
+ begin
32
+ response=Parser.parse_command(command, user)
33
+ rescue Exception => ex
34
+ puts ex.message
35
+ end
36
+ end
37
+ ## An example using this gem to work without command line:
38
+ user=User.new(
39
+ "consumer_key",
40
+ "consumer_secret",
41
+ "access_token",
42
+ "access_token_secret",
43
+ "login", "password")
44
+ user.send_message(text) # will send single message to your twitter account
45
+ user.send_message_from_file(file_path) # Read a file, split the data into words,
46
+ # will man the words in the message
47
+ # and send to Twitter with a specified interval (you can set interval so: user.delay=interval_value)
48
+ # Also you can set max length of messages in this way: user.message_length=max_message_length_value
49
+
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/parser.rb ADDED
@@ -0,0 +1,65 @@
1
+ require "./lib/phrases.rb"
2
+
3
+ module TwitterAutoPoster
4
+ class Parser
5
+ attr_accessor :words
6
+
7
+ def initialize
8
+ @words=Array.new
9
+ end
10
+
11
+ def parse_file(str)
12
+ if validate_file(str)
13
+ text = File.open(str, 'r') { |file| file.read }
14
+ @words=words_from_text(text)
15
+ else
16
+ raise Exception.new(FILE_IS_NOT_VALID)
17
+ end
18
+
19
+ end
20
+
21
+ def self.parse_command(command, user)
22
+ case command
23
+ when "-help"
24
+ puts HELP
25
+ when "-sm"
26
+ puts TwitterAutoPoster::Phrases::ASKING_FOR_MESSAGE
27
+ message=gets.chomp
28
+ user.send_message(message)
29
+ when "-smff"
30
+ puts TwitterAutoPoster::Phrases::ASKING_FOR_FILE_PATH
31
+ path=gets.chomp
32
+ user.send_messages_from_file(path)
33
+ when "-set ml"
34
+ puts TwitterAutoPoster::Phrases::ASKING_FOR_MESSAGE_LENGTH
35
+ length=gets.chomp
36
+ user.message_length=length
37
+ when "-set sep"
38
+ puts TwitterAutoPoster::Phrases::ASKING_FOR_SEPARATOR
39
+ separator=gets.chomp
40
+ user.separator=separator
41
+
42
+ when "-exit"
43
+ return "-exit"
44
+ else
45
+ raise Exception.new(UNKNOWN_COMMAND+" \""+command+"\"")
46
+ end
47
+ SUCCESS
48
+ end
49
+
50
+ private
51
+ def validate_file(str)
52
+ if (File.exist?(str))
53
+ !File.zero?(str)
54
+ else
55
+ false
56
+ end
57
+ end
58
+
59
+ def words_from_text(text)
60
+ text.downcase.scan(/[\w']+/)
61
+ end
62
+ end
63
+ end
64
+
65
+
data/lib/phrases.rb ADDED
@@ -0,0 +1,18 @@
1
+ module TwitterAutoPoster
2
+ module Phrases
3
+ ASKING_FOR_COMMAND="Please, enter command (for help you can use command \"-help\")\n"
4
+ ASKING_FOR_MESSAGE_LENGTH="Please, enter length of message\n"
5
+ ASKING_FOR_SEPARATOR="Please, enter separator\n"
6
+ ASKING_FOR_MESSAGE="Enter message, please:\n"
7
+ ASKING_FOR_FILE_PATH="Enter path to file, please\n"
8
+ HELP="-sm ---- use for sending a single message\n
9
+ -smff ---- use for sending messages from file\n
10
+ -set ml ---- use for setting max length of message\n
11
+ -set sep ---- use for setting separator, which will be used between sentences\n
12
+ -exit ---- exit\n"
13
+ UNKNOWN_COMMAND="Unknown command:"
14
+ SUCCESS="SUCCESS"
15
+ FAIL="FAIL"
16
+ FILE_IS_NOT_VALID="File don't exist or empty."
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ require "./lib/twitter_auto_poster/version"
2
+ require "./lib/user.rb"
3
+ require "./lib/parser.rb"
4
+ require "./lib/phrases.rb"
5
+
6
+ =begin
7
+ *******************************************************************
8
+ Example of usage:
9
+ *******************************************************************
10
+ user=User.new("consumer_key",
11
+ "consumer_secret",
12
+ "access_token",
13
+ "access_token_secret",
14
+ "login", "password")
15
+ response="work"
16
+ while response!="-exit"
17
+ puts ASKING_FOR_COMMAND
18
+ command=gets.chomp
19
+ begin
20
+ response=Parser.parse_command(command, user)
21
+ rescue Exception => ex
22
+ puts ex.message
23
+ end
24
+ end
25
+ *******************************************************************
26
+ =end
27
+
28
+
29
+
30
+
@@ -0,0 +1,3 @@
1
+ module TwitterAutoPoster
2
+ VERSION = "0.0.1"
3
+ end
data/lib/user.rb ADDED
@@ -0,0 +1,97 @@
1
+ require 'oauth'
2
+ require 'uri'
3
+ require "./lib/parser.rb"
4
+ module TwitterAutoPoster
5
+ class User
6
+ attr_accessor :messages, :consumer_key, :consumer_secret, :access_token, :access_token_secret,
7
+ :login, :password, :message_length, :separator, :delay
8
+
9
+ def initialize(consumer_key, consumer_secret, access_token, access_token_secret,
10
+ login, password, message_length=100, separator=" ", delay=5)
11
+
12
+ @messages=Array.new
13
+ @consumer_key=consumer_key
14
+ @consumer_secret=consumer_secret
15
+ @access_token=access_token
16
+ @access_token_secret=access_token_secret
17
+ @login=login
18
+ @password=password
19
+ @message_length=message_length
20
+ @separator=separator
21
+ @delay=delay
22
+ end
23
+
24
+
25
+ def prepare_messages(array)
26
+ sep_size=@separator.to_s.length
27
+ if array.nil?
28
+
29
+ else
30
+ current_message_length=0
31
+ current_message=""
32
+ i=0
33
+ while i<array.size
34
+ if current_message_length + array[i].to_s.length<=@message_length
35
+ current_message+=array[i].to_s
36
+ current_message_length+=array[i].to_s.length
37
+ if current_message_length+sep_size<@message_length-1 and i!=array.size-1 and
38
+ current_message_length+sep_size+array[i+1].to_s.length<=@message_length
39
+ current_message+=@separator
40
+ current_message_length+=sep_size
41
+ else
42
+ @messages<<current_message
43
+ current_message=""
44
+ current_message_length=0
45
+ end
46
+ i+=1
47
+ if i==array.size && current_message!=""
48
+ @messages<<current_message
49
+ end
50
+
51
+ else
52
+ if array[i].to_s.length>@message_length
53
+ raise Exception.new("message length too small!!")
54
+
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def prepare_access_token(oauth_token, oauth_token_secret)
62
+ consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret,
63
+ {:site => "http://api.twitter.com",
64
+ :scheme => :header
65
+ })
66
+
67
+ token_hash = {:oauth_token => @access_token,
68
+ :oauth_token_secret => @access_token_secret
69
+ }
70
+ access_token = OAuth::AccessToken.from_hash(consumer, token_hash)
71
+ end
72
+
73
+ def send_message(text)
74
+ access_token = prepare_access_token(@login, @password)
75
+ response = access_token.request(:post, "http://api.twitter.com/1/statuses/update.json?status="+URI.escape(text))
76
+ response.code
77
+ end
78
+
79
+
80
+ def send_messages_from_file(file)
81
+ parser=Parser.new
82
+ parser.parse_file(file)
83
+ prepare_messages(parser.words)
84
+ q=@messages.length
85
+ thread= Thread.start do
86
+ @messages.each do |m|
87
+ send_message(m.to_s)
88
+ q-=1
89
+ puts "[#{file}]: Message \""+m.to_s+"\" was sent. #{q} messages left.\n"
90
+ sleep @delay
91
+ end
92
+ puts "[#{file}]: All messages were sent.\n\n"
93
+ thread.exit!
94
+ end
95
+ end
96
+ end
97
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ I like you ruby!
2
+ We need more time
3
+ Use brain
4
+ Everything will be ok
@@ -0,0 +1,81 @@
1
+ require "rspec"
2
+ include Twitter_auto_poster::Phrases
3
+ describe "parse command" do
4
+ let(:parser) { Parser.new }
5
+ let(:user) {
6
+ us=User.new("consumer_key",
7
+ "consumer_secret",
8
+ "access_token",
9
+ "access_token_secret",
10
+ "login", "password") }
11
+ it "should rise exception with invalid command" do
12
+ expect { parser.parse_command("\\\\", user) }.to raise_error
13
+ end
14
+ it "shouldn't rise exception with valid command" do
15
+ Parser.parse_command("-exit", user)
16
+ end
17
+
18
+ it "should return #{SUCCESS} with command \"-set ml\" and correctly set max length of message" do
19
+ Parser.stub(:gets).and_return "50"
20
+ status=Parser.parse_command("-set ml", user)
21
+ user.message_length.should be_eql "50"
22
+ status.should be_eql SUCCESS
23
+ end
24
+
25
+ it "should return #{SUCCESS} with command \"-set sep\" and correctly change separator" do
26
+ Parser.stub(:gets).and_return "|"
27
+ status=Parser.parse_command("-set sep", user)
28
+ user.separator.should be_eql "|"
29
+ status.should be_eql SUCCESS
30
+ end
31
+
32
+ it "should return #{SUCCESS} with command \"-sm\" " do
33
+ Parser.stub(:gets).and_return "test message from ruby"
34
+ User.stub(:send_message).and_return "200"
35
+ status=Parser.parse_command("-sm", user)
36
+ status.should be_eql SUCCESS
37
+ end
38
+
39
+ it "should return #{SUCCESS} with command \"-smff\" " do
40
+ Parser.stub(:gets).and_return "test message from ruby"
41
+ user.stub(:send_messages_from_file)
42
+ status=Parser.parse_command("-smff", user)
43
+ status.should be_eql SUCCESS
44
+ end
45
+
46
+ describe "is file validate?" do
47
+ let(:parser) { Parser.new }
48
+
49
+ it "should be true with valid file" do
50
+ parser.send(:validate_file, './spec/files/text2.txt').should be_true
51
+ end
52
+ it "should be false with no valid file" do
53
+ parser.send(:validate_file, 'sd').should be_false
54
+ end
55
+ it "should be false with empty file" do
56
+ parser.send(:validate_file, './spec/files/text.txt').should be_false
57
+ end
58
+
59
+ end
60
+ describe "parse file" do
61
+ let(:parser) { Parser.new }
62
+ it " must have result, which shouldn't be empty" do
63
+ parser.parse_file("./spec/files/text2.txt")
64
+ parser.words.should_not be_nil
65
+ end
66
+ it "must have result, which should be array" do
67
+ parser.words.is_a?(Array).should be_true
68
+ end
69
+ it "must have result, elements of which should be String" do
70
+ parser.words.each { |el|
71
+ el.is_a?(String).should be_true
72
+ }
73
+ end
74
+ it "must have result, elements of which shouldn't has length more than 140 chars" do
75
+ parser.words.each { |el|
76
+ el.to_s.length.should be<=140
77
+ }
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'twitter_auto_poster'
4
+ require "./lib/user.rb"
5
+ require "./lib/parser.rb"
6
+ include Twitter_auto_poster
7
+
@@ -0,0 +1,3 @@
1
+ require "spec_helper"
2
+
3
+
data/spec/user_spec.rb ADDED
@@ -0,0 +1,40 @@
1
+ require "rspec"
2
+
3
+ describe "post message" do
4
+ it "should return code" do
5
+
6
+ end
7
+ end
8
+
9
+ describe "prepare messages from array" do
10
+ let(:user) {
11
+ us=User.new("consumer_key",
12
+ "consumer_secret",
13
+ "access_token",
14
+ "access_token_secret",
15
+ "login", "password")
16
+ us.message_length=9
17
+ us
18
+ }
19
+ before (:each) do
20
+ @arr='first', 'some_', 'second', 'something', '123', 'pass'
21
+ @arr2='123', 'something long', 'word'
22
+ end
23
+
24
+
25
+ it "should return length of message, which less or equal to property \"message length\"" do
26
+ user.prepare_messages(@arr)
27
+ user.messages.each { @this.to_s.length.should be <=user.message_length }
28
+ end
29
+
30
+ it "should rise exception if length of array's element bigger property \"message length\"" do
31
+ expect { user.prepare_messages(@arr2) }.to raise_error
32
+ end
33
+ it "should return \"correct\" messages" do
34
+ user.prepare_messages(@arr)
35
+ user.messages[0].should eq('first')
36
+ user.messages[1].should eq('some_')
37
+ user.messages[2].should eq('second')
38
+ user.messages[4].should eq('123'+user.separator+'pass')
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/twitter_auto_poster/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Victor"]
6
+ gem.email = ["v.dolgishev@gmail.com"]
7
+ gem.description = %q{Gem for auto tweeting}
8
+ gem.summary = %q{Gem for auto tweeting with possibility to tweet from different files simultaneously }
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "twitter_poster"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = TwitterAutoPoster::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "oauth"
20
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_poster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Victor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: oauth
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Gem for auto tweeting
47
+ email:
48
+ - v.dolgishev@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/parser.rb
59
+ - lib/phrases.rb
60
+ - lib/twitter_auto_poster.rb
61
+ - lib/twitter_auto_poster/version.rb
62
+ - lib/user.rb
63
+ - spec/files/text.txt
64
+ - spec/files/text2.txt
65
+ - spec/parser_spec.rb
66
+ - spec/spec_helper.rb
67
+ - spec/twitter_auto_poster_spec.rb
68
+ - spec/user_spec.rb
69
+ - twitter_auto_poster.gemspec
70
+ homepage: ''
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.24
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Gem for auto tweeting with possibility to tweet from different files simultaneously
94
+ test_files:
95
+ - spec/files/text.txt
96
+ - spec/files/text2.txt
97
+ - spec/parser_spec.rb
98
+ - spec/spec_helper.rb
99
+ - spec/twitter_auto_poster_spec.rb
100
+ - spec/user_spec.rb