utf-8 support in both ruby 1.9 and 1.8

This commit is contained in:
Emi Matchu 2011-06-04 18:40:15 -04:00
parent 3db7e7c7b2
commit cf94c7ef59
7 changed files with 37 additions and 15 deletions

View file

@ -29,6 +29,8 @@ gem 'resque', '~> 1.15.0'
gem 'right_aws', '~> 2.1.0' gem 'right_aws', '~> 2.1.0'
gem "character-encodings", "~> 0.4.1", :platforms => :ruby_18
group :development_async do group :development_async do
# async wrappers # async wrappers
gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git' gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git'

View file

@ -74,6 +74,7 @@ GEM
arel (2.0.8) arel (2.0.8)
bcrypt-ruby (2.1.4) bcrypt-ruby (2.1.4)
builder (2.1.2) builder (2.1.2)
character-encodings (0.4.1)
closure-compiler (1.0.0) closure-compiler (1.0.0)
compass (0.10.6) compass (0.10.6)
haml (>= 3.0.4) haml (>= 3.0.4)
@ -179,6 +180,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
RocketAMF! RocketAMF!
addressable addressable
character-encodings (~> 0.4.1)
compass (~> 0.10.1) compass (~> 0.10.1)
devise (~> 1.1.5) devise (~> 1.1.5)
em-http-request! em-http-request!

View file

@ -1,5 +1,6 @@
require 'fileutils' require 'fileutils'
require 'uri' require 'uri'
require 'utf8'
class SwfAsset < ActiveRecord::Base class SwfAsset < ActiveRecord::Base
PUBLIC_ASSET_DIR = File.join('swfs', 'outfit') PUBLIC_ASSET_DIR = File.join('swfs', 'outfit')
@ -177,7 +178,7 @@ class SwfAsset < ActiveRecord::Base
if response.is_a? Net::HTTPSuccess if response.is_a? Net::HTTPSuccess
new_local_path = File.join(LOCAL_ASSET_DIR, local_path_within_outfit_swfs) new_local_path = File.join(LOCAL_ASSET_DIR, local_path_within_outfit_swfs)
new_local_dir = File.dirname new_local_path new_local_dir = File.dirname new_local_path
content = response.body.force_encoding 'utf-8' content = +response.body
FileUtils.mkdir_p new_local_dir FileUtils.mkdir_p new_local_dir
File.open(new_local_path, 'w') do |f| File.open(new_local_path, 'w') do |f|
f.print content f.print content

View file

@ -1,39 +1,42 @@
require 'active_support/core_ext/hash' require 'active_support/core_ext/hash'
require 'msgpack' require 'msgpack'
require 'openneo-auth-signatory' require 'openneo-auth-signatory'
require 'utf8'
module Openneo module Openneo
module Auth module Auth
class Session class Session
REMOTE_MSG_KEYS = %w(session_id source user) REMOTE_MSG_KEYS = %w(session_id source user)
TMP_STORAGE_DIR = Rails.root.join('tmp', 'openneo-auth-sessions') TMP_STORAGE_DIR = Rails.root.join('tmp', 'openneo-auth-sessions')
attr_writer :id attr_writer :id
def save! def save!
content = +MessagePack.pack(@message)
FileUtils.mkdir_p TMP_STORAGE_DIR FileUtils.mkdir_p TMP_STORAGE_DIR
File.open(tmp_storage_path, 'w') do |file| File.open(tmp_storage_path, 'w') do |file|
file.write MessagePack.pack(@message).force_encoding('utf-8') file.write content
end end
end end
def destroy! def destroy!
File.delete(tmp_storage_path) File.delete(tmp_storage_path)
end end
def load_message! def load_message!
raise NotFound, "Session #{id} not found" unless File.exists?(tmp_storage_path) raise NotFound, "Session #{id} not found" unless File.exists?(tmp_storage_path)
@message = File.open(tmp_storage_path, 'r') do |file| @message = File.open(tmp_storage_path, 'r') do |file|
MessagePack.unpack file.read MessagePack.unpack file.read
end end
end end
def params=(params) def params=(params)
unless Auth.config.secret unless Auth.config.secret
raise "Must set config.secret to the remote auth server's secret" raise "Must set config.secret to the remote auth server's secret"
end end
given_signature = params['signature'] given_signature = params['signature']
signatory = Auth::Signatory.new(Auth.config.secret.force_encoding('utf-8')) secret = +Auth.config.secret
signatory = Auth::Signatory.new(secret)
REMOTE_MSG_KEYS.each do |key| REMOTE_MSG_KEYS.each do |key|
unless params.include?(key) unless params.include?(key)
raise MissingParam, "Missing required param #{key.inspect}" raise MissingParam, "Missing required param #{key.inspect}"
@ -46,35 +49,35 @@ module Openneo
"did not match message #{@message.inspect} (#{correct_signature})" "did not match message #{@message.inspect} (#{correct_signature})"
end end
end end
def user def user
Auth.config.find_user_with_remote_auth(@message['user']) Auth.config.find_user_with_remote_auth(@message['user'])
end end
def self.from_params(params) def self.from_params(params)
session = new session = new
session.params = params session.params = params
session session
end end
def self.find(id) def self.find(id)
session = new session = new
session.id = id session.id = id
session.load_message! session.load_message!
session session
end end
private private
def id def id
@id ||= @message[:session_id] @id ||= @message[:session_id]
end end
def tmp_storage_path def tmp_storage_path
name = "#{id}.mpac" name = "#{id}.mpac"
File.join TMP_STORAGE_DIR, name File.join TMP_STORAGE_DIR, name
end end
class InvalidSession < ArgumentError;end class InvalidSession < ArgumentError;end
class InvalidSignature < InvalidSession;end class InvalidSignature < InvalidSession;end
class MissingParam < InvalidSession;end class MissingParam < InvalidSession;end
@ -82,3 +85,4 @@ module Openneo
end end
end end
end end

13
lib/utf8.rb Normal file
View file

@ -0,0 +1,13 @@
if String.method_defined?(:force_encoding)
# UTF-8 support is native, so let's route the +"abc" syntax to the native
# encode method.
class String
def +@
force_encoding('utf-8')
end
end
else
require 'encoding/character/utf-8'
end

0
tmp/restart.txt Normal file
View file

Binary file not shown.