From 3eec57ca018e8e7ed2f929b82aa6228ba1f502a7 Mon Sep 17 00:00:00 2001 From: IWAMOTO Takashi Date: Tue, 12 Aug 2008 22:28:30 +0900 Subject: [PATCH] reconstructing... --- TODO | 1 + config/main.yml | 12 +-- lib/pinto.rb | 122 +++++++++++++++++++++-- lib/pinto/characters.rb | 15 +++ lib/pinto/config.rb | 36 +++++-- lib/pinto/config/db.rb | 17 +--- lib/pinto/config/key.rb | 17 ++++ lib/pinto/controller/base.rb | 59 +++++++++++ lib/pinto/controller/error.rb | 17 ++++ lib/pinto/controller/index.rb | 44 +++----- lib/pinto/controller/multiple.rb | 22 ++++ lib/pinto/controller/name.rb | 22 ++++ lib/pinto/controller/private/base.rb | 69 ------------- lib/pinto/controller/private/error.rb | 25 ----- lib/pinto/controller/private/multiple.rb | 33 ------ lib/pinto/controller/signup_account.rb | 72 +++++-------- lib/pinto/controller/signup_auth.rb | 53 ++++------ lib/pinto/controller/signup_openid.rb | 50 ++++------ lib/pinto/controller/users.rb | 50 ++-------- lib/pinto/core_ext/nil.rb | 5 + lib/pinto/{ => core_ext}/pathname.rb | 1 - lib/pinto/{ => core_ext}/string.rb | 3 +- lib/pinto/db/host.rb | 8 +- lib/pinto/db/name.rb | 8 +- lib/pinto/db/password.rb | 10 +- lib/pinto/db/user.rb | 10 +- lib/pinto/dispatcher.rb | 41 +------- lib/pinto/encoding/utf8.rb | 6 +- lib/pinto/error/message.rb | 17 ++++ lib/pinto/helper/html/escaper.rb | 37 ++++--- lib/pinto/helper/translator.rb | 25 ++--- lib/pinto/helper/uri.rb | 25 ++--- lib/pinto/http/request.rb | 106 ++++++++++++++++++++ lib/pinto/http/response.rb | 67 +++++++++++++ lib/pinto/http/response/body.rb | 19 ++++ lib/pinto/http/status_code.rb | 13 +++ lib/pinto/locale.rb | 54 +++++----- lib/pinto/locale/code.rb | 22 ++++ lib/pinto/model/signup_reservation.rb | 12 +-- lib/pinto/model/user.rb | 13 ++- lib/pinto/open_id.rb | 36 +++---- lib/pinto/open_id/claimed_id.rb | 13 +++ lib/pinto/open_id/provider.rb | 19 ++++ lib/pinto/open_id/providers.rb | 33 ++++++ lib/pinto/open_id/user_supplied_id.rb | 17 ++++ lib/pinto/request.rb | 51 ---------- lib/pinto/server/environment.rb | 13 +++ lib/pinto/translate/message_id.rb | 13 +++ lib/pinto/translator.rb | 15 +-- lib/pinto/type/claimed_id.rb | 17 ---- lib/pinto/type/config_key.rb | 17 ---- lib/pinto/type/controller_name.rb | 17 ---- lib/pinto/type/error_message.rb | 17 ---- lib/pinto/type/escape_chars.rb | 17 ---- lib/pinto/type/http_status_code.rb | 17 ---- lib/pinto/type/query_strings.rb | 17 ---- lib/pinto/type/uri.rb | 17 ---- lib/pinto/type/uri_map.rb | 17 ---- lib/pinto/type/user_name.rb | 17 ---- lib/pinto/type/user_supplied_id.rb | 17 ---- lib/pinto/type/view_name.rb | 17 ---- lib/pinto/type/view_param.rb | 17 ---- lib/pinto/uri.rb | 20 ++++ lib/pinto/uri/expand_processor.rb | 15 +-- lib/pinto/uri/extract_processor.rb | 20 ++-- lib/pinto/uri/parameters.rb | 25 +++++ lib/pinto/uri/query_strings.rb | 13 +++ lib/pinto/uri/template.rb | 17 ++++ lib/pinto/uri/templates.rb | 38 +++++++ lib/pinto/uri/templates/record.rb | 23 +++++ lib/pinto/view.rb | 37 +++++-- lib/pinto/view/context.rb | 11 +- lib/pinto/view/name.rb | 23 +++++ lib/pinto/view/parameters.rb | 21 ++++ lib/pinto/view/xhtml.rb | 32 +++--- locale/en/pinto.mo | Bin 1158 -> 1106 bytes locale/en/pinto.po | 39 +++----- locale/ja/pinto.mo | Bin 1264 -> 1209 bytes locale/ja/pinto.po | 39 +++----- locale/pinto.pot | 49 +++++---- spec/pinto/locale_spec.rb | 10 +- view/index.erb | 8 +- view/multiple.erb | 14 ++- view/signup_account.erb | 6 +- view/signup_openid.erb | 12 +-- 85 files changed, 1203 insertions(+), 938 deletions(-) create mode 100755 lib/pinto/characters.rb create mode 100755 lib/pinto/config/key.rb create mode 100755 lib/pinto/controller/base.rb create mode 100755 lib/pinto/controller/error.rb create mode 100755 lib/pinto/controller/multiple.rb create mode 100755 lib/pinto/controller/name.rb delete mode 100755 lib/pinto/controller/private/base.rb delete mode 100755 lib/pinto/controller/private/error.rb delete mode 100755 lib/pinto/controller/private/multiple.rb create mode 100755 lib/pinto/core_ext/nil.rb rename lib/pinto/{ => core_ext}/pathname.rb (90%) rename lib/pinto/{ => core_ext}/string.rb (77%) create mode 100755 lib/pinto/error/message.rb create mode 100755 lib/pinto/http/request.rb create mode 100755 lib/pinto/http/response.rb create mode 100755 lib/pinto/http/response/body.rb create mode 100755 lib/pinto/http/status_code.rb create mode 100755 lib/pinto/locale/code.rb create mode 100755 lib/pinto/open_id/claimed_id.rb create mode 100755 lib/pinto/open_id/provider.rb create mode 100755 lib/pinto/open_id/providers.rb create mode 100755 lib/pinto/open_id/user_supplied_id.rb delete mode 100755 lib/pinto/request.rb create mode 100755 lib/pinto/server/environment.rb create mode 100755 lib/pinto/translate/message_id.rb delete mode 100755 lib/pinto/type/claimed_id.rb delete mode 100755 lib/pinto/type/config_key.rb delete mode 100755 lib/pinto/type/controller_name.rb delete mode 100755 lib/pinto/type/error_message.rb delete mode 100755 lib/pinto/type/escape_chars.rb delete mode 100755 lib/pinto/type/http_status_code.rb delete mode 100755 lib/pinto/type/query_strings.rb delete mode 100755 lib/pinto/type/uri.rb delete mode 100755 lib/pinto/type/uri_map.rb delete mode 100755 lib/pinto/type/user_name.rb delete mode 100755 lib/pinto/type/user_supplied_id.rb delete mode 100755 lib/pinto/type/view_name.rb delete mode 100755 lib/pinto/type/view_param.rb create mode 100755 lib/pinto/uri.rb create mode 100755 lib/pinto/uri/parameters.rb create mode 100755 lib/pinto/uri/query_strings.rb create mode 100755 lib/pinto/uri/template.rb create mode 100755 lib/pinto/uri/templates.rb create mode 100755 lib/pinto/uri/templates/record.rb create mode 100755 lib/pinto/view/name.rb create mode 100755 lib/pinto/view/parameters.rb diff --git a/TODO b/TODO index f607a9a..6f6701f 100755 --- a/TODO +++ b/TODO @@ -1,4 +1,5 @@ ・できるだけ基本データ型をラップする +・受け入れ側は寛容に! ・既存クラスのテストを書く ・ユーザー登録処理をBDDで実装する diff --git a/config/main.yml b/config/main.yml index 49e98d6..6e69df7 100755 --- a/config/main.yml +++ b/config/main.yml @@ -1,10 +1,10 @@ uri_templates: - index: http://{lang}pinto.jp/ - signup_openid: http://{lang}pinto.jp/signup_forms/openid - signup_auth: http://{lang}pinto.jp/signup_forms/auth{query} - signup_account: http://{lang}pinto.jp/signup_forms/account{query} - users: http://{lang}pinto.jp/users - user: http://{lang}pinto.jp/users/{user_name} + index: http://{locale_code}pinto.jp/ + signup_openid: http://{locale_code}pinto.jp/signup_forms/openid + signup_auth: http://{locale_code}pinto.jp/signup_forms/auth{query_string} + signup_account: http://{locale_code}pinto.jp/signup_forms/account{query_string} + users: http://{locale_code}pinto.jp/users + user: http://{locale_code}pinto.jp/users/{user_name} openid_providers: - yahoo.com diff --git a/lib/pinto.rb b/lib/pinto.rb index 03d3edc..0e5c38d 100755 --- a/lib/pinto.rb +++ b/lib/pinto.rb @@ -1,21 +1,123 @@ -# lib/pinto.rb require 'pathname' -require 'pinto/pathname' -require 'pinto/string' -require 'rubygems' +require 'pinto/core_ext/nil' +require 'pinto/core_ext/pathname' +require 'pinto/core_ext/string' + module Pinto - VERSION = [0, 0, 1] def self.version - return VERSION.join('.') + return '0.0.1' + end + + autoload :Characters, 'pinto/characters' + autoload :Config, 'pinto/config' + autoload :Dispatcher, 'pinto/dispatcher' + autoload :Locale, 'pinto/locale' + autoload :OpenID, 'pinto/open_id' + autoload :Translator, 'pinto/translator' + autoload :URI, 'pinto/uri' + autoload :View, 'pinto/view' + + class Config + autoload :DB, 'pinto/config/db' + autoload :Key, 'pinto/config/key' + end + + module Controller + autoload :Base, 'pinto/controller/base' + autoload :Error, 'pinto/controller/error' + autoload :Index, 'pinto/controller/index' + autoload :Multiple, 'pinto/controller/multiple' + autoload :Name, 'pinto/controller/name' + autoload :SignupAccount, 'pinto/controller/signup_account' + autoload :SignupAuth, 'pinto/controller/signup_auth' + autoload :SignupOpenid, 'pinto/controller/signup_openid' + autoload :User, 'pinto/controller/user' + autoload :Users, 'pinto/controller/users' + end + + module DB + autoload :Host, 'pinto/db/host' + autoload :Name, 'pinto/db/name' + autoload :Password, 'pinto/db/password' + autoload :User, 'pinto/db/user' + end + + module Encoding + autoload :UTF8, 'pinto/encoding/utf8' + end + + module Error + autoload :Message, 'pinto/error/message' + end + + module Helper + autoload :Translator, 'pinto/helper/translator' + autoload :URI, 'pinto/helper/uri' + + module HTML + autoload :Escaper, 'pinto/helper/html/escaper' + end + end + + module HTTP + autoload :Request, 'pinto/http/request' + autoload :Response, 'pinto/http/response' + autoload :StatusCode, 'pinto/http/status_code' + + class Response + autoload :Body, 'pinto/http/response/body' + end + end + + class Locale + autoload :Code, 'pinto/locale/code' + end + + module Model + autoload :SignupReservation, 'pinto/model/signup_reservation' + autoload :User, 'pinto/model/user' + end + + class OpenID + autoload :ClaimedID, 'pinto/open_id/claimed_id' + autoload :UserSuppliedID, 'pinto/open_id/user_supplied_id' + autoload :Providers, 'pinto/open_id/providers' + autoload :Provider, 'pinto/open_id/provider' + end + + module Server + autoload :Environment, 'pinto/server/environment' + end + + module Translate + autoload :MessageID, 'pinto/translate/message_id' + end + + class URI + autoload :ExpandProcessor, 'pinto/uri/expand_processor' + autoload :ExtractProcessor, 'pinto/uri/extract_processor' + autoload :Parameters, 'pinto/uri/parameters' + autoload :QueryStrings, 'pinto/uri/query_strings' + autoload :Template, 'pinto/uri/template' + autoload :Templates, 'pinto/uri/templates' + + class Templates + autoload :Record, 'pinto/uri/templates/record' + end + end + + class View + autoload :Context, 'pinto/view/context' + autoload :Name, 'pinto/view/name' + autoload :Parameters, 'pinto/view/parameters' + autoload :XHTML, 'pinto/view/xhtml' end end -autoload :DBI, 'dbi' +autoload :DBI, 'dbi' autoload :YAML, 'yaml' -gem 'activesupport' -require 'active_support' -Dependencies.load_paths << 'lib' +require 'rubygems' gem 'addressable' module Addressable diff --git a/lib/pinto/characters.rb b/lib/pinto/characters.rb new file mode 100755 index 0000000..b9beb90 --- /dev/null +++ b/lib/pinto/characters.rb @@ -0,0 +1,15 @@ +module Pinto + class Characters + def initialize(characters) + unless characters.respond_to? :to_a + raise TypeError.new('characters must respond to #to_a') + end + + @characters = characters.to_a + end + + def to_a + return @characters + end + end +end diff --git a/lib/pinto/config.rb b/lib/pinto/config.rb index 2976b33..2955234 100755 --- a/lib/pinto/config.rb +++ b/lib/pinto/config.rb @@ -1,16 +1,36 @@ -# lib/pinto/config.rb module Pinto class Config - def self.load(key) - unless key.is_a? Pinto::Type::ConfigKey - raise ArgumentError.new('key must be Pinto::Type::ConfigKey') - end + def self.load(config_key) + config_key = Pinto::Config::Key.new(config_key) + config = YAML.load_file('config/main.yml') - if config[key.to_s].nil? - raise ArgumentError.new('non-existent key was given') + if config[config_key.to_s].nil? + raise ArgumentError.new('non-existent config_key was given') + end + + return config[config_key.to_s] + end + + def self.uri_templates + uri_templates = Pinto::URI::Templates.new + self.load('uri_templates').each do |controller_name, uri_template| + uri_templates.add(controller_name, uri_template) end + return uri_templates + end + + def self.openid_providers + openid_providers = Pinto::OpenID::Providers.new + self.load('openid_providers').each do |openid_provider| + openid_providers.add(openid_provider) + end + return openid_providers + end + + def self.valid_openid_provider?(openid_provider) + openid_provider = Pinto::OpenID::Provider.new(openid_provider) - return config[key.to_s] + return self.openid_providers.include?(openid_provider) end end end diff --git a/lib/pinto/config/db.rb b/lib/pinto/config/db.rb index 11c2a34..f2cc9e9 100755 --- a/lib/pinto/config/db.rb +++ b/lib/pinto/config/db.rb @@ -1,4 +1,3 @@ -# lib/pinto/config/db.rb module Pinto class Config class DB @@ -13,18 +12,10 @@ def self.load end def initialize(host, name, user, password) - unless host.is_a? Pinto::DB::Host - raise ArgumentError.new('host must be Pinto::DB::Host') - end - unless name.is_a? Pinto::DB::Name - raise ArgumentError.new('name must be Pinto::DB::Name') - end - unless user.is_a? Pinto::DB::User - raise ArgumentError.new('user must be Pinto::DB::User') - end - unless password.is_a? Pinto::DB::Password - raise ArgumentError.new('password must be Pinto::DB::Password') - end + host = Pinto::DB::Host.new(host) + name = Pinto::DB::Name.new(name) + user = Pinto::DB::User.new(user) + password = Pinto::DB::Password.new(password) @host = host @name = name diff --git a/lib/pinto/config/key.rb b/lib/pinto/config/key.rb new file mode 100755 index 0000000..3bd324b --- /dev/null +++ b/lib/pinto/config/key.rb @@ -0,0 +1,17 @@ +module Pinto + class Config + class Key + def initialize(config_key) + unless config_key.respond_to? :to_s + raise TypeError.new('config_key must respond to #to_s') + end + + @config_key = config_key.to_s + end + + def to_s + return @config_key + end + end + end +end diff --git a/lib/pinto/controller/base.rb b/lib/pinto/controller/base.rb new file mode 100755 index 0000000..e222cf2 --- /dev/null +++ b/lib/pinto/controller/base.rb @@ -0,0 +1,59 @@ +module Pinto + module Controller + module Base + def run(request) + request = Pinto::HTTP::Request.new(request) + + if request.get? || request.head? + method = 'get_action' + elsif request.post? + method = 'post_action' + elsif request.put? + method = 'put_action' + elsif request.delete? + method = 'delete_action' + elsif request.options? + method = 'options_action' + end + + if self.methods.include? method + return self.method(method).call(request) + end + + return Pinto::Controller::Private::Error.run( + request, + 405, + Pinto::Translator.new(request.uri_parameters.locale_code)._( + 'Requested HTTP method is invalid for this resource' + ) + ) + end + + def options_action(request) + request = Pinto::HTTP::Request.new(request) + + allowed_methods = [] + if self.methods.include? 'get_action' + allowed_methods.push('GET') + allowed_methods.push('HEAD') + end + if self.methods.include? 'post_action' + allowed_methods.push('POST') + end + if self.methods.include? 'put_action' + allowed_methods.push('PUT') + end + if self.methods.include? 'delete_action' + allowed_methods.push('DELETE') + end + allowed_methods.push('OPTIONS') + + response = Pinto::HTTP::Response.new + response.status_code = 200 + response.allow = allowed_methods.join(', ') + response.body = view.render + return response + end + end + end +end diff --git a/lib/pinto/controller/error.rb b/lib/pinto/controller/error.rb new file mode 100755 index 0000000..8dd8f84 --- /dev/null +++ b/lib/pinto/controller/error.rb @@ -0,0 +1,17 @@ +module Pinto + module Controller + class Error + def self.run(request, status_code, message = '') + request = Pinto::HTTP::Request.new(request) + status_code = Pinto::HTTP::StatusCode.new(status_code) + message = Pinto::Error::Message.new(message) + + response = Pinto::HTTP::Response.new + response.status_code = status_code + response.content_type = 'text/plain' + response.body = message + return response + end + end + end +end diff --git a/lib/pinto/controller/index.rb b/lib/pinto/controller/index.rb index 9480d00..7e5e082 100755 --- a/lib/pinto/controller/index.rb +++ b/lib/pinto/controller/index.rb @@ -1,39 +1,27 @@ -# lib/pinto/controller/index.rb module Pinto module Controller class Index - include Pinto::Controller::Private::Base + include Pinto::Controller::Base + def get_action(request) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end + request = Pinto::HTTP::Request.new(request) - request_lang = request.get_uri_map.to_hash['lang'] - if request_lang.empty? - return Pinto::Controller::Private::Multiple.run(request) + if request.no_locale? + return Pinto::Controller::Multiple.run(request) end - base_lang = Pinto::Locale.new(request_lang) - other_languages = base_lang.others - param = { - :lang => request_lang, - :other_langs => other_languages - } - - view_name = Pinto::Type::ViewName.new('index') - view_param = Pinto::Type::ViewParam.new(param) - response_body = Pinto::View.render(view_name, view_param) - - platonic_uri = Pinto::Helper::URI.uri('index') + view = Pinto::View.new + view.name = 'index' + view.set_parameter(:locale_code, request.locale_code) + view.set_parameter(:other_locales, + Pinto::Locale.others(request.locale_code)) - return [ - 200, - { - 'Content-Type' => 'application/xhtml+xml; charset=UTF-8', - 'Content-Location' => platonic_uri - }, - [response_body] - ] + response = Pinto::HTTP::Response.new + response.status_code = 200 + response.content_type = 'application/xhtml+xml; charset=UTF-8' + response.content_location = Pinto::Helper::URI.expand('index') + response.body = view.render + return response end end end diff --git a/lib/pinto/controller/multiple.rb b/lib/pinto/controller/multiple.rb new file mode 100755 index 0000000..258d314 --- /dev/null +++ b/lib/pinto/controller/multiple.rb @@ -0,0 +1,22 @@ +module Pinto + module Controller + class Multiple + def self.run(request) + request = Pinto::HTTP::Request.new(request) + + view = Pinto::View.new + view.name = 'multiple' + view.set_parameter(:requested_controller_name, + request.controller_name) + view.set_parameter(:locales, Pinto::Locale.list) + view.set_parameter(:uri_parameters, request.uri_parameters) + + response = Pinto::HTTP::Response.new + response.status_code = 300 + response.content_type = 'application/xhtml+xml; charset=UTF-8' + response.body = view.render + return response + end + end + end +end diff --git a/lib/pinto/controller/name.rb b/lib/pinto/controller/name.rb new file mode 100755 index 0000000..0e5488f --- /dev/null +++ b/lib/pinto/controller/name.rb @@ -0,0 +1,22 @@ +module Pinto + module Controller + class Name + def initialize(controller_name) + unless controller_name.respond_to? :to_s + raise TypeError.new('controller_name must respond to #to_s') + end + + @controller_name = controller_name.to_s + end + + def to_s + return @controller_name + end + + def ==(other) + other = Pinto::Controller::Name.new(other) + return (self.to_s == other.to_s) + end + end + end +end diff --git a/lib/pinto/controller/private/base.rb b/lib/pinto/controller/private/base.rb deleted file mode 100755 index 7798ef1..0000000 --- a/lib/pinto/controller/private/base.rb +++ /dev/null @@ -1,69 +0,0 @@ -# lib/pinto/controller/private/base.rb -module Pinto - module Controller - module Private - module Base - def run(request) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end - if request.get? || request.head? - method = 'get_action' - elsif request.post? - method = 'post_action' - elsif request.put? - method = 'put_action' - elsif request.delete? - method = 'delete_action' - elsif request.options? - method = 'options_action' - end - - if self.methods.include? method - return self.method(method).call(request) - end - - http_status_code = Pinto::Type::HttpStatusCode.new(405) - translator = Pinto::Translator.new(request.get_uri_map['lang']) - message = translator._( - 'Requested HTTP method is invalid for this resource' - ) - message = Pinto::Type::ErrorMessage.new(message) - return Pinto::Controller::Private::Error.run( - request, http_status_code, message - ) - end - - def options_action(request) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end - - allowed_methods = [] - if self.methods.include? 'get_action' - allowed_methods.push('GET') - allowed_methods.push('HEAD') - end - if self.methods.include? 'post_action' - allowed_methods.push('POST') - end - if self.methods.include? 'put_action' - allowed_methods.push('PUT') - end - if self.methods.include? 'delete_action' - allowed_methods.push('DELETE') - end - allowed_methods.push('OPTIONS') - return [ - 200, - { - 'Allow' => allowed_methods.join(', '), - 'Content-Type' => '' - }, - [] - ] - end - end - end - end -end diff --git a/lib/pinto/controller/private/error.rb b/lib/pinto/controller/private/error.rb deleted file mode 100755 index ae2af8e..0000000 --- a/lib/pinto/controller/private/error.rb +++ /dev/null @@ -1,25 +0,0 @@ -# lib/pinto/controller/private/error.rb -module Pinto - module Controller - module Private - class Error - def self.run(request, code, message = Pinto::Type::ErrorMessage.new('')) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end - unless code.is_a? Pinto::Type::HttpStatusCode - raise ArgumentError.new('code must be Pinto::Type::HttpStatusCode') - end - unless message.is_a? Pinto::Type::ErrorMessage - raise ArgumentError.new('message must be Pinto::Type::ErrorMessage') - end - return [ - code.to_i, - {'Content-Type' => 'text/plain'}, - [message.to_s] - ] - end - end - end - end -end diff --git a/lib/pinto/controller/private/multiple.rb b/lib/pinto/controller/private/multiple.rb deleted file mode 100755 index ea3166f..0000000 --- a/lib/pinto/controller/private/multiple.rb +++ /dev/null @@ -1,33 +0,0 @@ -# lib/pinto/controller/private/multiple.rb -module Pinto - module Controller - module Private - class Multiple - def self.run(request) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end - languages = Pinto::Locale.list - uri_map = request.get_uri_map.to_hash - uri_map.delete('lang') - - param = { - :controller => request.get_controller_name.to_s, - :languages => languages, - :uri_map => uri_map - } - - view_name = Pinto::Type::ViewName.new('multiple') - view_param = Pinto::Type::ViewParam.new(param) - response_body = Pinto::View.render(view_name, view_param) - - return [ - 300, - {'Content-Type' => 'application/xhtml+xml; charset=UTF-8'}, - [response_body] - ] - end - end - end - end -end diff --git a/lib/pinto/controller/signup_account.rb b/lib/pinto/controller/signup_account.rb index 4b30a1e..6d0fd9e 100755 --- a/lib/pinto/controller/signup_account.rb +++ b/lib/pinto/controller/signup_account.rb @@ -1,62 +1,42 @@ -# lib/pinto/controller/signup_account.rb module Pinto module Controller class SignupAccount - include Pinto::Controller::Private::Base + include Pinto::Controller::Base + def get_action(request) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end + request = Pinto::HTTP::Request.new(request) - request_lang = request.get_uri_map.to_hash['lang'] - if request_lang.empty? - http_status_code = Pinto::Type::HttpStatusCode.new(400) - translator = Pinto::Translator.new( - Pinto::Locale.new(request_lang) - ) - message = translator._('URI contains no valid language') - message = Pinto::Type::ErrorMessage.new(message) - return Pinto::Controller::Private::Error.run( - request, http_status_code, message + if request.no_locale? + return Pinto::Controller::Error.run( + request, 400, Pinto::Translator.new._( + 'URI contains no valid language' + ) ) end - query_strings = Pinto::Type::QueryStrings.new(request.GET) - current_uri = Pinto::Type::URI.new(request.url) - claimed_id = Pinto::OpenID.complete(query_strings, current_uri) - if claimed_id.nil? - http_status_code = Pinto::Type::HttpStatusCode.new(400) - translator = Pinto::Translator.new( - Pinto::Locale.new(request_lang) - ) - message = translator._('OpenID authentication failed') - message = Pinto::Type::ErrorMessage.new(message) - return Pinto::Controller::Private::Error.run( - request, http_status_code, message + begin + claimed_id = Pinto::OpenID.complete(request.query_strings, + request.uri) + rescue + return Pinto::Controller::Error.run( + request, 400, Pinto::Translator.new(request.locale_code)._( + 'OpenID authentication failed' + ) ) end - Pinto::Model::SignupReservation.add( - Pinto::Type::ClaimedID.new(claimed_id) - ) - - base_lang = Pinto::Locale.new(request_lang) - other_languages = base_lang.others - param = { - :lang => request_lang, - :claimed_id => claimed_id, - :user_name => '' - } + Pinto::Model::SignupReservation.add(claimed_id) - view_name = Pinto::Type::ViewName.new('signup_account') - view_param = Pinto::Type::ViewParam.new(param) - response_body = Pinto::View.render(view_name, view_param) + view = Pinto::View.new + view.name = 'signup_account' + view.set_parameter(:locale_code, request.locale_code) + view.set_parameter(:claimed_id, claimed_id) - return [ - 200, - {'Content-Type' => 'application/xhtml+xml; charset=UTF-8'}, - [response_body] - ] + response = Pinto::HTTP::Response.new + response.status_code = 200 + response.content_type = 'application/xhtml+xml; charset=UTF-8' + response.body = view.render + return response end end end diff --git a/lib/pinto/controller/signup_auth.rb b/lib/pinto/controller/signup_auth.rb index 02ccd45..16d04b2 100755 --- a/lib/pinto/controller/signup_auth.rb +++ b/lib/pinto/controller/signup_auth.rb @@ -1,47 +1,36 @@ -# lib/pinto/controller/signup_auth.rb module Pinto module Controller class SignupAuth - include Pinto::Controller::Private::Base + include Pinto::Controller::Base + def get_action(request) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end + request = Pinto::HTTP::Request.new(request) - request_lang = request.get_uri_map.to_hash['lang'] - openid = request.GET['openid'] + user_supplied_id = Pinto::OpenID::UserSuppliedID.new( + request.query('openid') + ) - config_key = Pinto::Type::ConfigKey.new('openid_providers') - providers = Pinto::Config.load(config_key) - unless providers.include? openid - http_status_code = Pinto::Type::HttpStatusCode.new(400) - translator = Pinto::Translator.new(request_lang) - message = translator._('Requested OpenID provider is not permitted') - message = Pinto::Type::ErrorMessage.new(message) - return Pinto::Controller::Private::Error.run( - request, http_status_code, message + unless Pinto::Config.valid_openid_provider? user_supplied_id + return Pinto::Controller::Error.run( + request, 400, Pinto::Translator.new(request.locale_code)._( + 'Requested OpenID provider is not permitted' + ) ) end - if request_lang.empty? - http_status_code = Pinto::Type::HttpStatusCode.new(400) - translator = Pinto::Translator.new(request_lang) - message = translator._('URI contains no valid language') - message = Pinto::Type::ErrorMessage.new(message) - return Pinto::Controller::Private::Error.run( - request, http_status_code, message + if request.no_locale? + return Pinto::Controller::Error.run( + request, 400, Pinto::Translator.new._( + 'URI contains no valid language' + ) ) end - user_supplied_id = Pinto::Type::UserSuppliedID.new(openid) - lang = Pinto::Locale.new(request_lang) - redirect_uri = Pinto::OpenID.begin(user_supplied_id, lang) - - return [ - 303, - {'Location' => redirect_uri, 'Content-Type' => ''}, - [] - ] + response = Pinto::HTTP::Response.new + response.status_code = 303 + response.location = + Pinto::OpenID.begin(user_supplied_id, request.locale_code) + return response end end end diff --git a/lib/pinto/controller/signup_openid.rb b/lib/pinto/controller/signup_openid.rb index 05e2b7f..1676fd9 100755 --- a/lib/pinto/controller/signup_openid.rb +++ b/lib/pinto/controller/signup_openid.rb @@ -1,44 +1,28 @@ -# lib/pinto/controller/signup_openid.rb module Pinto module Controller class SignupOpenid - include Pinto::Controller::Private::Base + include Pinto::Controller::Base + def get_action(request) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end + request = Pinto::HTTP::Request.new(request) - request_lang = request.get_uri_map.to_hash['lang'] - if request_lang.empty? - return Pinto::Controller::Private::Multiple.run(request) + if request.no_locale? + return Pinto::Controller::Multiple.run(request) end - base_lang = Pinto::Locale.new(request_lang) - other_languages = base_lang.others - - config_key = Pinto::Type::ConfigKey.new('openid_providers') - providers = Pinto::Config.load(config_key) - - param = { - :lang => request_lang, - :other_langs => other_languages, - :providers => providers - } - - view_name = Pinto::Type::ViewName.new('signup_openid') - view_param = Pinto::Type::ViewParam.new(param) - response_body = Pinto::View.render(view_name, view_param) - - platonic_uri = Pinto::Helper::URI.uri('signup_openid') + view = Pinto::View.new + view.name = Pinto::View::Name.new('signup_openid') + view.set_parameter(:locale_code, request.locale_code) + view.set_parameter(:other_locales, + Pinto::Locale.others(request.locale_code)) + view.set_parameter(:openid_providers, Pinto::Config.openid_providers) - return [ - 200, - { - 'Content-Type' => 'application/xhtml+xml; charset=UTF-8', - 'Content-Location' => platonic_uri - }, - [response_body] - ] + response = Pinto::HTTP::Response.new + response.status_code = 200 + response.content_type = 'application/xhtml+xml; charset=UTF-8' + response.content_location = Pinto::Helper::URI.expand('signup_openid') + response.body = view.render + return response end end end diff --git a/lib/pinto/controller/users.rb b/lib/pinto/controller/users.rb index d841268..28eca23 100755 --- a/lib/pinto/controller/users.rb +++ b/lib/pinto/controller/users.rb @@ -1,53 +1,23 @@ -# lib/pinto/controller/users.rb module Pinto module Controller class Users - include Pinto::Controller::Private::Base + include Pinto::Controller::Base + def post_action(request) - unless request.is_a? Pinto::Request - raise ArgumentError.new('request must be Pinto::Request') - end + request = Pinto::HTTP::Request.new(request) - request_lang = request.get_uri_map.to_hash['lang'] - user_name = request.POST['user_name'] - claimed_id = request.POST['claimed_id'] -=begin - begin - Pinto::Model::User.add(user_name, claimed_id) - rescue => e - base_lang = Pinto::Locale.new(request_lang) - other_languages = base_lang.others - param = { - :lang => request_lang, - :user_name => user_name, - :claimed_id => claimed_id, - :error_message => e.message - } - view_name = Pinto::Type::ViewName.new('signup_account') - view_param = Pinto::Type::ViewParam.new(param) - response_body = Pinto::View.render(view_name, view_param) - return [ - 400, - {'Content-Type' => 'application/xhtml+xml; charset=UTF-8'}, - [response_body] - ] - end -=end - param = { - 'lang' => request_lang, - 'user_name' => user_name - } - redirect_uri = Pinto::Helper::URI.uri('user', param) - return [ - 201, - {'Location' => redirect_uri, 'Content-Type' => ''}, - [] - ] + response = Pinto::HTTP::Response.new + response.status_code = 201 + response.location = Pinto::Helper::URI.expand( + 'user', 'locale_code' => request.locale_code, + 'user_name' => request.posted('user_name') + ) + return response end end end diff --git a/lib/pinto/core_ext/nil.rb b/lib/pinto/core_ext/nil.rb new file mode 100755 index 0000000..13e9c7c --- /dev/null +++ b/lib/pinto/core_ext/nil.rb @@ -0,0 +1,5 @@ +class NilClass + def to_hash + return {} + end +end diff --git a/lib/pinto/pathname.rb b/lib/pinto/core_ext/pathname.rb similarity index 90% rename from lib/pinto/pathname.rb rename to lib/pinto/core_ext/pathname.rb index d2bd54f..8a4c200 100755 --- a/lib/pinto/pathname.rb +++ b/lib/pinto/core_ext/pathname.rb @@ -1,4 +1,3 @@ -# lib/pinto/pathname.rb class Pathname def get_class self.to_classname.split('::').inject(Object) do |parent, child| diff --git a/lib/pinto/string.rb b/lib/pinto/core_ext/string.rb similarity index 77% rename from lib/pinto/string.rb rename to lib/pinto/core_ext/string.rb index 871bdfd..7affea6 100755 --- a/lib/pinto/string.rb +++ b/lib/pinto/core_ext/string.rb @@ -1,6 +1,5 @@ -# lib/pinto/string.rb class String def camelize self.split('_').map{|word| word.capitalize}.join end -end \ No newline at end of file +end diff --git a/lib/pinto/db/host.rb b/lib/pinto/db/host.rb index 2743f26..4e7ad7a 100755 --- a/lib/pinto/db/host.rb +++ b/lib/pinto/db/host.rb @@ -1,12 +1,12 @@ -# lib/pinto/db/host.rb module Pinto module DB class Host def initialize(host) - unless host.is_a? String - raise ArgumentError.new('host must be String') + unless host.respond_to? :to_s + raise TypeError.new('host must respond to #to_s') end - @host = host + + @host = host.to_s end def to_s diff --git a/lib/pinto/db/name.rb b/lib/pinto/db/name.rb index 3025e77..92fd994 100755 --- a/lib/pinto/db/name.rb +++ b/lib/pinto/db/name.rb @@ -1,12 +1,12 @@ -# lib/pinto/db/name.rb module Pinto module DB class Name def initialize(name) - unless name.is_a? String - raise ArgumentError.new('name must be String') + unless name.respond_to? :to_s + raise TypeError.new('name must respond to #to_s') end - @name = name + + @name = name.to_s end def to_s diff --git a/lib/pinto/db/password.rb b/lib/pinto/db/password.rb index 5788cee..f083030 100755 --- a/lib/pinto/db/password.rb +++ b/lib/pinto/db/password.rb @@ -1,17 +1,19 @@ -# lib/pinto/db/password.rb module Pinto module DB class Password def initialize(password) - unless password.is_a? String - raise ArgumentError.new('password must be String') + unless password.respond_to? :to_s + raise TypeError.new('password must respond to #to_s') end - @password = password + + @password = password.to_s end def to_s return @password end + + alias to_str to_s end end end diff --git a/lib/pinto/db/user.rb b/lib/pinto/db/user.rb index 9c6b20c..aec8eac 100755 --- a/lib/pinto/db/user.rb +++ b/lib/pinto/db/user.rb @@ -1,17 +1,19 @@ -# lib/pinto/db/user.rb module Pinto module DB class User def initialize(user) - unless user.is_a? String - raise ArgumentError.new('user must be String') + unless user.respond_to? :to_s + raise TypeError.new('user must respond to #to_s') end - @user = user + + @user = user.to_s end def to_s return @user end + + alias to_str to_s end end end diff --git a/lib/pinto/dispatcher.rb b/lib/pinto/dispatcher.rb index 57894e0..00219ea 100755 --- a/lib/pinto/dispatcher.rb +++ b/lib/pinto/dispatcher.rb @@ -1,45 +1,14 @@ -# lib/pinto/dispatcher.rb module Pinto class Dispatcher def call(env) - unless env.is_a? Hash - raise ArgumentError.new('env must be Hash') - end - request = Pinto::Request.new(env) - uri = Addressable::URI.parse(request.url) - - config_key = Pinto::Type::ConfigKey.new('uri_templates') - Pinto::Config.load(config_key).each do |controller, template| - uri_map = uri.extract_mapping(template, Pinto::URI::ExtractProcessor) - unless uri_map.nil? - controller_name = Pinto::Type::ControllerName.new(controller) - request.set_controller_name(controller_name) + env = Pinto::Server::Environment.new(env) - uri_map = Pinto::Type::UriMap.new(uri_map) - request.set_uri_map(uri_map) - break - end + request = Pinto::HTTP::Request.new(env) + if request.not_found? + return Pinto::Controller::Error.run(request, 404).for_rack end - if request.get_controller_name.to_s.empty? - http_status_code = Pinto::Type::HttpStatusCode.new(404) - return Pinto::Controller::Private::Error.run(request, http_status_code) - end - - controller_name = request.get_controller_name.to_s - path = Pathname.new("pinto/controller/#{controller_name}") - return path.get_class.new.run(request) - -=begin - rescue => exception - http_status_code = Pinto::Type::HttpStatusCode.new(500) - translator = Pinto::Translater.new(request.get_uri_map['lang']) -# message = translator._('Server error occurred') - message = Pinto::Type::ErrorMessage.new(exception.message) - return Pinto::Controller::Private::Error.run( - request, http_status_code, message - ) -=end + return request.run.for_rack end end end diff --git a/lib/pinto/encoding/utf8.rb b/lib/pinto/encoding/utf8.rb index 7c14f97..8e8e33b 100755 --- a/lib/pinto/encoding/utf8.rb +++ b/lib/pinto/encoding/utf8.rb @@ -1,11 +1,9 @@ -# lib/pinto/encoding/utf8.rb module Pinto module Encoding class UTF8 def self.valid?(value) - unless value.is_a? String - raise ArgumentError.new('value must be String') - end + value = value.to_s + valid_utf8 = /^(?: [\x00-\x7F] # U+0000 - U+007F |[\xC2-\xDF][\x80-\xBF] # U+0080 - U+07FF diff --git a/lib/pinto/error/message.rb b/lib/pinto/error/message.rb new file mode 100755 index 0000000..b7319a4 --- /dev/null +++ b/lib/pinto/error/message.rb @@ -0,0 +1,17 @@ +module Pinto + module Error + class Message + def initialize(error_message) + unless error_message.respond_to? :to_s + raise TypeError.new('error_message must respond to #to_s') + end + + @error_message = error_message.to_s + end + + def to_s + return @error_message + end + end + end +end diff --git a/lib/pinto/helper/html/escaper.rb b/lib/pinto/helper/html/escaper.rb index 0e04c89..4a24813 100755 --- a/lib/pinto/helper/html/escaper.rb +++ b/lib/pinto/helper/html/escaper.rb @@ -1,32 +1,31 @@ -# lib/pinto/helper/html/escaper.rb module Pinto module Helper module HTML module Escaper def escape_value(value) - unless value.is_a? String - raise ArgumentError.new('value must be String') + unless value.respond_to? 'to_s' + raise ArgumentError.new('value must respond to to_s') end - escape_chars = Pinto::Type::EscapeChars.new(['&', '<', '>']) - return eliminate(value, escape_chars) + escape_chars = Pinto::Characters.new(['&', '<', '>']) + return eliminate(value.to_s, escape_chars) end def escape_attribute(value) - unless value.is_a? String - raise ArgumentError.new('value must be String') + unless value.respond_to? 'to_s' + raise ArgumentError.new('value must respond to to_s') end - escape_chars = Pinto::Type::EscapeChars.new(['&', '<', '>', '"']) - return eliminate(value, escape_chars) + escape_chars = Pinto::Characters.new(['&', '<', '>', '"']) + return eliminate(value.to_s, escape_chars) end def escape_tag(value) - unless value.is_a? String - raise ArgumentError.new('value must be String') + unless value.respond_to? 'to_s' + raise ArgumentError.new('value must respond to to_s') end - return eliminate(value) + return eliminate(value.to_s) end private @@ -38,16 +37,14 @@ def escape_tag(value) '"' => '"' } - def eliminate(value, escape_chars = Pinto::Type::EscapeChars.new([])) - unless value.is_a? String - raise ArgumentError.new('value must be String') - end - unless escape_chars.is_a? Pinto::Type::EscapeChars - raise ArgumentError.new( - 'escape_chars must be Pinto::Type::EscapeChars' - ) + def eliminate(value, escape_chars = []) + unless value.respond_to? 'to_s' + raise TypeError.new('value must respond to #to_s') end + value = value.to_s + escape_chars = Pinto::Characters.new(escape_chars) + unless Pinto::Encoding::UTF8.valid? value raise ArgumentError.new('value is not UTF-8 string') end diff --git a/lib/pinto/helper/translator.rb b/lib/pinto/helper/translator.rb index 53ea676..e81f0bd 100755 --- a/lib/pinto/helper/translator.rb +++ b/lib/pinto/helper/translator.rb @@ -1,30 +1,23 @@ -# lib/pinto/view/translator.rb module Pinto module Helper module Translator - def locale=(locale) - unless locale.is_a? Pinto::Locale - raise ArgumentError.new('locale must be Pinto::Locale') - end - - @locale = locale + def locale_code=(locale_code) + @locale_code = Pinto::Locale::Code.new(locale_code) end - def locale - return @locale + def locale_code + return @locale_code end def _(message_id) - unless message_id.is_a? String - raise ArgumentError.new('message_id must be String') - end + message_id = Pinto::Translate::MessageID.new(message_id) - @locale = Pinto::Locale.new('en') if @locale.nil? + self.locale_code = 'en' if self.locale_code.nil? GetText.set_output_charset('UTF-8') - GetText.bindtextdomain('pinto', {:path => 'locale'}) - GetText.set_locale(@locale.to_s) - return GetText._(message_id) + GetText.bindtextdomain('pinto', :path => 'locale') + GetText.set_locale(self.locale_code.to_s) + return GetText._(message_id.to_s) end end end diff --git a/lib/pinto/helper/uri.rb b/lib/pinto/helper/uri.rb index 9c77269..74a8e44 100755 --- a/lib/pinto/helper/uri.rb +++ b/lib/pinto/helper/uri.rb @@ -1,26 +1,21 @@ -# lib/pinto/helper/uri.rb module Pinto module Helper module URI module_function - def uri(controller_name, param = {}) - unless controller_name.is_a? String - raise ArgumentError.new('controller_name must be String') - end - unless param.is_a? Hash - raise ArgumentError.new('param must be Hash') - end + def expand(controller_name, uri_parameters = {}) + controller_name = Pinto::Controller::Name.new(controller_name) + uri_parameters = Pinto::URI::Parameters.new(uri_parameters) - config_key = Pinto::Type::ConfigKey.new('uri_templates') - uri_templates = Pinto::Config.load(config_key) - - return Addressable::URI.expand_template( - uri_templates[controller_name], - param, + uri_templates = Pinto::Config.uri_templates + return Pinto::URI.new(Addressable::URI.expand_template( + uri_templates.template(controller_name).to_s, + uri_parameters.to_hash, Pinto::URI::ExpandProcessor - ).to_s + )) end + + alias uri expand end end end diff --git a/lib/pinto/http/request.rb b/lib/pinto/http/request.rb new file mode 100755 index 0000000..dae1e7d --- /dev/null +++ b/lib/pinto/http/request.rb @@ -0,0 +1,106 @@ +module Pinto + module HTTP + class Request + def initialize(env) + env = Pinto::Server::Environment.new(env) + + self.env = env + self.controller_name = '' + self.uri_parameters = {} + + Pinto::Config.uri_templates.each do |uri_templates_record| + uri_parameters = self.uri.extract_mapping( + uri_templates_record.uri_template + ) + + unless uri_parameters.empty? + self.controller_name = uri_templates_record.controller_name + self.uri_parameters = uri_parameters + break + end + end + end + + def env=(env) + @env = Pinto::Server::Environment.new(env) + end + + def env + return @env + end + + def to_hash + return @env.to_hash + end + + def controller_name=(controller_name) + @controller_name = Pinto::Controller::Name.new(controller_name) + end + + def controller_name + return @controller_name + end + + def uri_parameters=(uri_parameters) + @uri_parameters = Pinto::URI::Parameters.new(uri_parameters) + end + + def uri_parameters + return @uri_parameters + end + + def uri + return Pinto::URI.new(Addressable::URI.parse(self.request.url)) + end + + def request + return Rack::Request.new(self.env.to_hash) + end + + def not_found? + return (self.controller_name == '') + end + + def no_locale? + return self.locale_code.empty? + end + + def locale_code + return self.uri_parameters.locale_code + end + + def get? + return self.request.get? + end + + def post? + return self.request.post? + end + + def head? + return (self.request.request_method == 'HEAD') + end + + def options? + return (self.request.request_method == 'OPTIONS') + end + + def query(key) + return self.request.GET[key.to_s] + end + + def posted(key) + return self.request.POST[key.to_s] + end + + def run + return Pathname.new("pinto/controller/#{self.controller_name}" + ).get_class.new.run(self) + end + + def query_strings + return Pinto::URI::QueryStrings.new(self.request.GET) + end + end + end +end diff --git a/lib/pinto/http/response.rb b/lib/pinto/http/response.rb new file mode 100755 index 0000000..b0061c8 --- /dev/null +++ b/lib/pinto/http/response.rb @@ -0,0 +1,67 @@ +module Pinto + module HTTP + class Response + def initialize + @headers = {'Content-Type' => ''} + end + + def status_code=(status_code) + @status_code = Pinto::HTTP::StatusCode.new(status_code) + end + + def status_code + return @status_code + end + + def content_type=(content_type) + @headers['Content-Type'] = content_type.to_s + end + + def content_type + return @headers['Content-Type'] + end + + def location=(location) + @headers['Location'] = location.to_s + end + + def location + return @headers['Location'] + end + + def content_location=(content_location) + @headers['Content-Location'] = Pinto::URI.new(content_location) + end + + def content_location + return @headers['Content-Location'] + end + + def body=(response_body) + @response_body = Pinto::HTTP::Response::Body.new(response_body) + end + + def body + @response_body + end + + def headers + return @headers + end + + def for_rack + headers = {} + + self.headers.each do |key, value| + headers[key] = value.to_s + end + + return [ + self.status_code.to_i, + headers, + self.body.to_s + ] + end + end + end +end diff --git a/lib/pinto/http/response/body.rb b/lib/pinto/http/response/body.rb new file mode 100755 index 0000000..2ceb445 --- /dev/null +++ b/lib/pinto/http/response/body.rb @@ -0,0 +1,19 @@ +module Pinto + module HTTP + class Response + class Body + def initialize(response_body) + unless response_body.respond_to? :to_s + raise TypeError.new('response_body must respond to #to_s') + end + + @response_body = response_body.to_s + end + + def to_s + return @response_body + end + end + end + end +end diff --git a/lib/pinto/http/status_code.rb b/lib/pinto/http/status_code.rb new file mode 100755 index 0000000..8fbde2c --- /dev/null +++ b/lib/pinto/http/status_code.rb @@ -0,0 +1,13 @@ +module Pinto + module HTTP + class StatusCode + def initialize(status_code) + @status_code = status_code.to_i + end + + def to_i + return @status_code + end + end + end +end diff --git a/lib/pinto/locale.rb b/lib/pinto/locale.rb index 280bfa7..5da952f 100755 --- a/lib/pinto/locale.rb +++ b/lib/pinto/locale.rb @@ -1,47 +1,49 @@ -# lib/pinto/locale.rb module Pinto class Locale - LIST = ['en', 'ja'] - def self.list - return LIST.map do |locale| - Pinto::Locale.new(locale) - end + translator = Pinto::Translator.new + return [ + Pinto::Locale.new(Pinto::Locale::Code.new('en'), + translator._('English')), + Pinto::Locale.new(Pinto::Locale::Code.new('ja'), + translator._('Japanese')), + ] end - def initialize(locale) - unless locale.is_a? String - raise ArgumentError.new('locale must be String') - end - - @locale = locale + def initialize(code, name) + self.code = code + self.name = name end - def others + def self.others(locale_code) + locale_code = Pinto::Locale::Code.new(locale_code) return Pinto::Locale.list.delete_if do |locale| - locale == self + locale.code == locale_code end end - def to_s - return @locale + def code=(code) + @code = Pinto::Locale::Code.new(code) end - def name(translator) - unless translator.respond_to? '_' - raise ArgumentError.new('translator must have "_" method') - end + def code + return @code + end - case self.to_s - when 'en' - return translator._('English') - when 'ja' - return translator._('Japanese') + def name=(name) + unless name.respond_to? :to_s + raise TypeError.new('name must respond to #to_s') end + + @name = name.to_s + end + + def name + return @name end def ==(other) - return (self.to_s == other.to_s) + return (self.code == other.code && self.name == other.name) end end end diff --git a/lib/pinto/locale/code.rb b/lib/pinto/locale/code.rb new file mode 100755 index 0000000..47536e3 --- /dev/null +++ b/lib/pinto/locale/code.rb @@ -0,0 +1,22 @@ +module Pinto + class Locale + class Code + def initialize(code) + unless code.respond_to? :to_s + raise TypeError.new('code must respond to #to_s') + end + + @code = code.to_s + end + + def to_s + return @code + end + + def ==(other) + other = Pinto::Locale::Code.new(other) + return (self.to_s == other.to_s) + end + end + end +end diff --git a/lib/pinto/model/signup_reservation.rb b/lib/pinto/model/signup_reservation.rb index d02a0ad..b7d9591 100755 --- a/lib/pinto/model/signup_reservation.rb +++ b/lib/pinto/model/signup_reservation.rb @@ -1,18 +1,14 @@ -# lib/pinto/model/signup_reservation.rb module Pinto module Model class SignupReservation def self.add(claimed_id) - unless claimed_id.is_a? Pinto::Type::ClaimedID - raise ArgumentError.new('claimed_id must be Pinto::Type::ClaimedID') - end + claimed_id = Pinto::OpenID::ClaimedID.new(claimed_id) + db_config = Pinto::Config::DB.load begin - dsn = 'dbi:Mysql:%s:%s' % [db_config.name.to_s, db_config.host.to_s] - dbh = DBI.connect(dsn, - db_config.user.to_s, - db_config.password.to_s) + dsn = 'dbi:Mysql:%s:%s' % [db_config.name, db_config.host] + dbh = DBI.connect(dsn, db_config.user, db_config.password) dbh.do('INSERT INTO signup_reservations (claimed_id, reserved_at) ' + 'VALUES (?, NOW()) ' + 'ON DUPLICATE KEY UPDATE reserved_at = NOW()', diff --git a/lib/pinto/model/user.rb b/lib/pinto/model/user.rb index 625335b..fc6d93b 100755 --- a/lib/pinto/model/user.rb +++ b/lib/pinto/model/user.rb @@ -1,13 +1,12 @@ -# lib/pinto/model/user.rb module Pinto module Model class User def self.add(user_name, claimed_id) - unless user_name.is_a? Pinto::Type::UserName - raise ArgumentError.new('user_name must be Pinto::Type::UserName') + unless user_name.is_a? Pinto::User::Name + raise TypeError.new('user_name must be Pinto::User::Name') end - unless claimed_id.is_a? Pinto::Type::ClaimedID - raise ArgumentError.new('claimed_id must be Pinto::Type::ClaimedID') + unless claimed_id.is_a? Pinto::OpenID::ClaimedID + raise TypeError.new('claimed_id must be Pinto::OpenID::ClaimedID') end end =begin @@ -18,7 +17,7 @@ def self.add(user_name, open_id, lang) message = translator._('%{param} is required') % { :param => translator._('User name') } - raise ArgumentError.new(message) + raise TypeError.new(message) end open_id = open_id.to_s @@ -27,7 +26,7 @@ def self.add(user_name, open_id, lang) message = translator._('%{param} is required') % { :param => 'OpenID' } - raise ArgumentError.new(message) + raise TypeError.new(message) end end =end diff --git a/lib/pinto/open_id.rb b/lib/pinto/open_id.rb index 777819b..b16d891 100755 --- a/lib/pinto/open_id.rb +++ b/lib/pinto/open_id.rb @@ -1,34 +1,26 @@ -# lib/pinto/open_id.rb module Pinto class OpenID - def self.begin(user_supplied_id, lang) - unless user_supplied_id.is_a? Pinto::Type::UserSuppliedID - raise ArgumentError.new( - 'user_supplied_id must be Pinto::Type::UserSuppliedID' - ) - end - unless lang.is_a? Pinto::Locale - raise ArgumentError.new('lang must be Pinto::Locale') - end + def self.begin(user_supplied_id, locale_code) + user_supplied_id = Pinto::OpenID::UserSuppliedID.new(user_supplied_id) + locale_code = Pinto::Locale::Code.new(locale_code) + request = self.consumer.begin(user_supplied_id.to_s) - realm = Pinto::Helper::URI.uri('index', 'lang' => lang.to_s) - return_to = Pinto::Helper::URI.uri('signup_account', 'lang' => lang.to_s) + realm = Pinto::Helper::URI.expand('index', + 'locale_code' => locale_code) + return_to = Pinto::Helper::URI.expand('signup_account', + 'locale_code' => locale_code) - return request.redirect_url(realm, return_to) + return request.redirect_url(realm.to_s, return_to.to_s) end - def self.complete(query, uri) - unless query.is_a? Pinto::Type::QueryStrings - raise ArgumentError.new('query must be Pinto::Type::QueryStrings') - end - unless uri.is_a? Pinto::Type::URI - raise ArgumentError.new('uri must be Pinto::Type::URI') - end + def self.complete(query_strings, uri) + query_strings = Pinto::URI::QueryStrings.new(query_strings) + uri = Pinto::URI.new(uri) - response = self.consumer.complete(query.to_hash, uri.to_s) + response = self.consumer.complete(query_strings.to_hash, uri.to_s) unless response.is_a? ::OpenID::Consumer::SuccessResponse - return nil + raise RuntimeError.new(response.message) end return response.identity_url end diff --git a/lib/pinto/open_id/claimed_id.rb b/lib/pinto/open_id/claimed_id.rb new file mode 100755 index 0000000..29579a4 --- /dev/null +++ b/lib/pinto/open_id/claimed_id.rb @@ -0,0 +1,13 @@ +module Pinto + class OpenID + class ClaimedID + def initialize(claimed_id) + @claimed_id = claimed_id.to_s + end + + def to_s + return @claimed_id + end + end + end +end diff --git a/lib/pinto/open_id/provider.rb b/lib/pinto/open_id/provider.rb new file mode 100755 index 0000000..06e4a98 --- /dev/null +++ b/lib/pinto/open_id/provider.rb @@ -0,0 +1,19 @@ +module Pinto + class OpenID + class Provider + def initialize(openid_provider) + @openid_provider = openid_provider.to_s + end + + def to_s + return @openid_provider + end + + def ==(other) + other = Pinto::OpenID::Provider.new(other) + + return (self.to_s == other.to_s) + end + end + end +end diff --git a/lib/pinto/open_id/providers.rb b/lib/pinto/open_id/providers.rb new file mode 100755 index 0000000..5e0b8fc --- /dev/null +++ b/lib/pinto/open_id/providers.rb @@ -0,0 +1,33 @@ +module Pinto + class OpenID + class Providers + def initialize + @openid_providers = [] + end + + def add(openid_provider) + openid_provider = Pinto::OpenID::Provider.new(openid_provider) + + @openid_providers.push( + Pinto::OpenID::Provider.new(openid_provider) + ) + end + + def to_a + return @openid_providers + end + + def each + @openid_providers.each do |item| + yield item + end + end + + def include?(openid_provider) + openid_provider = Pinto::OpenID::Provider.new(openid_provider) + + return @openid_providers.include? openid_provider + end + end + end +end diff --git a/lib/pinto/open_id/user_supplied_id.rb b/lib/pinto/open_id/user_supplied_id.rb new file mode 100755 index 0000000..9f41e02 --- /dev/null +++ b/lib/pinto/open_id/user_supplied_id.rb @@ -0,0 +1,17 @@ +module Pinto + class OpenID + class UserSuppliedID + def initialize(user_supplied_id) + unless user_supplied_id.respond_to? :to_s + raise TypeError.new('user_supplied_id must respond to #to_s') + end + + @user_supplied_id = user_supplied_id.to_s + end + + def to_s + return @user_supplied_id + end + end + end +end diff --git a/lib/pinto/request.rb b/lib/pinto/request.rb deleted file mode 100755 index 5043d87..0000000 --- a/lib/pinto/request.rb +++ /dev/null @@ -1,51 +0,0 @@ -# lib/pinto/request.rb -module Pinto - class Request < Rack::Request - def initialize(env) - unless env.is_a? Hash - raise ArgumentError.new('env must be Hash') - end - controller_name = Pinto::Type::ControllerName.new('') - self.set_controller_name(controller_name) - - uri_map = Pinto::Type::UriMap.new({}) - self.set_uri_map(uri_map) - - super(env) - end - - def set_controller_name(controller_name) - unless controller_name.is_a? Pinto::Type::ControllerName - raise ArgumentError.new( - 'controller_name must be Pinto::Type::ControllerName' - ) - end - - @controller_name = controller_name - end - - def get_controller_name - return @controller_name - end - - def set_uri_map(uri_map) - unless uri_map.is_a? Pinto::Type::UriMap - raise ArgumentError.new('uri_map must be Pinto::Type::UriMap') - end - - @uri_map = uri_map - end - - def get_uri_map - return @uri_map - end - - def head? - return (request_method == 'HEAD') - end - - def options? - return (request_method == 'OPTIONS') - end - end -end diff --git a/lib/pinto/server/environment.rb b/lib/pinto/server/environment.rb new file mode 100755 index 0000000..93b4a22 --- /dev/null +++ b/lib/pinto/server/environment.rb @@ -0,0 +1,13 @@ +module Pinto + module Server + class Environment + def initialize(server_environment) + @server_environment = server_environment.to_hash + end + + def to_hash + return @server_environment + end + end + end +end diff --git a/lib/pinto/translate/message_id.rb b/lib/pinto/translate/message_id.rb new file mode 100755 index 0000000..21a97f7 --- /dev/null +++ b/lib/pinto/translate/message_id.rb @@ -0,0 +1,13 @@ +module Pinto + module Translate + class MessageID + def initialize(message_id) + @message_id = message_id.to_s + end + + def to_s + return @message_id + end + end + end +end diff --git a/lib/pinto/translator.rb b/lib/pinto/translator.rb index 7b9a7e2..967731e 100755 --- a/lib/pinto/translator.rb +++ b/lib/pinto/translator.rb @@ -1,22 +1,17 @@ -# lib/pinto/translator.rb module Pinto class Translator - def initialize(locale) - unless locale.is_a? Pinto::Locale - raise ArgumentError.new('locale must be Pinto::Locale') - end + def initialize(locale_code = 'en') + locale_code = Pinto::Locale::Code.new(locale_code) @translator = Class.new @translator.extend(Pinto::Helper::Translator) - @translator.locale = locale + @translator.locale_code = locale_code end def _(message_id) - unless message_id.is_a? String - raise ArgumentError.new('message_id must be String') - end + message_id = Pinto::Translate::MessageID.new(message_id) - return @translator._(message_id) + return @translator._(message_id.to_s) end end end diff --git a/lib/pinto/type/claimed_id.rb b/lib/pinto/type/claimed_id.rb deleted file mode 100755 index 343832a..0000000 --- a/lib/pinto/type/claimed_id.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/claimed_id.rb -module Pinto - module Type - class ClaimedID - def initialize(claimed_id) - unless claimed_id.is_a? String - raise ArgumentError.new('claimed_id must be String') - end - @code = claimed_id - end - - def to_s - return @code - end - end - end -end diff --git a/lib/pinto/type/config_key.rb b/lib/pinto/type/config_key.rb deleted file mode 100755 index 1a79cd1..0000000 --- a/lib/pinto/type/config_key.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/config_key.rb -module Pinto - module Type - class ConfigKey - def initialize(config_key) - unless config_key.is_a? String - raise ArgumentError.new('config_key must be String') - end - @config_key = config_key - end - - def to_s - return @config_key - end - end - end -end diff --git a/lib/pinto/type/controller_name.rb b/lib/pinto/type/controller_name.rb deleted file mode 100755 index 297e4cb..0000000 --- a/lib/pinto/type/controller_name.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/controller_name.rb -module Pinto - module Type - class ControllerName - def initialize(controller_name) - unless controller_name.is_a? String - raise ArgumentError.new('controller_name must be String') - end - @controller_name = controller_name - end - - def to_s - return @controller_name - end - end - end -end diff --git a/lib/pinto/type/error_message.rb b/lib/pinto/type/error_message.rb deleted file mode 100755 index 715b6c9..0000000 --- a/lib/pinto/type/error_message.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/error_message.rb -module Pinto - module Type - class ErrorMessage - def initialize(error_message) - unless error_message.is_a? String - raise ArgumentError.new('error_message must be String') - end - @error_message = error_message - end - - def to_s - return @error_message - end - end - end -end diff --git a/lib/pinto/type/escape_chars.rb b/lib/pinto/type/escape_chars.rb deleted file mode 100755 index 4ba86cd..0000000 --- a/lib/pinto/type/escape_chars.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/escape_chars.rb -module Pinto - module Type - class EscapeChars - def initialize(escape_chars) - unless escape_chars.is_a? Array - raise ArgumentError.new('escape_chars must be Array') - end - @escape_chars = escape_chars - end - - def to_a - return @escape_chars - end - end - end -end diff --git a/lib/pinto/type/http_status_code.rb b/lib/pinto/type/http_status_code.rb deleted file mode 100755 index 8076eff..0000000 --- a/lib/pinto/type/http_status_code.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/http_status_code.rb -module Pinto - module Type - class HttpStatusCode - def initialize(http_status_code) - unless http_status_code.is_a? Integer - raise ArgumentError.new('http_status_code must be Integer') - end - @http_status_code = http_status_code - end - - def to_i - return @http_status_code - end - end - end -end diff --git a/lib/pinto/type/query_strings.rb b/lib/pinto/type/query_strings.rb deleted file mode 100755 index a61de84..0000000 --- a/lib/pinto/type/query_strings.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/query_strings.rb -module Pinto - module Type - class QueryStrings - def initialize(query_strings) - unless query_strings.is_a? Hash - raise ArgumentError.new('query_strings must be Hash') - end - @query_strings = query_strings - end - - def to_hash - return @query_strings - end - end - end -end diff --git a/lib/pinto/type/uri.rb b/lib/pinto/type/uri.rb deleted file mode 100755 index c4558a4..0000000 --- a/lib/pinto/type/uri.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/uri.rb -module Pinto - module Type - class URI - def initialize(uri) - unless uri.is_a? String - raise ArgumentError.new('uri must be String') - end - @uri = uri - end - - def to_s - return @uri - end - end - end -end diff --git a/lib/pinto/type/uri_map.rb b/lib/pinto/type/uri_map.rb deleted file mode 100755 index 1d3e3c6..0000000 --- a/lib/pinto/type/uri_map.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/uri_map.rb -module Pinto - module Type - class UriMap - def initialize(uri_map) - unless uri_map.is_a? Hash - raise ArgumentError.new('uri_map must be Hash') - end - @uri_map = uri_map - end - - def to_hash - return @uri_map - end - end - end -end diff --git a/lib/pinto/type/user_name.rb b/lib/pinto/type/user_name.rb deleted file mode 100755 index 50fc34c..0000000 --- a/lib/pinto/type/user_name.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/user_name.rb -module Pinto - module Type - class UserName - def initialize(user_name) - unless user_name.is_a? String - raise ArgumentError.new('user_name must be String') - end - @user_name = user_name - end - - def to_s - return @user_name - end - end - end -end diff --git a/lib/pinto/type/user_supplied_id.rb b/lib/pinto/type/user_supplied_id.rb deleted file mode 100755 index 9138207..0000000 --- a/lib/pinto/type/user_supplied_id.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/user_supplied_id.rb -module Pinto - module Type - class UserSuppliedID - def initialize(user_supplied_id) - unless user_supplied_id.is_a? String - raise ArgumentError.new('user_supplied_id must be String') - end - @user_supplied_id = user_supplied_id - end - - def to_s - return @user_supplied_id - end - end - end -end diff --git a/lib/pinto/type/view_name.rb b/lib/pinto/type/view_name.rb deleted file mode 100755 index 77559aa..0000000 --- a/lib/pinto/type/view_name.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/view_name.rb -module Pinto - module Type - class ViewName - def initialize(view_name) - unless view_name.is_a? String - raise ArgumentError.new('view_name must be String') - end - @view_name = view_name - end - - def to_s - return @view_name - end - end - end -end diff --git a/lib/pinto/type/view_param.rb b/lib/pinto/type/view_param.rb deleted file mode 100755 index fb0eee1..0000000 --- a/lib/pinto/type/view_param.rb +++ /dev/null @@ -1,17 +0,0 @@ -# lib/pinto/type/view_param.rb -module Pinto - module Type - class ViewParam - def initialize(view_param) - unless view_param.is_a? Hash - raise ArgumentError.new('view_param must be Hash') - end - @view_param = view_param - end - - def to_hash - return @view_param - end - end - end -end diff --git a/lib/pinto/uri.rb b/lib/pinto/uri.rb new file mode 100755 index 0000000..91154ce --- /dev/null +++ b/lib/pinto/uri.rb @@ -0,0 +1,20 @@ +module Pinto + class URI + def initialize(uri) + @uri = uri.to_s + end + + def extract_mapping(uri_template) + uri_template = Pinto::URI::Template.new(uri_template) + + uri_parameters = Addressable::URI.parse(@uri).extract_mapping( + uri_template.to_s, Pinto::URI::ExtractProcessor + ) + return Pinto::URI::Parameters.new(uri_parameters) + end + + def to_s + return @uri + end + end +end diff --git a/lib/pinto/uri/expand_processor.rb b/lib/pinto/uri/expand_processor.rb index 10b58cb..51eb771 100755 --- a/lib/pinto/uri/expand_processor.rb +++ b/lib/pinto/uri/expand_processor.rb @@ -1,16 +1,17 @@ -# lib/pinto/uri/expand_processor.rb module Pinto - module URI + class URI class ExpandProcessor def self.transform(name, value) unless name.is_a? String - raise ArgumentError.new('name must be String') + raise TypeError.new('name must be String') end - unless value.is_a? String - raise ArgumentError.new('value must be String') + unless value.respond_to? 'to_s' + raise TypeError.new('value must respond to to_s') end - return value + '.' if name == 'lang' - return '?' + value if name == 'query' + + value = value.to_s + return value + '.' if name == 'locale_code' + return '?' + value if name == 'query_string' return value end end diff --git a/lib/pinto/uri/extract_processor.rb b/lib/pinto/uri/extract_processor.rb index 43d9d9f..4f7bd36 100755 --- a/lib/pinto/uri/extract_processor.rb +++ b/lib/pinto/uri/extract_processor.rb @@ -1,29 +1,29 @@ -# lib/pinto/uri/extract_processor.rb module Pinto - module URI + class URI class ExtractProcessor def self.match(name) unless name.is_a? String - raise ArgumentError.new('name must be String') + raise TypeError.new('name must be String') end - return 'ja\.|en\.|' if name == 'lang' - return '\?.+|' if name == 'query' - return '[0-9a-zA-Z_\-]+' if name == 'username' + + return 'ja\.|en\.|' if name == 'locale_code' + return '\?.+|' if name == 'query_string' + return '[0-9a-zA-Z_\-]+' if name == 'user_name' return '.*' end def self.restore(name, value) unless name.is_a? String - raise ArgumentError.new('name must be String') + raise TypeError.new('name must be String') end unless value.is_a? String - raise ArgumentError.new('value must be String') + raise TypeError.new('value must be String') end case name - when 'lang' + when 'locale_code' restore_value = value.gsub(/\.$/, '') - when 'query' + when 'query_string' restore_value = value.gsub(/^\?/, '') else restore_value = value diff --git a/lib/pinto/uri/parameters.rb b/lib/pinto/uri/parameters.rb new file mode 100755 index 0000000..f750f03 --- /dev/null +++ b/lib/pinto/uri/parameters.rb @@ -0,0 +1,25 @@ +module Pinto + class URI + class Parameters + def initialize(uri_parameters) + @uri_parameters = uri_parameters.to_hash + end + + def to_hash + return @uri_parameters + end + + def empty? + return @uri_parameters.empty? + end + + def locale_code=(locale_code) + @uri_parameters['locale_code'] = Pinto::Locale::Code.new(locale_code) + end + + def locale_code + return @uri_parameters['locale_code'] + end + end + end +end diff --git a/lib/pinto/uri/query_strings.rb b/lib/pinto/uri/query_strings.rb new file mode 100755 index 0000000..e9363b0 --- /dev/null +++ b/lib/pinto/uri/query_strings.rb @@ -0,0 +1,13 @@ +module Pinto + class URI + class QueryStrings + def initialize(query_strings) + @query_strings = query_strings.to_hash + end + + def to_hash + return @query_strings + end + end + end +end diff --git a/lib/pinto/uri/template.rb b/lib/pinto/uri/template.rb new file mode 100755 index 0000000..792ede0 --- /dev/null +++ b/lib/pinto/uri/template.rb @@ -0,0 +1,17 @@ +module Pinto + class URI + class Template + def initialize(uri_template) + unless uri_template.respond_to? :to_s + raise TypeError.new('uri_template must respond to #to_s') + end + + @uri_template = uri_template.to_s + end + + def to_s + return @uri_template + end + end + end +end diff --git a/lib/pinto/uri/templates.rb b/lib/pinto/uri/templates.rb new file mode 100755 index 0000000..593f6a6 --- /dev/null +++ b/lib/pinto/uri/templates.rb @@ -0,0 +1,38 @@ +module Pinto + class URI + class Templates + def initialize + @uri_templates = [] + end + + def add(controller_name, uri_template) + controller_name = Pinto::Controller::Name.new(controller_name) + uri_template = Pinto::URI::Template.new(uri_template) + + @uri_templates.push( + Pinto::URI::Templates::Record.new(controller_name, uri_template) + ) + end + + def to_a + return @uri_templates + end + + def each + @uri_templates.each do |item| + yield item + end + end + + def template(controller_name) + controller_name = Pinto::Controller::Name.new(controller_name) + + self.each do |uri_templates_record| + if uri_templates_record.controller_name == controller_name + return uri_templates_record.uri_template + end + end + end + end + end +end diff --git a/lib/pinto/uri/templates/record.rb b/lib/pinto/uri/templates/record.rb new file mode 100755 index 0000000..4fc81b2 --- /dev/null +++ b/lib/pinto/uri/templates/record.rb @@ -0,0 +1,23 @@ +module Pinto + class URI + class Templates + class Record + def initialize(controller_name, uri_template) + controller_name = Pinto::Controller::Name.new(controller_name) + uri_template = Pinto::URI::Template.new(uri_template) + + @controller_name = controller_name + @uri_template = uri_template + end + + def controller_name + return @controller_name + end + + def uri_template + return @uri_template + end + end + end + end +end diff --git a/lib/pinto/view.rb b/lib/pinto/view.rb index a8a4242..5dc7ed3 100755 --- a/lib/pinto/view.rb +++ b/lib/pinto/view.rb @@ -1,15 +1,34 @@ -# lib/pinto/view.rb module Pinto class View - def self.render(name, param = Pinto::Type::ViewParam.new({})) - unless name.is_a? Pinto::Type::ViewName - raise ArgumentError.new('name must be Pinto::Type::ViewName') - end - unless param.is_a? Pinto::Type::ViewParam - raise ArgumentError.new('param must be Pinto::Type::ViewParam') + def initialize + @parameters = Pinto::View::Parameters.new + end + + def set_parameter(symbol, value) + unless symbol.is_a? Symbol + raise TypeError.new('symbol must be Symbol') end - template = File.read("view/#{name.to_s}.erb") - return Pinto::View::XHTML.new(template).evaluate(param) + + @parameters.set(symbol, value) + end + + def parameters + return @parameters + end + + def name=(name) + @name = Pinto::View::Name.new(name) + end + + def name + return @name + end + + def render + template = File.read("view/#{self.name.to_s}.erb") + return Pinto::HTTP::Response::Body.new( + Pinto::View::XHTML.new(template).evaluate(self.parameters) + ) end end end diff --git a/lib/pinto/view/context.rb b/lib/pinto/view/context.rb index 5bd4795..19f25c3 100755 --- a/lib/pinto/view/context.rb +++ b/lib/pinto/view/context.rb @@ -1,4 +1,3 @@ -# lib/pinto/view/context.rb module Pinto class View class Context < Erubis::Context @@ -10,12 +9,14 @@ class Context < Erubis::Context alias attr escape_attribute alias tag escape_tag - def initialize(param) - unless param.is_a? Pinto::Type::ViewParam - raise ArgumentError.new('param must be Pinto::Type::ViewParam') + def initialize(view_parameters) + unless view_parameters.is_a? Pinto::View::Parameters + raise TypeError.new( + 'view_parameters must be Pinto::View::Parameters' + ) end - super(param.to_hash) + super(view_parameters.to_hash) end end end diff --git a/lib/pinto/view/name.rb b/lib/pinto/view/name.rb new file mode 100755 index 0000000..260b372 --- /dev/null +++ b/lib/pinto/view/name.rb @@ -0,0 +1,23 @@ +module Pinto + class View + class Name + def initialize(view_name) + unless view_name.respond_to? :to_s + raise TypeError.new('view_name must be #to_s') + end + + @view_name = view_name.to_s + end + + def to_s + return @view_name + end + + def ==(other) + other = Pinto::View::Name.new(other) + + return (self.to_s == other.to_s) + end + end + end +end diff --git a/lib/pinto/view/parameters.rb b/lib/pinto/view/parameters.rb new file mode 100755 index 0000000..83b6c7f --- /dev/null +++ b/lib/pinto/view/parameters.rb @@ -0,0 +1,21 @@ +module Pinto + class View + class Parameters + def initialize + @parameters = {} + end + + def set(symbol, value) + unless symbol.is_a? Symbol + raise TypeError.new('symbol must be Symbol') + end + + @parameters[symbol] = value + end + + def to_hash + return @parameters + end + end + end +end diff --git a/lib/pinto/view/xhtml.rb b/lib/pinto/view/xhtml.rb index d773232..261208f 100755 --- a/lib/pinto/view/xhtml.rb +++ b/lib/pinto/view/xhtml.rb @@ -1,26 +1,30 @@ -# lib/pinto/view/xhtml.rb module Pinto class View class XHTML < Erubis::EscapedEruby - def escaped_expr(code) - unless code.is_a? String - raise ArgumentError.new('code must be String') + def evaluate(view_parameters) + unless view_parameters.is_a? Pinto::View::Parameters + raise TypeError.new( + 'view_parameters must be Pinto::View::Parameters' + ) end - return "h(#{code})" - end - def evaluate(param) - unless param.is_a? Pinto::Type::ViewParam - raise ArgumentError.new('param must be Pinto::Type::ViewParam') + context = Pinto::View::Context.new(view_parameters) + if view_parameters.to_hash.has_key? :locale_code + locale_code = Pinto::Locale::Code.new( + view_parameters.to_hash[:locale_code] + ) + context.locale_code = locale_code end - context = Pinto::View::Context.new(param) - if param.to_hash.has_key? :lang - locale = Pinto::Locale.new(param.to_hash[:lang]) - context.locale = locale + super(context) + end + + def escaped_expr(code) + unless code.is_a? String + raise TypeError.new('code must be String') end - super(context) + return "h(#{code})" end end end diff --git a/locale/en/pinto.mo b/locale/en/pinto.mo index df79cbeb858a645fd05ed2d34889237afa70463c..0adcab24c06fb8d60ff8e2eaa973b78abe072b0f 100755 GIT binary patch delta 403 zcmajau}cDB7{~GVy|nZUB%v+j6=^64ItLMV#icc9NshQv!93A$utachXg%r==wGXd~9+#e(t7bpR*C;=f#z&lDngc9(H z67Y)>P)a2M7bpQOBrEMmy@wv<>)l9-<$^AGmjl3Dxp?uGu`@w_T_u2#Z(VLlx wHyZbXfeC!`)a{MU(Cv7UuWfDDt{lfyoceV&oLa^IZ7uw;zRbQiLSL`{0D})U$p8QV delta 420 zcma*ize@sf7{~F)JG<1fC=47#zKe@+(ANej`U9GSz@_0JIfQ;*2*DL9ME}4cw7U5h zG`0qAx(Y#4i+gMD$6FkI;Nf*&?s>kSyIJln-}~Or4uvYx75bIlqZhQ+VMfHD#S)(5 zCWe^9J9O{~bw9>ke8&ckv5s@pJ&)lk?qFV|Cq55nylkT3Mc++>MyLxPs6n5|h\n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -16,48 +16,43 @@ msgstr "" # This file is distributed under the same license as the Pinto package. # IWAMOTO Takashi , 2008. # -#: lib/pinto/controller/private/base.rb:25 +#: lib/pinto/controller/private/base.rb:27 msgid "Requested HTTP method is invalid for this resource" msgstr "Requested HTTP method is invalid for this resource" -#: lib/pinto/controller/signup_account.rb:12 -#: lib/pinto/controller/signup_auth.rb:21 +#: lib/pinto/controller/signup_account.rb:16 +#: lib/pinto/controller/signup_auth.rb:28 msgid "URI contains no valid language" msgstr "URI contains no valid language" -#: lib/pinto/controller/signup_account.rb:19 +#: lib/pinto/controller/signup_account.rb:31 msgid "OpenID authentication failed" msgstr "OpenID authentication failed" -#: lib/pinto/controller/signup_auth.rb:15 +#: lib/pinto/controller/signup_auth.rb:18 msgid "Requested OpenID provider is not permitted" msgstr "Requested OpenID provider is not permitted" -#: lib/pinto/language.rb:8 +#: lib/pinto/locale.rb:7 msgid "English" msgstr "English" -#: lib/pinto/language.rb:9 +#: lib/pinto/locale.rb:9 msgid "Japanese" msgstr "Japanese" -#: lib/pinto/model/user.rb:9 -msgid "%{param} is required" -msgstr "%{param} is required" - -#: lib/pinto/model/user.rb:10 -#: view/signup_account.erb:12 -msgid "User name" -msgstr "User name" - #: view/index.erb:10 msgid "Sign up" msgstr "Sign up" #: view/index.erb:12 #: view/signup_openid.erb:22 -msgid "%{language} version of this page" -msgstr "%{language} version of this page" +msgid "%{locale_name} version of this page" +msgstr "%{locale_name} version of this page" + +#: view/signup_account.erb:12 +msgid "User name" +msgstr "User name" #: view/signup_account.erb:14 #: view/signup_openid.erb:17 @@ -67,7 +62,3 @@ msgstr "OK" #: view/signup_openid.erb:11 msgid "OpenID provider" msgstr "OpenID provider" - -#~ msgid "Server error occurred" -#~ msgstr "Server error occurred" - diff --git a/locale/ja/pinto.mo b/locale/ja/pinto.mo index cb1d7c5c6d009cdeff9c42f03204c8641d651e32..145896ffae84b127c19c747cdf0d5156c69030a1 100755 GIT binary patch delta 386 zcmYk%y-EW?5Ww--OZ>QtCj$BQ>lMS;7)d;Wkd= z9(s6yT6c(Z*ue_k;tD3Hbst#7FZ3}-@vf1+6gcRBT1I3NZ%`lHqYik$5du4W77V*l*rEbH+tlKI&cE%orW$X%h%2LC~CS|D>`*O z+i~aZM%=P--CmqD+V(6ucB8rpOt7&UhPD!Jme+)tX5IV( D0f#Z) delta 422 zcmYMvJxjw-6vpwBv_>1>uvB!A5|J)Kp@~C4oW#Yola2vHDEJyF2%*%7LY;%4;2_`l8Ifrno%x#!$_9}4|K`@NXI5Ne#vkyA27eu%1?CSswD zC9LBx1~`IysN)f;{s<@W5*@t9MSMfmXRx`96Ic*wOO=mxe!6J!BhHD;;4K>Xh-{Q+ zWQ)AuJig*I_OXseUSt(Fv541L!!D`|eIUmS9tuL6jLAwibd-g9jVg7Rsv%+$bqRV0 zrCuPFXxrNjnqH7pwb@c7+V?_lr)4z`t)$?_ diff --git a/locale/ja/pinto.po b/locale/ja/pinto.po index 1e4708c..f634880 100755 --- a/locale/ja/pinto.po +++ b/locale/ja/pinto.po @@ -1,8 +1,8 @@ msgid "" msgstr "" "Project-Id-Version: Pinto 0.0.1\n" -"POT-Creation-Date: 2008-07-31 21:11+0900\n" -"PO-Revision-Date: 2008-07-31 21:12+0900\n" +"POT-Creation-Date: 2008-08-11 21:06+0900\n" +"PO-Revision-Date: 2008-08-11 21:09+0900\n" "Last-Translator: IWAMOTO Takashi \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -16,48 +16,43 @@ msgstr "" # This file is distributed under the same license as the Pinto package. # IWAMOTO Takashi , 2008. # -#: lib/pinto/controller/private/base.rb:25 +#: lib/pinto/controller/private/base.rb:27 msgid "Requested HTTP method is invalid for this resource" msgstr "要求されたHTTPメソッドは、このリソースに対しては無効です" -#: lib/pinto/controller/signup_account.rb:12 -#: lib/pinto/controller/signup_auth.rb:21 +#: lib/pinto/controller/signup_account.rb:16 +#: lib/pinto/controller/signup_auth.rb:28 msgid "URI contains no valid language" msgstr "URIに有効な言語が含まれていません" -#: lib/pinto/controller/signup_account.rb:19 +#: lib/pinto/controller/signup_account.rb:31 msgid "OpenID authentication failed" msgstr "OpenID認証に失敗しました" -#: lib/pinto/controller/signup_auth.rb:15 +#: lib/pinto/controller/signup_auth.rb:18 msgid "Requested OpenID provider is not permitted" msgstr "指定されたOpenIDプロバイダは許可されていません" -#: lib/pinto/language.rb:8 +#: lib/pinto/locale.rb:7 msgid "English" msgstr "英語" -#: lib/pinto/language.rb:9 +#: lib/pinto/locale.rb:9 msgid "Japanese" msgstr "日本語" -#: lib/pinto/model/user.rb:9 -msgid "%{param} is required" -msgstr "%{param}は必須です" - -#: lib/pinto/model/user.rb:10 -#: view/signup_account.erb:12 -msgid "User name" -msgstr "ユーザー名" - #: view/index.erb:10 msgid "Sign up" msgstr "ユーザー登録" #: view/index.erb:12 #: view/signup_openid.erb:22 -msgid "%{language} version of this page" -msgstr "このページの%{language}版" +msgid "%{locale_name} version of this page" +msgstr "このページの%{locale_name}版" + +#: view/signup_account.erb:12 +msgid "User name" +msgstr "ユーザー名" #: view/signup_account.erb:14 #: view/signup_openid.erb:17 @@ -67,7 +62,3 @@ msgstr "OK" #: view/signup_openid.erb:11 msgid "OpenID provider" msgstr "OpenIDプロバイダ" - -#~ msgid "Server error occurred" -#~ msgstr "サーバーエラーが発生しました" - diff --git a/locale/pinto.pot b/locale/pinto.pot index f54c8f0..59569c1 100755 --- a/locale/pinto.pot +++ b/locale/pinto.pot @@ -1,56 +1,55 @@ -# Pinto -# Copyright (C) 2008 IWAMOTO Takashi -# This file is distributed under the same license as the Pinto package. -# IWAMOTO Takashi , 2008. +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. # -"Project-Id-Version: Pinto 0.0.1\n" -"POT-Creation-Date: 2008-07-31 21:11+0900\n" -"PO-Revision-Date: 2008-07-31 21:11+0900\n" -"Last-Translator: IWAMOTO Takashi \n" -"Language-Team: \n" +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"POT-Creation-Date: 2008-08-11 21:06+0900\n" +"PO-Revision-Date: 2008-08-11 21:06+0900\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"Plural-Forms: nplurals=1; plural=0;\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" -#: lib/pinto/controller/private/base.rb:25 +#: lib/pinto/controller/private/base.rb:27 msgid "Requested HTTP method is invalid for this resource" msgstr "" -#: lib/pinto/controller/signup_account.rb:12 lib/pinto/controller/signup_auth.rb:21 +#: lib/pinto/controller/signup_account.rb:16 lib/pinto/controller/signup_auth.rb:28 msgid "URI contains no valid language" msgstr "" -#: lib/pinto/controller/signup_account.rb:19 +#: lib/pinto/controller/signup_account.rb:31 msgid "OpenID authentication failed" msgstr "" -#: lib/pinto/controller/signup_auth.rb:15 +#: lib/pinto/controller/signup_auth.rb:18 msgid "Requested OpenID provider is not permitted" msgstr "" -#: lib/pinto/language.rb:8 +#: lib/pinto/locale.rb:7 msgid "English" msgstr "" -#: lib/pinto/language.rb:9 +#: lib/pinto/locale.rb:9 msgid "Japanese" msgstr "" -#: lib/pinto/model/user.rb:9 -msgid "%{param} is required" -msgstr "" - -#: lib/pinto/model/user.rb:10 view/signup_account.erb:12 -msgid "User name" -msgstr "" - #: view/index.erb:10 msgid "Sign up" msgstr "" #: view/index.erb:12 view/signup_openid.erb:22 -msgid "%{language} version of this page" +msgid "%{locale_name} version of this page" +msgstr "" + +#: view/signup_account.erb:12 +msgid "User name" msgstr "" #: view/signup_account.erb:14 view/signup_openid.erb:17 diff --git a/spec/pinto/locale_spec.rb b/spec/pinto/locale_spec.rb index 284e01a..e23b7cf 100755 --- a/spec/pinto/locale_spec.rb +++ b/spec/pinto/locale_spec.rb @@ -45,16 +45,16 @@ end describe 'Pinto::Locale#name' do - it 'should return name in English when translator.locale is English' do + it 'should return "English" when locale is "en"' do locale = Pinto::Locale.new('en') - name = locale.name(Pinto::Translator.new(locale)) + name = locale.name name.should == 'English' end - it 'should return name in Japanese when translator.locale is Japanese' do + it 'should return "Japanese" when locale is "ja"' do locale = Pinto::Locale.new('ja') - name = locale.name(Pinto::Translator.new(locale)) - name.should == '日本語' + name = locale.name + name.should == 'Japanese' end end diff --git a/view/index.erb b/view/index.erb index bc58ac2..f65ec68 100755 --- a/view/index.erb +++ b/view/index.erb @@ -1,15 +1,15 @@ - + Pinto

Pinto

diff --git a/view/multiple.erb b/view/multiple.erb index 1fb3937..0c7a7a4 100755 --- a/view/multiple.erb +++ b/view/multiple.erb @@ -5,11 +5,17 @@ Pinto -

<% unless @controller == 'index' %><% end %>Pinto<% unless @controller == 'index' %><% end %>

+

<% unless @requested_controller_name == 'index' %><% end %>Pinto<% unless @requested_controller_name == 'index' %><% end %>

diff --git a/view/signup_account.erb b/view/signup_account.erb index 9d98775..1143e3c 100755 --- a/view/signup_account.erb +++ b/view/signup_account.erb @@ -1,13 +1,13 @@ - + Pinto -

Pinto

+

Pinto

<% unless @error_message.nil? %>

<%= @error_message %>

<% end %> -
+

<%= _('User name') %>: diff --git a/view/signup_openid.erb b/view/signup_openid.erb index a8c8216..4f24ea3 100755 --- a/view/signup_openid.erb +++ b/view/signup_openid.erb @@ -1,16 +1,16 @@ - + Pinto -

Pinto

- +

Pinto

+

<%= _('OpenID provider') %>: @@ -18,8 +18,8 @@