Skip to content

Instantly share code, notes, and snippets.

@kusakari
Created March 10, 2011 06:40
Show Gist options
  • Save kusakari/863667 to your computer and use it in GitHub Desktop.
Save kusakari/863667 to your computer and use it in GitHub Desktop.
oauth gem を日本のソーシャルプラットフォームで使う
# -*- coding: utf-8 -*-
require 'addressable/uri'
module OAuth::RequestProxy
class ActionControllerRequest < OAuth::RequestProxy::Base
def parameters_for_signature_with_opensocial
params = []
params << options[:parameters].to_query if options[:parameters]
unless options[:clobber_request]
params << header_params.to_query
params << request.query_string unless query_string_blank?
# mixi は signature 生成時に query_string しか使用しない
if request.post? && request.content_type == Mime::Type.lookup("application/x-www-form-urlencoded") && !AppResources.mixi?
raw_post = request.raw_post
# 携帯からの送信の場合、signature 生成前にエンコーディングを設定する
if request.mobile &&
!(request.mobile.instance_of?(Jpmobile::Mobile::Vodafone) || request.mobile.instance_of?(Jpmobile::Mobile::Softbank))
if Object.const_defined?(:Encoding) && raw_post.encoding != 'Windows-31J'
raw_post_hash = Rack::Utils.parse_query(raw_post)
raw_post_array = raw_post_hash.inject([]) do |a,(k,values)|
if values.is_a?(Array)
a << values.map do |s|
if s.is_a?(String)
s = Kconv.kconv(s, Kconv::SJIS, Kconv::UTF8) unless s.nil?
"#{k}=#{CGI.escape(s.force_encoding('Windows-31J'))}"
else
"#{k}=#{CGI.escape(s)}"
end
end
else
s = Kconv.kconv(s, Kconv::SJIS, Kconv::UTF8) unless s.nil?
a << "#{k}=#{CGI.escape(values.force_encoding('Windows-31J'))}"
end
end
raw_post = raw_post_array.join('&')
end
end
params << raw_post
end
end
params.
join('&').split('&').
reject(&:blank?).
map { |p| p.split('=').map{|esc| CGI.unescape(esc)} }.
reject { |kv| kv[0] == 'oauth_signature'}
end
alias_method_chain :parameters_for_signature, :opensocial
end
end
module OAuth
module Helper
def escape_with_rfc3986(value)
return Addressable::URI.encode_component(value.to_s, OAuth::RESERVED_CHARACTERS)
rescue ArgumentError
return Addressable::URI.encode_component(value.to_s.force_encoding(Encoding::UTF_8), OAuth::RESERVED_CHARACTERS)
end
def sorted_numerically(values)
values.sort do |a,b|
numeric_from_string(a) <=> numeric_from_string(b)
end
end
def numeric_from_string(s)
if match = /(\d+)/.match(s)
return match[0].to_i
else
return 0
end
end
# GREEは同名パラメーターの値のソートを asort($values, SORT_NUMERIC) としているっぽい
def normalize_with_gree(params)
hash = {}
# prepare same key parameters
params.sort.each_with_index do |values, i|
k, v = *values
if hash[k].nil?
hash[k] = v.nil? ? '' : v
else
unless hash[k].is_a?(Array)
hash[k] = [hash[k]]
end
hash[k] << (v.nil? ? '' : v)
end
end
params = hash.inject([]) {|a,(k,v)| a << [k, v] }
params.sort.map do |k, values|
if values.is_a?(Array)
# multiple values were provided for a single key
sorted_values = sorted_numerically(values)
sorted_values.map do |v|
[escape(k),escape(v)] * "="
end
else
[escape(k),escape(values)] * "="
end
end * "&"
end
if AppResources.gree?
alias_method_chain :escape, :rfc3986
alias_method_chain :normalize, :gree
elsif AppResources.mbga?
alias_method_chain :escape, :rfc3986
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment