Skip to content

Commit

Permalink
Added rubocop and fixed most offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
bobes committed Oct 2, 2017
1 parent a7cb5d0 commit e3880eb
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 128 deletions.
102 changes: 102 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,102 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.0

Documentation:
Enabled: false

Lint/EndAlignment:
EnforcedStyleAlignWith: variable

Lint/LiteralInInterpolation:
Enabled: true

Metrics/AbcSize:
Exclude:
- test/**/*

Metrics/ClassLength:
Exclude:
- test/**/*

Metrics/LineLength:
Max: 120
Exclude:
- test/**/*

Metrics/MethodLength:
Exclude:
- test/**/*

Layout/AlignParameters:
EnforcedStyle: with_fixed_indentation

Layout/CaseIndentation:
EnforcedStyle: end

Layout/DotPosition:
EnforcedStyle: trailing

Layout/EmptyLinesAroundBlockBody:
Enabled: false

Layout/EmptyLinesAroundClassBody:
EnforcedStyle: empty_lines

Layout/EmptyLinesAroundModuleBody:
EnforcedStyle: empty_lines

Layout/FirstParameterIndentation:
EnforcedStyle: special_for_inner_method_call

Layout/IndentArray:
EnforcedStyle: consistent

Layout/IndentHash:
EnforcedStyle: consistent

Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented
IndentationWidth: 2

Layout/MultilineOperationIndentation:
EnforcedStyle: indented
IndentationWidth: 2

Style/AutoResourceCleanup:
Enabled: true

Style/DoubleNegation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Style/MethodCalledOnDoEndBlock:
Enabled: true

Style/MethodCalledOnDoEndBlock:
Exclude:
- test/**/*

Style/Send:
Enabled: true

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes

Style/StringMethods:
Enabled: true

Style/SymbolArray:
Enabled: true

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInLiteral:
EnforcedStyleForMultiline: comma
38 changes: 38 additions & 0 deletions .rubocop_todo.yml
@@ -0,0 +1,38 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-10-02 15:24:21 +0000 using RuboCop version 0.50.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 4
Metrics/AbcSize:
Max: 35

# Offense count: 16
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 265

# Offense count: 2
Metrics/CyclomaticComplexity:
Max: 13

# Offense count: 4
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 17

# Offense count: 2
Metrics/PerceivedComplexity:
Max: 11

# Offense count: 27
Style/Send:
Exclude:
- 'bin/tm'
- 'lib/textmagic/api.rb'
- 'lib/textmagic/response.rb'
- 'test/test_api.rb'
- 'test/test_response.rb'
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -6,4 +6,4 @@ Rake::TestTask.new(:test) do |test|
test.pattern = "test/**/test_*.rb"
test.verbose = true
end
task :default => :test
task default: :test
56 changes: 28 additions & 28 deletions bin/tm
@@ -1,44 +1,44 @@
#!/usr/bin/env ruby
require 'optparse'
require "optparse"

lib = File.join(File.dirname(__FILE__), '..', 'lib', 'textmagic')
lib = File.join(File.dirname(__FILE__), "..", "lib", "textmagic")

if File.exist?("#{lib}.rb")
require lib
else
require 'rubygems'
require 'textmagic'
require "rubygems"
require "textmagic"
end

filename = File.join(ENV['HOME'], '.textmagic')
filename = File.join(ENV["HOME"], ".textmagic")
options = YAML.load_file(filename) if File.exist?(filename)
options ||= {}

parser = OptionParser.new do |opts|
opts.banner = 'Usage:'
opts.banner = "Usage:"
opts.separator " "
opts.separator [
"tm account",
'tm send PHONE[,PHONE2[,PHONE3 ...]] MESSAGE',
'tm status MESSAGE_ID',
'tm receive [LAST_RETREIVED_ID]',
'tm delete MESSAGE_ID [MESSAGE_ID2 [MESSAGE_ID3 ...]]'
"tm send PHONE[,PHONE2[,PHONE3 ...]] MESSAGE",
"tm status MESSAGE_ID",
"tm receive [LAST_RETREIVED_ID]",
"tm delete MESSAGE_ID [MESSAGE_ID2 [MESSAGE_ID3 ...]]",
]

opts.separator " "
opts.separator "Specific options:"

opts.on('-u', '--username USERNAME',
opts.on("-u", "--username USERNAME",
"Specify your TextMagic API username (overrides ~/.textmagic setting)") do |username|
options['username'] = username
options["username"] = username
end

opts.on('-p', '--password PASSWORD',
opts.on("-p", "--password PASSWORD",
"Specify your TextMagic API password (overrides ~/.textmagic setting)") do |password|
options['password'] = password
options["password"] = password
end

opts.on_tail('-h', '--help', "Show this message") do
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
Expand All @@ -53,54 +53,54 @@ end

command = ARGV.shift

unless options['username']
unless options["username"]
puts "Username not specified. Use --help option to find out details"
exit 1
end

unless options['password']
unless options["password"]
puts "Password not specified. Use --help option to find out details"
exit 1
end

api = TextMagic::API.new(options['username'], options['password'])
api = TextMagic::API.new(options["username"], options["password"])

begin
case command
when 'account'
when "account"
puts "Your account's balance: #{api.account.balance} credits"
when 'send'
unless phones = ARGV.shift
when "send"
unless (phones = ARGV.shift)
puts "Phone number(s) and message not specified. Use --help option to find out details"
exit 1
end
if (text = ARGV.join(' ')).empty?
if (text = ARGV.join(" ")).empty?
puts "Message not specified. Use --help option to find out details"
exit 1
end
response = api.send(text, phones.split(','))
response = api.send(text, phones.split(","))
puts "Sent text: #{response.sent_text}"
puts "Parts: #{response.parts_count}"
response.each do |phone, message_id|
puts "Message id (#{phone}): #{message_id}"
end
when 'status'
when "status"
if ARGV.empty?
puts "Message id(s) not specified. Use --help option to find out details"
exit 1
end
api.status(ARGV).each do |message_id, status|
puts "Status (#{message_id}): #{status}"
end
when 'receive'
when "receive"
response = api.receive(ARGV.first)
response.each do |message|
puts "#{message} [#{message.message_id}, #{message.timestamp}]"
end
puts 'No new messages' if response.empty?
when 'delete'
puts "No new messages" if response.empty?
when "delete"
api.delete(ARGV)
puts 'Message(s) deleted'
puts "Message(s) deleted"
else
puts "Unknown command #{command}. Use --help option to find out details"
exit 1
Expand Down
1 change: 1 addition & 0 deletions gems.rb
Expand Up @@ -4,4 +4,5 @@

gem "minitest", require: "minitest/autorun"
gem "mocha", require: "mocha/mini_test"
gem "rubocop"
gem "webmock"
21 changes: 11 additions & 10 deletions lib/textmagic/api.rb
Expand Up @@ -88,13 +88,13 @@ def send(text, *args)
when nil then unicode ? 1 : 0
else raise Error.new(10, "Wrong parameter value #{options[:unicode]} for parameter unicode")
end
raise Error.new(6, "Message contains invalid characters") if unicode && options[:unicode] == 0
raise Error.new(6, "Message contains invalid characters") if unicode && options[:unicode].zero?
raise Error.new(7, "Message too long") unless API.validate_text_length(text, unicode)
single = args.size == 1 && args.first.is_a?(String)
phones = args.flatten
raise Error.new(9, "Invalid phone number format") unless API.validate_phones(phones)
options[:send_time] = options[:send_time].to_i if options[:send_time]
hash = Executor.execute("send", @username, @password, options.merge(:text => text, :phone => phones.join(",")))
hash = Executor.execute("send", @username, @password, options.merge(text: text, phone: phones.join(",")))
TextMagic::API::Response.send(hash, single)
end

Expand Down Expand Up @@ -141,10 +141,10 @@ def message_status(*ids)
single = ids.size == 1 && ids.first.is_a?(String)
ids.flatten!
raise TextMagic::API::Error.new(4, "Insufficient parameters") if ids.empty?
hash = Executor.execute("message_status", @username, @password, :ids => ids.join(","))
hash = Executor.execute("message_status", @username, @password, ids: ids.join(","))
TextMagic::API::Response.message_status(hash, single)
end
alias :status :message_status
alias status message_status

# Executes a receive command by sending a request to the TextMagic's
# SMS gateway.
Expand Down Expand Up @@ -175,7 +175,7 @@ def message_status(*ids)
# <b>It is strongly encouraged to setup callbacks to receive replies instead of
# using this method.</b>
def receive(last_retrieved_id = nil)
hash = Executor.execute("receive", @username, @password, :last_retrieved_id => last_retrieved_id)
hash = Executor.execute("receive", @username, @password, last_retrieved_id: last_retrieved_id)
TextMagic::API::Response.receive(hash)
end

Expand All @@ -192,13 +192,12 @@ def receive(last_retrieved_id = nil)
# api.delete_reply("173205", "223606")
# api.delete_reply(["244948", "264575"])
def delete_reply(*ids)
single = ids.size == 1 && ids.first.is_a?(String)
ids.flatten!
raise TextMagic::API::Error.new(4, "Insufficient parameters") if ids.empty?
Executor.execute("delete_reply", @username, @password, :ids => ids.join(","))
Executor.execute("delete_reply", @username, @password, ids: ids.join(","))
true
end
alias :delete :delete_reply
alias delete delete_reply

# Executes a check_number command by sending a request to the TextMagic's
# SMS gateway.
Expand Down Expand Up @@ -239,9 +238,11 @@ def check_number(*phones)
single = phones.size == 1 && phones.first.is_a?(String)
phones.flatten!
raise TextMagic::API::Error.new(4, "Insufficient parameters") if phones.empty?
hash = Executor.execute("check_number", @username, @password, :phone => phones.join(","))
hash = Executor.execute("check_number", @username, @password, phone: phones.join(","))
TextMagic::API::Response.check_number(hash, single)
end
alias :check :check_number
alias check check_number

end

end
17 changes: 11 additions & 6 deletions lib/textmagic/charset.rb
@@ -1,30 +1,35 @@
# encoding: utf-8

module TextMagic

class API

module Charset

GSM_CHARSET = "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\e\f^{}\\[~]|€ÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà".scan(/./u)
ESCAPED_CHARS = "{}\\~[]|€"
GSM_CHARSET =
"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\e\f^{}\\[~]|€ÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡"\
"ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà".scan(/./u).freeze
ESCAPED_CHARS = "{}\\~[]|€".freeze

# Returns +true+ if the supplied text contains only characters from
# GSM 03.38 charset, otherwise it returns +false+.
def is_gsm(text)
def gsm?(text)
text.scan(/./u).each { |c| return false unless GSM_CHARSET.include?(c) }
true
end
alias is_gsm gsm?

# Returns +true+ if the supplied text contains characters outside of
# GSM 03.38 charset, otherwise it returns +false+.
def is_unicode(text)
def unicode?(text)
!is_gsm(text)
end
alias is_unicode unicode?

def real_length(text, unicode)
text.size + (unicode ? 0 : text.scan(/[\{\}\\~\[\]\|€]/).size)
end

end

end

end

0 comments on commit e3880eb

Please sign in to comment.