bell 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.
- data/.gitignore +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +47 -0
- data/MIT-LICENSE +20 -0
- data/README.org +128 -0
- data/Rakefile +16 -0
- data/bell.gemspec +30 -0
- data/bin/bell +5 -0
- data/features/create_user.feature +16 -0
- data/features/full_report.feature +84 -0
- data/features/import_public_contacts.feature +20 -0
- data/features/import_user_contacts.feature +43 -0
- data/features/list_all_contacts.feature +19 -0
- data/features/list_user_contacts.feature +27 -0
- data/features/list_users.feature +22 -0
- data/features/remove_user.feature +21 -0
- data/features/rename_user.feature +25 -0
- data/features/step_definitions/cli_steps.rb +59 -0
- data/features/step_definitions/contact_steps.rb +104 -0
- data/features/step_definitions/database_steps.rb +3 -0
- data/features/step_definitions/report_steps.rb +17 -0
- data/features/step_definitions/user_steps.rb +71 -0
- data/features/support/env.rb +65 -0
- data/features/user_report.feature +67 -0
- data/lib/bell/cli.rb +12 -0
- data/lib/bell/commands/command.rb +58 -0
- data/lib/bell/commands/contact_command.rb +91 -0
- data/lib/bell/commands/implosion_command.rb +14 -0
- data/lib/bell/commands/report_command.rb +69 -0
- data/lib/bell/commands/user_command.rb +74 -0
- data/lib/bell/commands.rb +5 -0
- data/lib/bell/csv_parser.rb +52 -0
- data/lib/bell/dispatcher.rb +13 -0
- data/lib/bell/displayable.rb +13 -0
- data/lib/bell/full_report.rb +145 -0
- data/lib/bell/handlers/contacts_handler.rb +79 -0
- data/lib/bell/handlers/implosions_handler.rb +9 -0
- data/lib/bell/handlers/reports_handler.rb +41 -0
- data/lib/bell/handlers/users_handler.rb +50 -0
- data/lib/bell/handlers.rb +4 -0
- data/lib/bell/message.rb +115 -0
- data/lib/bell/public_contact.rb +17 -0
- data/lib/bell/user.rb +16 -0
- data/lib/bell/user_contact.rb +18 -0
- data/lib/bell/user_report.rb +88 -0
- data/lib/bell/util.rb +31 -0
- data/lib/bell.rb +157 -0
- data/spec/bell/cli_spec.rb +4 -0
- data/spec/bell/commands/command_spec.rb +59 -0
- data/spec/bell/commands/contact_command_spec.rb +112 -0
- data/spec/bell/commands/implosion_command_spec.rb +31 -0
- data/spec/bell/commands/report_command_spec.rb +71 -0
- data/spec/bell/commands/user_command_spec.rb +128 -0
- data/spec/bell/csv_parser_spec.rb +167 -0
- data/spec/bell/dispatcher_spec.rb +4 -0
- data/spec/bell/full_report_spec.rb +4 -0
- data/spec/bell/handlers/contacts_handler_spec.rb +160 -0
- data/spec/bell/handlers/implosions_handler_spec.rb +11 -0
- data/spec/bell/handlers/reports_handler_spec.rb +183 -0
- data/spec/bell/handlers/users_handler_spec.rb +94 -0
- data/spec/bell/public_contact_spec.rb +4 -0
- data/spec/bell/user_contact_spec.rb +4 -0
- data/spec/bell/user_report_spec.rb +4 -0
- data/spec/bell/user_spec.rb +4 -0
- data/spec/spec_helper.rb +21 -0
- metadata +230 -0
@@ -0,0 +1,104 @@
|
|
1
|
+
Given /^a contact with name "([^"]*)" and number "([^"]*)" exists$/ do |contact_name, contact_number|
|
2
|
+
Bell::UserContact.create(:name => contact_name, :number => contact_number)
|
3
|
+
end
|
4
|
+
|
5
|
+
Given /^a contact with name "([^"]*)" exists$/ do |contact_name|
|
6
|
+
Given %{"#{random_name}" has a contact with name "#{contact_name}" and number "#{random_number}" in his contact list}
|
7
|
+
end
|
8
|
+
|
9
|
+
Given /^"([^"]*)" has a contact with name "([^"]*)" and number "([^"]*)" in his contact list$/ do |user_name, contact_name, contact_number|
|
10
|
+
user = Bell::User.find_or_create(:name => user_name)
|
11
|
+
user.add_contact(:name => contact_name, :number => contact_number)
|
12
|
+
end
|
13
|
+
|
14
|
+
Given /^"([^"]*)" has a contact with name "([^"]*)" in his contact list$/ do |user_name, contact_name|
|
15
|
+
Given %{"#{user_name}" has a contact with name "#{contact_name}" and number "#{random_number}" in his contact list}
|
16
|
+
end
|
17
|
+
|
18
|
+
Given /^"([^"]*)" has a contact with number "([^"]*)" in his contact list$/ do |user_name, contact_number|
|
19
|
+
Given %{"#{user_name}" has a contact with name "#{random_name}" and number "#{contact_number}" in his contact list}
|
20
|
+
end
|
21
|
+
|
22
|
+
Given /^"([^"]*)" doesn't have a contact with name "([^"]*)" in his contacts$/ do |user_name, contact_name|
|
23
|
+
user = Bell::User.find(:name => user_name)
|
24
|
+
Bell::UserContact.filter(:name => contact_name, :user_id => user.id).delete
|
25
|
+
end
|
26
|
+
|
27
|
+
Given /^a public contact with number "([^"]*)" and name "([^"]*)"$/ do |contact_number, contact_name|
|
28
|
+
Bell::PublicContact.create(:name => contact_name, :number => contact_number)
|
29
|
+
end
|
30
|
+
|
31
|
+
Given /^no contact with name "([^"]*)" exists$/ do |contact_name|
|
32
|
+
Bell::UserContact.filter(:name => contact_name).delete
|
33
|
+
end
|
34
|
+
|
35
|
+
Given /^no created contacts$/ do
|
36
|
+
Bell::UserContact.delete
|
37
|
+
end
|
38
|
+
|
39
|
+
When /^I list all contacts$/ do
|
40
|
+
Bell::Handlers::ContactsHandler.list
|
41
|
+
end
|
42
|
+
|
43
|
+
When /^I list the contacts for the user with name "([^"]*)"$/ do |user_name|
|
44
|
+
params = { :user => { :name => user_name }, :csv => @csv }
|
45
|
+
Bell::Handlers::ContactsHandler.list(params)
|
46
|
+
end
|
47
|
+
|
48
|
+
When /^I list the contacts for the user with name "([^"]*)" in CSV format$/ do |user_name|
|
49
|
+
@csv = true
|
50
|
+
When %{I list the contacts for the user with name "#{user_name}"}
|
51
|
+
end
|
52
|
+
|
53
|
+
When /^I request a contact import using "([^"]*)"$/ do |path|
|
54
|
+
Bell::Handlers::ContactsHandler.import(@params || { :path => File.join(TMP_PATH, path) })
|
55
|
+
end
|
56
|
+
|
57
|
+
When /^I request a contact import for "([^"]*)" using "([^"]*)"$/ do |user_name, path|
|
58
|
+
@params = { :path => File.join(TMP_PATH, path), :user => { :name => user_name } }
|
59
|
+
When %{I request a contact import using "#{path}"}
|
60
|
+
end
|
61
|
+
|
62
|
+
When /^I request a public contact import using "([^"]*)"$/ do |path|
|
63
|
+
@params = { :path => File.join(TMP_PATH, path), :public => true }
|
64
|
+
When %{I request a contact import using "#{path}"}
|
65
|
+
end
|
66
|
+
|
67
|
+
Then /^bell should tell me that "([^"]*)" already has "([^"]*)" in his contact list$/ do |user_name, contact_name|
|
68
|
+
user = Bell::User.find(:name => user_name)
|
69
|
+
contact = Bell::UserContact.find(:name => contact_name, :user_id => user.id)
|
70
|
+
Bell.output.string.chomp.should == Bell::Message.contact_name_taken(contact_name)
|
71
|
+
end
|
72
|
+
|
73
|
+
Then /^"([^"]*)" should have "([^"]*)" in his contact list$/ do |user_name, contact_name|
|
74
|
+
user = Bell::User.find(:name => user_name)
|
75
|
+
Bell::UserContact.find(:name => contact_name, :user_id => user.id).should_not be_nil
|
76
|
+
end
|
77
|
+
|
78
|
+
Then /^"([^"]*)" should be in the public contact list$/ do |contact_name|
|
79
|
+
Bell::PublicContact.find(:name => contact_name).should_not be_nil
|
80
|
+
end
|
81
|
+
|
82
|
+
Then /^bell should tell me that the number "([^"]*)" was already taken$/ do |contact_number|
|
83
|
+
Bell.output.string.chomp.should == Bell::Message.contact_number_taken(contact_number)
|
84
|
+
end
|
85
|
+
|
86
|
+
Then /^bell should tell me that the number "([^"]*)" has a bad format$/ do |contact_number|
|
87
|
+
Bell.output.string.chomp.should == Bell::Message.contact_number_bad_format(contact_number)
|
88
|
+
end
|
89
|
+
|
90
|
+
Then /^bell should tell me that the contact list of the user with name "([^"]*)" is empty$/ do |user_name|
|
91
|
+
Bell.output.string.chomp.should == Bell::Message.contact_list_empty(user_name)
|
92
|
+
end
|
93
|
+
|
94
|
+
Then /^I should not have a contact with name "([^"]*)" in the database$/ do |contact_name|
|
95
|
+
Bell::UserContact.find(:name => contact_name).should be_nil
|
96
|
+
end
|
97
|
+
|
98
|
+
Then /^bell should tell me that the contact "([^"]*)" was removed$/ do |contact_name|
|
99
|
+
Bell.output.string.chomp.should == Bell::Message.contact_removed(contact_name)
|
100
|
+
end
|
101
|
+
|
102
|
+
Then /^bell should tell me that there are no created contacts$/ do
|
103
|
+
Bell.output.string.chomp.should == Bell::Message.no_contacts_created
|
104
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
When /^I request a full report using "([^"]*)"$/ do |path|
|
2
|
+
path = File.join(TMP_PATH, path)
|
3
|
+
params = { :path => path }
|
4
|
+
Bell::Handlers::ReportsHandler.full_report(params)
|
5
|
+
end
|
6
|
+
|
7
|
+
When /^I request a user report using "([^"]*)"$/ do |path|
|
8
|
+
path = File.join(TMP_PATH, path)
|
9
|
+
params = { :path => path }
|
10
|
+
Bell::Handlers::ReportsHandler.user_report(params)
|
11
|
+
end
|
12
|
+
|
13
|
+
When /^I request a user report for "([^"]*)" using "([^"]*)"$/ do |user_name, path|
|
14
|
+
path = File.join(TMP_PATH, path)
|
15
|
+
params = { :path => path, :user => { :name => user_name } }
|
16
|
+
Bell::Handlers::ReportsHandler.user_report(params)
|
17
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
Given /^no user exists$/ do
|
2
|
+
Bell::User.delete
|
3
|
+
end
|
4
|
+
|
5
|
+
Given /^no user with name "([^"]*)" exists$/ do |user_name|
|
6
|
+
Bell::User.filter(:name => user_name).delete
|
7
|
+
end
|
8
|
+
|
9
|
+
Given /^a user with name "([^"]*)" exists$/ do |user_name|
|
10
|
+
Bell::User.create(:name => user_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
Given /^"([^"]*)" has an empty contact list$/ do |user_name|
|
14
|
+
Bell::User.find(:name => user_name).remove_all_contacts
|
15
|
+
end
|
16
|
+
|
17
|
+
When /^I create a user with name "([^"]*)"$/ do |user_name|
|
18
|
+
params = { :user => { :name => user_name } }
|
19
|
+
Bell::Handlers::UsersHandler.create(params)
|
20
|
+
end
|
21
|
+
|
22
|
+
When /^I list all users$/ do
|
23
|
+
Bell::Handlers::UsersHandler.list
|
24
|
+
end
|
25
|
+
|
26
|
+
When /^I remove the user with name "([^"]*)"$/ do |user_name|
|
27
|
+
params = { :user => { :name => user_name } }
|
28
|
+
Bell::Handlers::UsersHandler.remove(params)
|
29
|
+
end
|
30
|
+
|
31
|
+
When /^I rename "([^"]*)" to "([^"]*)"$/ do |source_name, target_name|
|
32
|
+
params = { :user => { :source_name => source_name, :target_name => target_name } }
|
33
|
+
Bell::Handlers::UsersHandler.rename(params)
|
34
|
+
end
|
35
|
+
|
36
|
+
Then /^bell should tell me that there are no created users$/ do
|
37
|
+
Bell.output.string.chomp.should == Bell::Message.no_created_users
|
38
|
+
end
|
39
|
+
|
40
|
+
Then /^bell should tell me that a user with name "([^"]*)" was created$/ do |user_name|
|
41
|
+
Bell.output.string.chomp.should == Bell::Message.user_created(user_name)
|
42
|
+
end
|
43
|
+
|
44
|
+
Then /^bell should tell me that the user "([^"]*)" was removed$/ do |user_name|
|
45
|
+
Bell.output.string.chomp.should == Bell::Message.user_removed(user_name)
|
46
|
+
end
|
47
|
+
|
48
|
+
Then /^bell should tell me that the user "([^"]*)" already exists$/ do |user_name|
|
49
|
+
Bell.output.string.chomp.should == Bell::Message.user_already_exists(user_name)
|
50
|
+
end
|
51
|
+
|
52
|
+
Then /^bell should tell me that there is no user with name "([^"]*)"$/ do |user_name|
|
53
|
+
Bell.output.string.chomp.should == Bell::Message.user_does_not_exist(user_name)
|
54
|
+
end
|
55
|
+
|
56
|
+
Then /^I should have the user "([^"]*)" in the database$/ do |user_name|
|
57
|
+
Bell::User.find(:name => user_name).should_not be_nil
|
58
|
+
end
|
59
|
+
|
60
|
+
Then /^I should not have the user "([^"]*)" in the database$/ do |user_name|
|
61
|
+
Bell::User.find(:name => user_name).should be_nil
|
62
|
+
end
|
63
|
+
|
64
|
+
Then /^bell should tell me that the user "([^"]*)" was renamed to "([^"]*)"$/ do |source_name, target_name|
|
65
|
+
params = { :source_name => source_name, :target_name => target_name }
|
66
|
+
Bell.output.string.chomp.should == Bell::Message.user_renamed(params)
|
67
|
+
end
|
68
|
+
|
69
|
+
Then /^bell should tell me that "([^"]*)" has an empty contact list$/ do |user_name|
|
70
|
+
Bell.output.string.chomp.should == Bell::Message.contact_list_empty(user_name)
|
71
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
|
2
|
+
require 'bell'
|
3
|
+
|
4
|
+
TMP_PATH = File.join(File.dirname(__FILE__), '..', '..', 'tmp')
|
5
|
+
|
6
|
+
After('@no-txn') do
|
7
|
+
Given 'a clean slate'
|
8
|
+
end
|
9
|
+
|
10
|
+
Before do
|
11
|
+
Bell.output.reopen
|
12
|
+
FileUtils.rm_rf(TMP_PATH)
|
13
|
+
FileUtils.mkdir_p(TMP_PATH)
|
14
|
+
end
|
15
|
+
|
16
|
+
at_exit do
|
17
|
+
FileUtils.rm_rf(TMP_PATH)
|
18
|
+
end
|
19
|
+
|
20
|
+
module Bell::StepDefinitionHelper
|
21
|
+
FIXTURES_PATH = File.join(File.dirname(__FILE__), '..', '..', 'spec', 'fixtures')
|
22
|
+
|
23
|
+
def inside_the_tmp_directory(&block)
|
24
|
+
FileUtils.chdir(TMP_PATH, &block)
|
25
|
+
end
|
26
|
+
|
27
|
+
def non_existing_file_path
|
28
|
+
"#{FIXTURES_PATH}/non_existing_file.csv"
|
29
|
+
end
|
30
|
+
|
31
|
+
def directory_path
|
32
|
+
"#{FIXTURES_PATH}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def non_phone_bill_file_path
|
36
|
+
"#{FIXTURES_PATH}/text_file.txt"
|
37
|
+
end
|
38
|
+
|
39
|
+
def malformed_csv_file_path
|
40
|
+
"#{FIXTURES_PATH}/malformed_csv_file.csv"
|
41
|
+
end
|
42
|
+
|
43
|
+
def valid_phone_bill_file_path
|
44
|
+
"#{FIXTURES_PATH}/valid_phone_bill_file.csv"
|
45
|
+
end
|
46
|
+
|
47
|
+
def invalid_contacts_file_path
|
48
|
+
"#{FIXTURES_PATH}/invalid_contacts_file.csv"
|
49
|
+
end
|
50
|
+
|
51
|
+
def valid_contacts_file_path
|
52
|
+
"#{FIXTURES_PATH}/valid_contacts_file.csv"
|
53
|
+
end
|
54
|
+
|
55
|
+
def random_name
|
56
|
+
(0...8).map{65.+(rand(25)).chr}.join
|
57
|
+
end
|
58
|
+
|
59
|
+
def random_number
|
60
|
+
charset = %w[0 1 2 3 4 5 6 7 9]
|
61
|
+
(0...10).map{ charset.to_a[rand(charset.size)] }.join
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
World(Bell::StepDefinitionHelper)
|
@@ -0,0 +1,67 @@
|
|
1
|
+
@no-txn
|
2
|
+
Feature: Shell user requests an user report
|
3
|
+
As a person using the shell
|
4
|
+
I want to show user reports
|
5
|
+
In order to see how much each user spent on phone calls
|
6
|
+
|
7
|
+
Scenario: Non-existing file
|
8
|
+
When I request a user report using "/non/existent/path"
|
9
|
+
Then bell should tell me that "/non/existent/path" does not exist
|
10
|
+
|
11
|
+
Scenario: Directory
|
12
|
+
Given a directory named "tmp"
|
13
|
+
When I request a user report using "tmp"
|
14
|
+
Then bell should tell me that "tmp" is a directory
|
15
|
+
|
16
|
+
Scenario: Non CSV file
|
17
|
+
Given a file named "non_csv_file.txt" with:
|
18
|
+
"""
|
19
|
+
Lorem ipsum dolor sit amet, consectetur adipisicing elit
|
20
|
+
"""
|
21
|
+
When I request a user report using "non_csv_file.txt"
|
22
|
+
Then bell should tell me that "non_csv_file.txt" is not a csv file
|
23
|
+
|
24
|
+
Scenario: Malformed CSV file
|
25
|
+
Given a file named "malformed_csv_file.csv" with:
|
26
|
+
"""
|
27
|
+
"1", 'first"
|
28
|
+
"""
|
29
|
+
When I request a full report using "malformed_csv_file.csv"
|
30
|
+
Then bell should tell me that "malformed_csv_file.csv" is a malformed csv file
|
31
|
+
|
32
|
+
Scenario: Phone bill file with invalid rows
|
33
|
+
Given a file named "invalid_rows.csv" with:
|
34
|
+
"""
|
35
|
+
Detalhes da fatura
|
36
|
+
|
37
|
+
"Seq ","Origem ","Descrição ","Periodo/Data ","Terminal_Destino ","Local Origem","Local Destino ","Hora Inicio ","Hora Fim ","Imp ","Pais ","Qtde ","Unid ","Valor (R$) "
|
38
|
+
1,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","11/08/10 A 99/99/99 ",1993692887,"SCL -SP ","CAS -SP ",02:56:29 AM," ","E "," ",
|
39
|
+
2,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","11/08/10 A 99/99/99 ",1993692887,"SCL -SP ","CAS -SP ",02:59:03 AM," ","E "," ",900,"MIN ",1.3
|
40
|
+
3,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES "
|
41
|
+
|
42
|
+
"""
|
43
|
+
When I request a user report using "invalid_rows.csv"
|
44
|
+
Then bell should tell me that "invalid_rows.csv" has errors on line 4, 6
|
45
|
+
|
46
|
+
Scenario: Valid phone bill file
|
47
|
+
Given a user with name "bob" exists
|
48
|
+
And "bob" has a contact with name "earl" and number "1993692887" in his contact list
|
49
|
+
And a file named "fatura.csv" with:
|
50
|
+
"""
|
51
|
+
Detalhes da fatura
|
52
|
+
|
53
|
+
"Seq ","Origem ","Descri��o ","Periodo/Data ","Terminal_Destino ","Local Origem","Local Destino ","Hora Inicio ","Hora Fim ","Imp ","Pais ","Qtde ","Unid ","Valor (R$) "
|
54
|
+
1,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","11/08/10 A 99/99/99 ",1993692887,"SCL -SP ","CAS -SP ",02:56:29 AM," ","E "," ",500,"MIN ",0.73
|
55
|
+
2,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","11/08/10 A 99/99/99 ",1993692887,"SCL -SP ","CAS -SP ",02:59:03 AM," ","E "," ",900,"MIN ",1.3
|
56
|
+
3,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","13/08/10 A 99/99/99 ",1992563321,"SCL -SP ","CAS -SP ",09:07:55 PM," ","E "," ",5800,"MIN ",8.47
|
57
|
+
|
58
|
+
"""
|
59
|
+
When I request a user report for "bob" using "fatura.csv"
|
60
|
+
Then the output should be:
|
61
|
+
"""
|
62
|
+
Data Contato Número Horário Custo
|
63
|
+
11/08/10 earl 1993692887 02:56:29 AM 0.73
|
64
|
+
11/08/10 earl 1993692887 02:59:03 AM 1.30
|
65
|
+
|
66
|
+
Total: R$ 2.03
|
67
|
+
"""
|
data/lib/bell/cli.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Bell::Commands
|
4
|
+
class Command
|
5
|
+
CSV_FLAGS = %w[--csv]
|
6
|
+
USER_NAME_FLAGS = %w[-u --user]
|
7
|
+
CONTACT_NUMBER_FLAGS = %w[-n --number]
|
8
|
+
|
9
|
+
USAGE = <<-USAGE.gsub(/^ /, '')
|
10
|
+
uso: bell [--version] [--help] <comando> [<ação>] [<argumentos>]
|
11
|
+
|
12
|
+
Comandos:
|
13
|
+
user Cria, lista, renomeia ou remove usuários
|
14
|
+
contact Importa uma lista de contatos ou liste contatos
|
15
|
+
report Mostra relatórios completos, de usuário ou de ligação
|
16
|
+
implode Remove todos os usuários e contatos
|
17
|
+
|
18
|
+
Veja `bell help <comando>` para mais informações sobre um comando específico
|
19
|
+
USAGE
|
20
|
+
|
21
|
+
def initialize(args)
|
22
|
+
@args = args
|
23
|
+
@handler ||= nil
|
24
|
+
@action ||= nil
|
25
|
+
@params = {}
|
26
|
+
end
|
27
|
+
|
28
|
+
def route
|
29
|
+
{ :handler => @handler,
|
30
|
+
:action => @action,
|
31
|
+
:params => @params }
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse
|
35
|
+
case @args[0]
|
36
|
+
when 'user' then UserCommand.new(@args[1..-1]).parse
|
37
|
+
when 'contact' then ContactCommand.new(@args[1..-1]).parse
|
38
|
+
when 'report' then ReportCommand.new(@args[1..-1]).parse
|
39
|
+
when 'implode' then ImplosionCommand.new(@args[1..-1]).parse
|
40
|
+
else raise ArgumentError, USAGE
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
def csv_given?
|
47
|
+
!(@args & CSV_FLAGS).empty?
|
48
|
+
end
|
49
|
+
|
50
|
+
def user_name_given?
|
51
|
+
!(@args & USER_NAME_FLAGS).empty?
|
52
|
+
end
|
53
|
+
|
54
|
+
def contact_number_given?
|
55
|
+
!(@args & USER_NAME_FLAGS).empty?
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Bell::Commands
|
4
|
+
class ContactCommand < Command
|
5
|
+
USAGE = <<-USAGE.gsub(/^ /, '')
|
6
|
+
uso: bell contact [<ação>] [<argumentos>]
|
7
|
+
|
8
|
+
Ações:
|
9
|
+
list [--csv] [-u|--user <usuário>]
|
10
|
+
import [-f|--force] /path/para/lista/de/contatos.csv -u|--user <usuário>
|
11
|
+
USAGE
|
12
|
+
|
13
|
+
LIST_USAGE = <<-LIST_USAGE.gsub(/^ /, '')
|
14
|
+
uso: bell contact list [<argumentos>]
|
15
|
+
|
16
|
+
Argumentos:
|
17
|
+
--csv Lista de contatos em formato CSV
|
18
|
+
-u|--user <usuário> Lista somente os contatos de determinado usuário
|
19
|
+
LIST_USAGE
|
20
|
+
|
21
|
+
IMPORT_USAGE = <<-IMPORT_USAGE.gsub(/^ /, '')
|
22
|
+
uso: bell contact import /path/contatos.csv -u|--user <usuário> [<argumentos>]
|
23
|
+
|
24
|
+
Argumentos:
|
25
|
+
-f|--force Ignora a lista de contatos e faz uma importação forçada
|
26
|
+
IMPORT_USAGE
|
27
|
+
|
28
|
+
def initialize(args)
|
29
|
+
super(args)
|
30
|
+
@handler = 'contacts_handler'
|
31
|
+
@action = @args.shift
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse
|
35
|
+
case @action
|
36
|
+
when 'import' then
|
37
|
+
if valid_contact_import_args?(@args)
|
38
|
+
@params = extract_contact_import_params(@args)
|
39
|
+
else
|
40
|
+
raise ArgumentError, IMPORT_USAGE
|
41
|
+
end
|
42
|
+
when 'list' then
|
43
|
+
if valid_contact_list_args?(@args)
|
44
|
+
@params = extract_contact_list_params(@args)
|
45
|
+
else
|
46
|
+
raise ArgumentError, LIST_USAGE
|
47
|
+
end
|
48
|
+
else
|
49
|
+
raise ArgumentError, USAGE
|
50
|
+
end
|
51
|
+
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def valid_contact_import_args?(args)
|
58
|
+
(args.length == 3 && user_name_given?) ||
|
59
|
+
(args.length == 2 && args.any? { |e| e == '-p' || e == '--public' })
|
60
|
+
end
|
61
|
+
|
62
|
+
def valid_contact_list_args?(args)
|
63
|
+
args_without_csv = args.reject { |element| element == '--csv' }
|
64
|
+
user_name_given? ? args_without_csv.length == 2 : args_without_csv.length == 0
|
65
|
+
end
|
66
|
+
|
67
|
+
def extract_contact_import_params(args)
|
68
|
+
if user_name_given?
|
69
|
+
user_name_flag = (args & USER_NAME_FLAGS).first
|
70
|
+
user_name = args[args.index(user_name_flag) + 1]
|
71
|
+
args.reject! { |e| e == user_name_flag || e == user_name }
|
72
|
+
path = args[0]
|
73
|
+
{ :path => path, :user => { :name => user_name } }
|
74
|
+
elsif args.any? { |e| e == '-p' || e == '--public' }
|
75
|
+
{ :path => args.first, :public => true }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def extract_contact_list_params(args)
|
80
|
+
if user_name_given?
|
81
|
+
user_name_flag = (args & USER_NAME_FLAGS).first
|
82
|
+
user_name = args[args.index(user_name_flag) + 1]
|
83
|
+
csv_flag = !((args & CSV_FLAGS).first || []).empty?
|
84
|
+
params = { :user => { :name => user_name } }
|
85
|
+
csv_flag ? params.merge(:csv => csv_flag) : params
|
86
|
+
else
|
87
|
+
{}
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Bell::Commands
|
4
|
+
class ReportCommand < Command
|
5
|
+
USAGE = <<-USAGE.gsub(/^ /, '')
|
6
|
+
uso: bell report /path/fatura.csv [<argumentos>]
|
7
|
+
|
8
|
+
Argumentos:
|
9
|
+
-u|--user <usuário> Mostra relatório para o dado usuário
|
10
|
+
-n|--number <número> Mostra relatório para o dado número de telefone
|
11
|
+
USAGE
|
12
|
+
|
13
|
+
FULL_REPORT_USAGE = <<-FULL_REPORT_USAGE.gsub(/^ /, '')
|
14
|
+
uso: bell report /path/fatura.csv
|
15
|
+
FULL_REPORT_USAGE
|
16
|
+
|
17
|
+
USER_REPORT_USAGE = <<-USER_REPORT_USAGE.gsub(/^ /, '')
|
18
|
+
uso: bell report /path/fatura.csv -u <usuário>
|
19
|
+
USER_REPORT_USAGE
|
20
|
+
|
21
|
+
def initialize(args = [])
|
22
|
+
super(args)
|
23
|
+
@handler = 'reports_handler'
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse
|
27
|
+
if full_report?
|
28
|
+
@action = 'full_report'
|
29
|
+
if @args[0]
|
30
|
+
@params = { :path => @args[0] }
|
31
|
+
else
|
32
|
+
raise ArgumentError, FULL_REPORT_USAGE
|
33
|
+
end
|
34
|
+
elsif user_report?
|
35
|
+
@action = 'user_report'
|
36
|
+
if valid_args_for_user_report?
|
37
|
+
@params = user_report_params
|
38
|
+
else
|
39
|
+
raise ArgumentError, USER_REPORT_USAGE
|
40
|
+
end
|
41
|
+
else
|
42
|
+
raise ArgumentError, USAGE
|
43
|
+
end
|
44
|
+
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def full_report?
|
51
|
+
!user_name_given? && !contact_number_given?
|
52
|
+
end
|
53
|
+
|
54
|
+
def user_report?
|
55
|
+
user_name_given?
|
56
|
+
end
|
57
|
+
|
58
|
+
def valid_args_for_user_report?
|
59
|
+
@args.size > 2
|
60
|
+
end
|
61
|
+
|
62
|
+
def user_report_params
|
63
|
+
user_flag = (@args & USER_NAME_FLAGS).first
|
64
|
+
user_name = @args[@args.index(user_flag) + 1]
|
65
|
+
|
66
|
+
{ :path => @args[0], :user => { :name => user_name } }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Bell::Commands
|
4
|
+
class UserCommand < Command
|
5
|
+
USAGE = <<-USAGE.gsub(/^ /, '')
|
6
|
+
uso: bell user [<ação>]
|
7
|
+
|
8
|
+
Ações:
|
9
|
+
create <usuário>
|
10
|
+
rename <nome anterior> <novo nome>
|
11
|
+
list
|
12
|
+
remove <usuário>
|
13
|
+
USAGE
|
14
|
+
|
15
|
+
CREATE_USAGE = <<-CREATE_USAGE.gsub(/^ /, '')
|
16
|
+
uso: bell user create <usuário>
|
17
|
+
CREATE_USAGE
|
18
|
+
|
19
|
+
RENAME_USAGE = <<-RENAME_USAGE.gsub(/^ /, '')
|
20
|
+
uso: bell user rename <nome anterior> <novo nome>
|
21
|
+
RENAME_USAGE
|
22
|
+
|
23
|
+
REMOVE_USAGE = <<-REMOVE_USAGE.gsub(/^ /, '')
|
24
|
+
uso: bell user create <usuário>
|
25
|
+
REMOVE_USAGE
|
26
|
+
|
27
|
+
def initialize(args = [])
|
28
|
+
super(args)
|
29
|
+
@handler = 'users_handler'
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse
|
33
|
+
case @args[0]
|
34
|
+
when 'create' then
|
35
|
+
if @args[1]
|
36
|
+
@action = 'create'
|
37
|
+
@params = { :user => { :name => @args[1] } }
|
38
|
+
else
|
39
|
+
raise ArgumentError, CREATE_USAGE
|
40
|
+
end
|
41
|
+
when 'rename' then
|
42
|
+
if valid_args_for_user_rename?
|
43
|
+
@action = 'rename'
|
44
|
+
@params = user_rename_params
|
45
|
+
else
|
46
|
+
raise ArgumentError, RENAME_USAGE
|
47
|
+
end
|
48
|
+
when 'list' then
|
49
|
+
@action = 'list'
|
50
|
+
when 'remove' then
|
51
|
+
if @args[1]
|
52
|
+
@action = 'remove'
|
53
|
+
@params = { :user => { :name => @args[1] } }
|
54
|
+
else
|
55
|
+
raise ArgumentError, CREATE_USAGE
|
56
|
+
end
|
57
|
+
else
|
58
|
+
raise ArgumentError, USAGE
|
59
|
+
end
|
60
|
+
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def valid_args_for_user_rename?
|
67
|
+
@args.length == 3
|
68
|
+
end
|
69
|
+
|
70
|
+
def user_rename_params
|
71
|
+
{ :user => { :source_name => @args[1], :target_name => @args[2] } }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,5 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'commands', 'command.rb')
|
2
|
+
require File.join(File.dirname(__FILE__), 'commands', 'user_command')
|
3
|
+
require File.join(File.dirname(__FILE__), 'commands', 'contact_command')
|
4
|
+
require File.join(File.dirname(__FILE__), 'commands', 'report_command')
|
5
|
+
require File.join(File.dirname(__FILE__), 'commands', 'implosion_command')
|