Skip to content

Commit

Permalink
Merge pull request #1 from mortro/master
Browse files Browse the repository at this point in the history
Extend gem to handle unicode messages
  • Loading branch information
tmschndr committed Nov 21, 2011
2 parents 632d1ef + 1e5974d commit f297bbc
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 71 deletions.
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source :rubygems

gemspec
30 changes: 30 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,30 @@
PATH
remote: .
specs:
mobilove (0.0.4)
rest-client

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
mime-types (1.17.2)
rake (0.9.2.2)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.7.0)
rspec-core (~> 2.7.0)
rspec-expectations (~> 2.7.0)
rspec-mocks (~> 2.7.0)
rspec-core (2.7.1)
rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.7.0)

PLATFORMS
ruby

DEPENDENCIES
mobilove!
rake
rspec
13 changes: 5 additions & 8 deletions Rakefile
@@ -1,12 +1,9 @@
require 'rake'
require 'rake/testtask'
require 'bundler/gem_tasks'

Rake::TestTask.new do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end

task :spec => :check_dependencies

task :default => :spec
17 changes: 10 additions & 7 deletions lib/mobilove/exceptions.rb
@@ -1,11 +1,11 @@
module Mobilove

class InvalidNumber < Exception
end

class InvalidSender < Exception
end

class MessageTooLong < Exception
end

Expand All @@ -14,13 +14,13 @@ class InvalidMessageType < Exception

class InvalidRoute < Exception
end

class AuthenticationFailed < Exception
end

class NoCreditLeft < Exception
end

class RouteCannotHandleProvider < Exception
end

Expand All @@ -32,5 +32,8 @@ class SMSCTransferFailed < Exception

class UnknownError < Exception
end

end

class MessageIsNoUtf8String < Exception
end

end
48 changes: 37 additions & 11 deletions lib/mobilove/text.rb
@@ -1,24 +1,50 @@
# coding: utf-8
require 'uri'
require 'iconv'

module Mobilove

class Text
def initialize(key, route, from)
@key, @route, @from = key, route, from
end
def send(to, message, debug_mode = false)
url = send_url(to, message, debug_mode)

def send_message(to, message, options={})
url = send_url(to, message, options)
response = RestClient.get(url)
respond(response)
end

private

def send_url(to, message, debug_mode)
"http://gw.mobilant.net/?key=#{@key}&to=#{to}&message=#{URI.escape(message)}&route=#{@route}&from=#{URI.escape(@from)}&debug=#{debug_mode ? '1' : '0'}&charset=utf-8"

# :debug_mode => true // Message will not be sent
# :concat => true // Message will be sent as concatenated texts if it has more than 160 chars (70 unicode)
def send_url(to, message, options={})
options[:debug_mode] ||= false
options[:concat] ||= false
if is_gsm0338_encoded? message
"http://gw.mobilant.net/?key=#{@key}&to=#{to}&message=#{URI.escape(message)}&route=#{@route}&from=#{URI.escape(@from)}&debug=#{options[:debug_mode] ? '1' : '0'}&charset=utf-8&concat=#{options[:concat] ? '1' : '0'}"
else
"http://gw.mobilant.net/?key=#{@key}&to=#{to}&message=#{string_to_hexadecimal_code_points(message)}&route=#{@route}&from=#{URI.escape(@from)}&debug=#{options[:debug_mode] ? '1' : '0'}&messagetype=unicode&concat=#{options[:concat] ? '1' : '0'}"
end
end

def is_gsm0338_encoded?(message)
gsm0338 = "@£$¥èéùìòÇ\fØø\nÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>\?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà\^\{\}\[~\]\|€"
message.each_char {|c| return false unless gsm0338.include?(c)}
return true
end


def string_to_hexadecimal_code_points(message)
if message.class.to_s == 'String' && message.encoding.to_s == 'UTF-8'
hex_code = ''
Iconv.iconv("UCS-2", "utf-8", message).first.each_char {|unicode_char| hex_code += unicode_char.unpack('H*').first}
hex_code
else
raise MessageIsNoUtf8String.new("The message is either not a string or not UTF-8 encoded. MESSAGE: #{message}")
end
end

def respond(response)
case response.code.to_i
when 100
Expand Down Expand Up @@ -50,5 +76,5 @@ def respond(response)
end
end
end
end

end
4 changes: 4 additions & 0 deletions mobilove.gemspec
Expand Up @@ -12,6 +12,10 @@ Gem::Specification.new do |s|
s.summary = "Small wrapper for sending text messages with mobilant.de"
s.description = "Use mobilove to send text messages with Ruby. Account on mobilant.de required."

s.add_dependency 'rest-client'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'

s.files = `git ls-files app lib`.split("\n")
s.platform = Gem::Platform::RUBY
s.require_path = 'lib'
Expand Down
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,12 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
require 'mobilove'

require 'rspec'
require 'rspec/autorun'

# require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = 'annoying'
# require 'ruby-debug/completion'

# Requiring custom spec helpers
Dir[File.dirname(__FILE__) + "/spec_helpers/**/*.rb"].sort.each { |f| require File.expand_path(f) }

0 comments on commit f297bbc

Please sign in to comment.