Class: EFreeSMS::Bin

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/efreesms/bin.rb

Defined Under Namespace

Modules: Exec

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



84
85
86
# File 'lib/efreesms/bin.rb', line 84

def self.run
  instance.run
end

Instance Method Details

#editObject



72
73
74
75
76
77
78
79
80
# File 'lib/efreesms/bin.rb', line 72

def edit
  editor = Shellwords.shellwords(@editor)
  file = Tempfile.new(['sms', '.txt']).tap {|x| x.close }.path
  Exec.system(*editor, file)

  File.read(file).tap {
    File.unlink(file)
  }
end

#parse_optsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/efreesms/bin.rb', line 39

def parse_opts
  OptionParser.new {|opts|
    opts.banner = "Usage: #$0 [options] phone"

    opts.on('-e', '--editor EDITOR', 'Select editor') {|ed|
      @editor = ed
    }

    opts.on('-c', '--country COUNTRY', 'Select recipients\' country') {|c|
      @country = c
    }

    opts.on('-u', '--user USER', 'Select your name in sms') {|u|
      @user = u
    }

    opts.on_tail('-v', '--version', 'Show version') {
      puts "e-freesms sender #{EFreeSMS::VERSION}"
      exit 0
    }
  }.tap {|opts|
    o = opts.parse!(ARGV)

    if o.size != 1
      $stderr.puts opts
      exit 1
    end

    @number = o.first
    @user ||= ENV['USER']
  }
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/efreesms/bin.rb', line 23

def run
  reset
  parse_opts

  unless @user and @editor and @country and @number
    $stderr.puts "Please, set up the client"
    exit 1
  end

  EFreeSMS::SMS.send(@user, @country, @number, edit)
end