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 "character-encodings", "~> 0.4.1", :platforms => :ruby_18
group :development_async do
# async wrappers
gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git'

View file

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

View file

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

View file

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