Commit 3f012c2

mo <mo.khan@gmail.com>
2018-04-15 19:48:31
build cli for sending SMS messages. tag: v0.1.0
1 parent 3798762
exe/incognito
@@ -1,8 +1,6 @@
 #!/usr/bin/env ruby
 # frozen_string_literal: true
 
-require 'incognito'
+require 'incognito/cli'
 
-sms = Incognito::Sms.new
-puts ARGV.inspect
-sms.send(to: ARGV[0], message: ARGV[1])
+Incognito::CLI::Application.start(ARGV)
lib/incognito/cli/application.rb
@@ -0,0 +1,13 @@
+module Incognito
+  module CLI
+    class Application < Thor
+      desc 'sms SUBCOMMAND ...ARGS', 'SMS messaging.'
+      subcommand :sms, SMSCommand
+
+      desc 'version', 'display the version of the gem'
+      def version
+        say Incognito::VERSION
+      end
+    end
+  end
+end
lib/incognito/cli/sms_command.rb
@@ -0,0 +1,25 @@
+module Incognito
+  module CLI
+    class SMSCommand < Thor
+      desc 'message <PHONE_NUMBER> <MESSAGE>', "Send an SMS to the phone number. (+18005557777)"
+      def message(phone_number, message)
+        say "Sending `#{message}` to #{phone_number}"
+        result = sms.send_message(to: phone_number, message: message)
+        print_table [
+          [:to, result.to],
+          [:from, result.from],
+          [:body, result.body],
+          [:error, "#{result.error_code}: #{result.error_message}"],
+          [:price, "#{result.price} #{result.price_unit}"],
+          [:uri, result.uri],
+        ]
+      end
+
+      private
+
+      def sms
+        @sms ||= Sms.new
+      end
+    end
+  end
+end
lib/incognito/cli.rb
@@ -0,0 +1,4 @@
+require 'thor'
+require 'incognito'
+require 'incognito/cli/sms_command'
+require 'incognito/cli/application'
lib/incognito/sms.rb
@@ -0,0 +1,21 @@
+module Incognito
+  class Sms
+    def initialize(sid: ENV['TWILIO_SID'], token: ENV['TWILIO_TOKEN'], phone_number: ENV['TWILIO_NUMBER'])
+      @sid = sid
+      @token = token
+      @phone_number = phone_number
+    end
+
+    def send_message(to:, message:)
+      client.messages.create(body: message, from: phone_number, to: to)
+    end
+
+    private
+
+    attr_reader :sid, :token, :phone_number
+
+    def client
+      @client ||= Twilio::REST::Client.new(sid, token)
+    end
+  end
+end
lib/incognito.rb
@@ -1,28 +1,11 @@
-require "incognito/version"
 require 'dotenv'
-require 'twilio-ruby'
 require 'pathname'
+require 'twilio-ruby'
+
+require 'incognito/sms'
+require "incognito/version"
 
 Dotenv.load(".env.local", Pathname.new(Dir.home).join(".incognitorc").to_s)
 
 module Incognito
-  class Sms
-    def initialize(sid: ENV['TWILIO_SID'], token: ENV['TWILIO_TOKEN'], phone_number: ENV['TWILIO_NUMBER'])
-      @sid = sid
-      @token = token
-      @phone_number = phone_number
-    end
-
-    def send(to:, message:)
-      client.messages.create(body: message, from: phone_number, to: to)
-    end
-
-    private
-
-    attr_reader :sid, :token, :phone_number
-
-    def client
-      @client ||= Twilio::REST::Client.new(sid, token)
-    end
-  end
 end
Gemfile.lock
@@ -3,6 +3,7 @@ PATH
   specs:
     incognito (0.1.0)
       dotenv (~> 2.2)
+      thor (~> 0.20)
       twilio-ruby (~> 5.8)
 
 GEM
@@ -18,6 +19,7 @@ GEM
     nokogiri (1.8.2)
       mini_portile2 (~> 2.3.0)
     rake (10.5.0)
+    thor (0.20.0)
     twilio-ruby (5.8.0)
       faraday (~> 0.9)
       jwt (>= 1.5, <= 2.5)
incognito.gemspec
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
 
   spec.add_dependency "dotenv", "~> 2.2"
   spec.add_dependency "twilio-ruby", "~> 5.8"
+  spec.add_dependency "thor", "~> 0.20"
   spec.add_development_dependency "bundler", "~> 1.16"
   spec.add_development_dependency "rake", "~> 10.0"
   spec.add_development_dependency "minitest", "~> 5.0"