Permalink
Browse files

utf-8 support in both ruby 1.9 and 1.8

  • Loading branch information...
1 parent 3db7e7c commit cf94c7ef59ba1f6831929831d84c87cfa70af556 @matchu matchu committed Jun 4, 2011
View
@@ -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
@@ -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!
@@ -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 @@ def mall_data=(data)
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
@@ -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,39 +49,40 @@ def params=(params)
"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
class NotFound < StandardError;end
end
end
end
+
View
@@ -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
+
View
No changes.
Binary file not shown.

0 comments on commit cf94c7e

Please sign in to comment.