From e3880eb2696c79aec6e18a774b662de09f9812c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Bobe=C5=A1=20Tu=C5=BEinsk=C3=BD?= Date: Mon, 2 Oct 2017 17:25:06 +0200 Subject: [PATCH] Added rubocop and fixed most offenses --- .rubocop.yml | 102 ++++++++++++++++++++++++++++++++++++ .rubocop_todo.yml | 38 ++++++++++++++ Rakefile | 2 +- bin/tm | 56 ++++++++++---------- gems.rb | 1 + lib/textmagic/api.rb | 21 ++++---- lib/textmagic/charset.rb | 17 +++--- lib/textmagic/error.rb | 6 ++- lib/textmagic/executor.rb | 4 +- lib/textmagic/response.rb | 26 +++++---- lib/textmagic/validation.rb | 7 ++- test/test_api.rb | 73 ++++++++++++++------------ test/test_charset.rb | 2 - test/test_executor.rb | 10 ++-- test/test_helper.rb | 14 +++-- test/test_response.rb | 31 ++++++----- test/test_validation.rb | 9 ++-- textmagic.gemspec | 10 ++-- 18 files changed, 301 insertions(+), 128 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..841101a --- /dev/null +++ b/.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 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..341921a --- /dev/null +++ b/.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' diff --git a/Rakefile b/Rakefile index c375bf5..e996e18 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/bin/tm b/bin/tm index 71a2508..617b14a 100755 --- a/bin/tm +++ b/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 @@ -53,38 +53,38 @@ 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 @@ -92,15 +92,15 @@ begin 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 diff --git a/gems.rb b/gems.rb index fd6b120..2e6741f 100644 --- a/gems.rb +++ b/gems.rb @@ -4,4 +4,5 @@ gem "minitest", require: "minitest/autorun" gem "mocha", require: "mocha/mini_test" +gem "rubocop" gem "webmock" diff --git a/lib/textmagic/api.rb b/lib/textmagic/api.rb index c0de271..f7ff952 100644 --- a/lib/textmagic/api.rb +++ b/lib/textmagic/api.rb @@ -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 @@ -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. @@ -175,7 +175,7 @@ def message_status(*ids) # It is strongly encouraged to setup callbacks to receive replies instead of # using this method. 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 @@ -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. @@ -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 diff --git a/lib/textmagic/charset.rb b/lib/textmagic/charset.rb index 8f5d6ce..195e209 100644 --- a/lib/textmagic/charset.rb +++ b/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 diff --git a/lib/textmagic/error.rb b/lib/textmagic/error.rb index 675ba09..b906516 100644 --- a/lib/textmagic/error.rb +++ b/lib/textmagic/error.rb @@ -13,7 +13,8 @@ class Error < StandardError # TextMagic::API::Error.new("error_code" => code, "error_message" => message) def initialize(*args) if args.first.is_a?(Hash) - @code, @message = args.first["error_code"], args.first["error_message"] + @code = args.first["error_code"] + @message = args.first["error_message"] else @code, @message = args end @@ -22,6 +23,9 @@ def initialize(*args) def to_s "#{@message} (#{@code})" end + end + end + end diff --git a/lib/textmagic/executor.rb b/lib/textmagic/executor.rb index 328dede..c5b7a7f 100644 --- a/lib/textmagic/executor.rb +++ b/lib/textmagic/executor.rb @@ -23,12 +23,12 @@ module Executor def execute(command, username, password, options = {}) raise TextMagic::API::Error.new(3, "Command is undefined") unless command raise TextMagic::API::Error.new(5, "Invalid username & password combination") unless username && password - options.merge!(:username => username, :password => password, :cmd => command) + options.merge!(username: username, password: password, cmd: command) options.delete_if { |key, value| !key || !value } uri = URI("https://www.textmagic.com/app/api") response = Net::HTTP.post_form(uri, options) result = JSON.parse(response.body) if response.body - raise Error.new(response.body) if result && result["error_code"] + raise Error, response.body if result && result["error_code"] result end diff --git a/lib/textmagic/response.rb b/lib/textmagic/response.rb index 5688887..27e296d 100644 --- a/lib/textmagic/response.rb +++ b/lib/textmagic/response.rb @@ -4,21 +4,22 @@ module TextMagic class API - class Response + module Response - def self.account(hash) + module_function + + def account(hash) response = OpenStruct.new(hash) response.balance = response.balance.to_f - response.balance = response.balance.to_i if response.balance % 1 == 0 + response.balance = response.balance.to_i if (response.balance % 1).zero? response end - def self.send(hash, single) - response = nil - if single - response = hash["message_id"].keys.first.dup + def send(hash, single) + response = if single + hash["message_id"].keys.first.dup else - response = hash["message_id"].invert + hash["message_id"].invert end metaclass = class << response; self; end metaclass.send :attr_accessor, :sent_text, :parts_count, :message_id @@ -28,7 +29,7 @@ def self.send(hash, single) response end - def self.message_status(hash, single) + def message_status(hash, single) response = {} hash.each do |message_id, message_hash| status = message_hash["status"].dup @@ -45,7 +46,7 @@ def self.message_status(hash, single) single ? response.values.first : response end - def self.receive(hash) + def receive(hash) response = hash["messages"].collect do |message_hash| message = "#{message_hash["from"]}: #{message_hash["text"]}" metaclass = class << message; self; end @@ -62,13 +63,16 @@ def self.receive(hash) response end - def self.check_number(hash, single) + def check_number(hash, single) response = {} hash.each do |phone, check_hash| response[phone] = OpenStruct.new(check_hash) end single ? response.values.first : response end + end + end + end diff --git a/lib/textmagic/validation.rb b/lib/textmagic/validation.rb index 0c63d4c..97f0a7d 100644 --- a/lib/textmagic/validation.rb +++ b/lib/textmagic/validation.rb @@ -4,8 +4,8 @@ class API module Validation - MAX_LENGTH_GSM = [160, 306, 459] - MAX_LENGTH_UNICODE = [70, 134, 201] + MAX_LENGTH_GSM = [160, 306, 459].freeze + MAX_LENGTH_UNICODE = [70, 134, 201].freeze # Validates message text length. Returns +true+ if the text length is # within the limits for the unicode/parts combination, otherwise it @@ -29,6 +29,9 @@ def validate_phones(*phones) phones.each { |phone| return false unless phone =~ /^\d{1,15}$/ } true end + end + end + end diff --git a/test/test_api.rb b/test/test_api.rb index 281a55f..fd5ef9f 100644 --- a/test/test_api.rb +++ b/test/test_api.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - require "test_helper" describe "API" do @@ -17,7 +15,8 @@ describe "Account command" do before do - @username, @password = random_string, random_string + @username = random_string + @password = random_string @api = TextMagic::API.new(@username, @password) @response = random_string @processed_response = random_string @@ -40,8 +39,10 @@ describe "Send command" do before do - @username, @password = random_string, random_string - @text, @phone = random_string, random_phone + @username = random_string + @password = random_string + @text = random_string + @phone = random_phone @api = TextMagic::API.new(@username, @password) @response = random_string @processed_response = random_string @@ -50,46 +51,46 @@ end it "should call Executor execute with correct arguments" do - TextMagic::API::Executor.expects(:execute).with("send", @username, @password, :text => @text, :phone => @phone, :unicode => 0).returns(@response) + TextMagic::API::Executor.expects(:execute).with("send", @username, @password, text: @text, phone: @phone, unicode: 0).returns(@response) @api.send(@text, @phone) end it "should join multiple phone numbers supplied as an array" do phones = Array.new(3) { random_phone } - TextMagic::API::Executor.expects(:execute).with("send", @username, @password, :text => @text, :phone => phones.join(","), :unicode => 0).returns(@response) + TextMagic::API::Executor.expects(:execute).with("send", @username, @password, text: @text, phone: phones.join(","), unicode: 0).returns(@response) @api.send(@text, phones) end it "should join multiple phone numbers supplied as arguments" do phones = Array.new(3) { random_phone } - TextMagic::API::Executor.expects(:execute).with("send", @username, @password, :text => @text, :phone => phones.join(","), :unicode => 0).returns(@response) + TextMagic::API::Executor.expects(:execute).with("send", @username, @password, text: @text, phone: phones.join(","), unicode: 0).returns(@response) @api.send(@text, *phones) end it "should replace true with 1 for unicode" do - TextMagic::API::Executor.expects(:execute).with("send", @username, @password, :text => @text, :phone => @phone, :unicode => 1).returns(@response) - @api.send(@text, @phone, :unicode => true) + TextMagic::API::Executor.expects(:execute).with("send", @username, @password, text: @text, phone: @phone, unicode: 1).returns(@response) + @api.send(@text, @phone, unicode: true) end it "should set unicode value to 0 if it is not set to by user and text contains only characters from GSM 03.38 charset" do - TextMagic::API::Executor.expects(:execute).with("send", @username, @password, :text => @text, :phone => @phone, :unicode => 0).returns(@response).times(2) + TextMagic::API::Executor.expects(:execute).with("send", @username, @password, text: @text, phone: @phone, unicode: 0).returns(@response).times(2) @api.send(@text, @phone) - @api.send(@text, @phone, :unicode => false) + @api.send(@text, @phone, unicode: false) end it "should raise an error if unicode is set to 0 and text contains characters outside of GSM 03.38 charset" do text = "Вильма Привет" assert_raises TextMagic::API::Error do - @api.send(text, @phone, :unicode => false) + @api.send(text, @phone, unicode: false) end end it "should raise an error if unicode value is not valid" do assert_raises TextMagic::API::Error do - @api.send(@text, @phone, :unicode => 2 + rand(10)) + @api.send(@text, @phone, unicode: 2 + rand(10)) end assert_raises TextMagic::API::Error do - @api.send(@text, @phone, :unicode => random_string) + @api.send(@text, @phone, unicode: random_string) end end @@ -124,14 +125,14 @@ it "should support send_time option" do time = Time.now + rand - TextMagic::API::Executor.expects(:execute).with("send", @username, @password, :text => @text, :phone => @phone, :unicode => 0, :send_time => time.to_i).returns(@response) - @api.send(@text, @phone, :send_time => time.to_i) + TextMagic::API::Executor.expects(:execute).with("send", @username, @password, text: @text, phone: @phone, unicode: 0, send_time: time.to_i).returns(@response) + @api.send(@text, @phone, send_time: time.to_i) end it "should convert send_time to Fixnum" do time = Time.now + rand - TextMagic::API::Executor.expects(:execute).with("send", @username, @password, :text => @text, :phone => @phone, :unicode => 0, :send_time => time.to_i).returns(@response) - @api.send(@text, @phone, :send_time => time) + TextMagic::API::Executor.expects(:execute).with("send", @username, @password, text: @text, phone: @phone, unicode: 0, send_time: time.to_i).returns(@response) + @api.send(@text, @phone, send_time: time) end it "should call Response.send method to process the response hash (single phone)" do @@ -151,7 +152,8 @@ describe "Message status command" do before do - @username, @password = random_string, random_string + @username = random_string + @password = random_string @api = TextMagic::API.new(@username, @password) @response = random_string @processed_response = random_string @@ -161,19 +163,19 @@ it "should call Executor execute with correct arguments" do id = random_string - TextMagic::API::Executor.expects(:execute).with("message_status", @username, @password, :ids => id).returns(@response) + TextMagic::API::Executor.expects(:execute).with("message_status", @username, @password, ids: id).returns(@response) @api.message_status(id) end it "should join ids supplied as array" do ids = Array.new(3) { random_string } - TextMagic::API::Executor.expects(:execute).with("message_status", @username, @password, :ids => ids.join(",")) + TextMagic::API::Executor.expects(:execute).with("message_status", @username, @password, ids: ids.join(",")) @api.message_status(ids) end it "should join ids supplied as arguments" do ids = Array.new(3) { random_string } - TextMagic::API::Executor.expects(:execute).with("message_status", @username, @password, :ids => ids.join(",")) + TextMagic::API::Executor.expects(:execute).with("message_status", @username, @password, ids: ids.join(",")) @api.message_status(*ids) end @@ -199,7 +201,8 @@ describe "Receive command" do before do - @username, @password = random_string, random_string + @username = random_string + @password = random_string @api = TextMagic::API.new(@username, @password) @response = random_string @processed_response = random_string @@ -208,13 +211,13 @@ end it "should call Executor execute with correct arguments" do - TextMagic::API::Executor.expects(:execute).with("receive", @username, @password, :last_retrieved_id => nil) + TextMagic::API::Executor.expects(:execute).with("receive", @username, @password, last_retrieved_id: nil) @api.receive end it "should accept last_retrieved_id optional value" do last_retrieved_id = rand(1e10) - TextMagic::API::Executor.expects(:execute).with("receive", @username, @password, :last_retrieved_id => last_retrieved_id) + TextMagic::API::Executor.expects(:execute).with("receive", @username, @password, last_retrieved_id: last_retrieved_id) @api.receive(last_retrieved_id) end @@ -227,7 +230,8 @@ describe "Delete reply command" do before do - @username, @password = random_string, random_string + @username = random_string + @password = random_string @api = TextMagic::API.new(@username, @password) @response = random_string @processed_response = random_string @@ -237,19 +241,19 @@ it "should call Executor execute with correct arguments" do id = random_string - TextMagic::API::Executor.expects(:execute).with("delete_reply", @username, @password, :ids => id) + TextMagic::API::Executor.expects(:execute).with("delete_reply", @username, @password, ids: id) @api.delete_reply(id) end it "should join ids supplied as array" do ids = Array.new(3) { random_string } - TextMagic::API::Executor.expects(:execute).with("delete_reply", @username, @password, :ids => ids.join(",")) + TextMagic::API::Executor.expects(:execute).with("delete_reply", @username, @password, ids: ids.join(",")) @api.delete_reply(ids) end it "should join ids supplied as arguments" do ids = Array.new(3) { random_string } - TextMagic::API::Executor.expects(:execute).with("delete_reply", @username, @password, :ids => ids.join(",")) + TextMagic::API::Executor.expects(:execute).with("delete_reply", @username, @password, ids: ids.join(",")) @api.delete_reply(*ids) end @@ -268,7 +272,8 @@ describe "Check number command" do before do - @username, @password = random_string, random_string + @username = random_string + @password = random_string @api = TextMagic::API.new(@username, @password) @response = random_string @processed_response = random_string @@ -278,19 +283,19 @@ it "should call Executor execute with correct arguments" do phone = random_phone - TextMagic::API::Executor.expects(:execute).with("check_number", @username, @password, :phone => phone) + TextMagic::API::Executor.expects(:execute).with("check_number", @username, @password, phone: phone) @api.check_number(phone) end it "should join phones supplied as array" do phones = Array.new(3) { random_phone } - TextMagic::API::Executor.expects(:execute).with("check_number", @username, @password, :phone => phones.join(",")) + TextMagic::API::Executor.expects(:execute).with("check_number", @username, @password, phone: phones.join(",")) @api.check_number(phones) end it "should join phones supplied as arguments" do phones = Array.new(3) { random_phone } - TextMagic::API::Executor.expects(:execute).with("check_number", @username, @password, :phone => phones.join(",")) + TextMagic::API::Executor.expects(:execute).with("check_number", @username, @password, phone: phones.join(",")) @api.check_number(*phones) end diff --git a/test/test_charset.rb b/test/test_charset.rb index 6452d03..63bfa8b 100644 --- a/test/test_charset.rb +++ b/test/test_charset.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - require "test_helper" describe "Charset" do diff --git a/test/test_executor.rb b/test/test_executor.rb index 4a7a48e..6be26d1 100644 --- a/test/test_executor.rb +++ b/test/test_executor.rb @@ -5,8 +5,10 @@ describe "execute method" do before do - @username, @password = random_string, random_string - @command, @options = random_string, random_hash + @username = random_string + @password = random_string + @command = random_string + @options = random_hash @uri = "https://www.textmagic.com/app/api" end @@ -35,7 +37,7 @@ it "should not send parameters with nil keys" do options_with_empty_values = @options.merge(nil => random_string) - @options.merge!(:username => @username, :password => @password, :cmd => @command) + @options.merge!(username: @username, password: @password, cmd: @command) WebMock. stub_request(:post, @uri). with(body: @options). @@ -45,7 +47,7 @@ it "should not send parameters with nil values" do options_with_empty_values = @options.merge(random_string => nil) - @options.merge!(:username => @username, :password => @password, :cmd => @command) + @options.merge!(username: @username, password: @password, cmd: @command) WebMock. stub_request(:post, @uri). with(body: @options). diff --git a/test/test_helper.rb b/test/test_helper.rb index 7f01dcd..10a46b8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,7 +6,7 @@ def random_string(legth = 5 + rand(10)) end def random_phone - rand(10 ** 12).to_s + rand(10**12).to_s end def random_hash @@ -15,11 +15,15 @@ def random_hash hash end -class Minitest::Test +module Minitest + + class Test + + def self.it(name, &block) + test_name = "test_#{name.gsub(/\s+/, "_")}".to_sym + define_method(test_name, &block) + end - def self.it(name, &block) - test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym - define_method(test_name, &block) end end diff --git a/test/test_response.rb b/test/test_response.rb index 561ba93..4c08bf3 100644 --- a/test/test_response.rb +++ b/test/test_response.rb @@ -22,7 +22,8 @@ describe "Response to send command with single phone number" do before do - @message_id, @phone = random_string, random_phone + @message_id = random_string + @phone = random_phone @text = random_string @parts_count = 1 + rand(3) @hash = { "message_id" => { @message_id => @phone }, "sent_text" => @text, "parts_count" => @parts_count } @@ -45,8 +46,10 @@ describe "Response to send command with multiple phone numbers" do before do - @message_id1, @phone1 = random_string, random_phone - @message_id2, @phone2 = random_string, random_phone + @message_id1 = random_string + @phone1 = random_phone + @message_id2 = random_string + @phone2 = random_phone @text = random_string @parts_count = 1 + rand(3) @hash = { "message_id" => { @message_id1 => @phone1, @message_id2 => @phone2 }, "sent_text" => @text, "parts_count" => @parts_count } @@ -91,8 +94,8 @@ "created_time" => @created_time.to_s, "reply_number" => @reply_number, "completed_time" => @completed_time.to_s, - "credits_cost" => @credits_cost - } + "credits_cost" => @credits_cost, + }, } @response = TextMagic::API::Response.message_status(@hash, true) end @@ -142,8 +145,8 @@ "created_time" => @created_time, "reply_number" => @reply_number, "completed_time" => @completed_time, - "credits_cost" => @credits_cost - } + "credits_cost" => @credits_cost, + }, } @response = TextMagic::API::Response.message_status(@hash, false) end @@ -189,12 +192,14 @@ before do @timestamp = (Time.now - 30).to_i - @text, @phone, @message_id = random_string, random_phone, random_string + @text = random_string + @phone = random_phone + @message_id = random_string @message = { "timestamp" => @timestamp, "from" => @phone, "text" => @text, - "message_id" => @message_id + "message_id" => @message_id, } @unread = rand(1e4) @hash = { "unread" => @unread, "messages" => [@message] } @@ -239,8 +244,8 @@ @hash = { @phone => { "price" => @price, - "country" => @country - } + "country" => @country, + }, } @response = TextMagic::API::Response.check_number(@hash, true) end @@ -267,8 +272,8 @@ @hash = { @phone => { "price" => @price, - "country" => @country - } + "country" => @country, + }, } @response = TextMagic::API::Response.check_number(@hash, false) end diff --git a/test/test_validation.rb b/test/test_validation.rb index 6f4d9ad..a08bb7a 100644 --- a/test/test_validation.rb +++ b/test/test_validation.rb @@ -75,11 +75,11 @@ describe "validate_phones method" do it "should return true if phone number consists of up to 15 digits" do - assert_equal true, TextMagic::API.validate_phones(rand(10 ** 15).to_s) + assert_equal true, TextMagic::API.validate_phones(rand(10**15).to_s) end it "should return false if phone number is longer than 15 digits" do - assert_equal false, TextMagic::API.validate_phones((10 ** 16 + rand(10 ** 15)).to_s) + assert_equal false, TextMagic::API.validate_phones((10**16 + rand(10**15)).to_s) end it "should return false if phone number contains non-digits" do @@ -91,7 +91,8 @@ end it "should return true if all phone numbers in a list are valid" do - phone1, phone2 = rand(10 ** 15).to_s, rand(10 ** 15).to_s + phone1 = rand(10**15).to_s + phone2 = rand(10**15).to_s assert_equal true, TextMagic::API.validate_phones(phone1, phone2) assert_equal true, TextMagic::API.validate_phones([phone1, phone2]) end @@ -101,7 +102,7 @@ end it "should return false if format of any of phone numbers in a list is invalid" do - phone1 = rand(10 ** 15).to_s, rand(10 ** 15).to_s + phone1 = rand(10**15).to_s, rand(10**15).to_s phone2 = random_string assert_equal false, TextMagic::API.validate_phones(phone1, phone2) assert_equal false, TextMagic::API.validate_phones([phone1, phone2]) diff --git a/textmagic.gemspec b/textmagic.gemspec index 31c4ee7..48e7dfb 100644 --- a/textmagic.gemspec +++ b/textmagic.gemspec @@ -1,19 +1,19 @@ -# -*- encoding: utf-8 -*- + require File.expand_path("../lib/textmagic/version", __FILE__) Gem::Specification.new do |gem| gem.authors = ["Vladimir Bobes Tuzinsky"] gem.email = ["vladimir@tuzinsky.com"] - gem.summary = %q{Ruby interface to the TextMagic's Bulk SMS Gateway} - gem.description = %q{ + gem.summary = "Ruby interface to the TextMagic's Bulk SMS Gateway" + gem.description = " textmagic is a Ruby interface to the TextMagic's Bulk SMS Gateway. It can be used to easily integrate SMS features into your application. It supports sending messages, receiving replies and more. You need to have a valid TextMagic account to use this gem. You can get one at http://www.textmagic.com. - } + " gem.homepage = "" - gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } gem.files = `git ls-files`.split("\n") gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") gem.name = "textmagic"