chagol 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/chagol CHANGED
@@ -9,7 +9,16 @@ if !Dir.exists?(Dir.home + "/.chagol")
9
9
  Chagol.install_chagol
10
10
  puts "Please enter your contacts in ~/.chagol/contacts.json\n
11
11
  Modify the one that already exists and add as many more as \n
12
- you want in the same format"
12
+ you want in the same format.\n
13
+ Before using chagol you need to edit your configuration file ~/.chagol/config.rb"
14
+ exit
15
+ end
16
+
17
+ require File.expand_path("~/.chagol/config")
18
+ # Check if the user has configured chagol exit if not done
19
+ if $user[:phone_no] == "" || $user[:password] == ""
20
+ puts "Please enter your phone no and your password in ~/.chagol/config.rb"
21
+ exit
13
22
  end
14
23
 
15
24
  # Parse the command line options with the awesome optionparser
@@ -39,26 +48,20 @@ end.parse!
39
48
  # A message is necessary for all actions except quering
40
49
  # for the version of the gem
41
50
  if options[:message] && (options[:users] || options[:lists])
42
- # Load the contacts in from the json also TODO load lists too
51
+ # Load the contacts in from the json
43
52
  contacts = Array.new
44
53
  File.open(File.expand_path("~/.chagol/contacts.json","r")).each_line do |line|
45
54
  contacts << JSON.parse(line)
46
55
  end
47
- # Load the username, password and provider from the config file
48
- require File.expand_path("~/.chagol/config")
56
+ # Instantiate the messenger with the users configuration
49
57
  messenger = Chagol::SmsSender.new($user[:phone_no], $user[:password], $user[:provider])
50
58
  # Send messages to individual users
51
59
  if options[:users] && options[:users].length > 0
52
- #TODO : Replace these stubs with the actual code to send messages
53
- puts "Your Message will be sent to the following users"
54
- options[:users].each do |target|
55
- contacts.each do |contact|
56
- if target.casecmp(contact.name) == 0
57
- puts "#{target}, #{contact.phone} => #{options[:message]}"
58
- messenger.send(contact.phone,options[:message])
59
- end
60
- end
61
- end
60
+ phone_nos = Chagol.get_phone_nos_from_name(contacts, options[:users])
61
+ phone_nos.each do |no|
62
+ messenger.send(no,options[:message])
63
+ puts no
64
+ end
62
65
  end
63
66
  # Send messages to user created lists
64
67
  if options[:lists] && options[:lists].length > 0
@@ -69,5 +72,6 @@ if options[:message] && (options[:users] || options[:lists])
69
72
  else
70
73
  puts "The great chagol isn't a mind reader!
71
74
  Please specify a recipient or a list of recipients
72
- to whom you want the message delivered."
75
+ to whom you want the message delivered.
76
+ Run chagol -h to learn what it can do."
73
77
  end
@@ -8,8 +8,43 @@ module Chagol
8
8
  Dir.mkdir(chagol_path)
9
9
  # Create the sample json files for the contacts
10
10
  File.open(chagol_path + "/contacts.json", "w") do |f|
11
- f.write('{"json_class":"Contact","data":{"id":1,"name":"Nikhil","phone":"9900000018"}}')
11
+ f.write('{"json_class":"Chagol::Contact","data":{"id":1,"name":"Nikhil","phone":"9900000018"}}')
12
12
  end
13
13
  # Create the json for the lists
14
+ #
15
+ # Create the user configuration
16
+ File.open(chagol_path + "/config.rb", "w") do |f|
17
+ f.write('# Please change this file to replace your phone no, password and provider
18
+ # The password is your way2sms/160by2/provider password
19
+
20
+ $user = { phone_no: "",
21
+ password: "",
22
+ provider: "way2sms" }')
23
+ end
24
+ end
25
+
26
+ # More helper methods
27
+
28
+ # This finds the phone nos for the users whose names have been passed as
29
+ # command line arguments also it returns phones no's that were passed
30
+ # directly. The messages are to be sent to each no returned from here.
31
+ def self.get_phone_nos_from_name(contacts, names)
32
+ phone_nos = Array.new
33
+ # Parse out phone no's first and remove them from the names first
34
+ names.delete_if do |ph_no|
35
+ if /[7-9][0-9]{9}/.match(ph_no)
36
+ phone_nos << ph_no
37
+ end
38
+ end
39
+ # And find the phone no's from the contacts next
40
+ names.each do |target|
41
+ contacts.each do |contact|
42
+ if target.casecmp(contact.name) == 0
43
+ # puts "#{target}, #{contact.phone}" # debug mode info
44
+ phone_nos << contact.phone
45
+ end
46
+ end
47
+ end
48
+ return phone_nos.uniq
14
49
  end
15
50
  end
@@ -1,3 +1,3 @@
1
1
  module Chagol
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chagol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
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-06-27 00:00:00.000000000 Z
12
+ date: 2012-06-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Chagol can send messages to any Indian mobile no
15
15
  email: