Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
reconstructing...
Browse files Browse the repository at this point in the history
  • Loading branch information
iwamot committed Aug 12, 2008
1 parent 012cac0 commit 3eec57c
Show file tree
Hide file tree
Showing 85 changed files with 1,203 additions and 938 deletions.
1 change: 1 addition & 0 deletions TODO
@@ -1,4 +1,5 @@
・できるだけ基本データ型をラップする
・受け入れ側は寛容に!
・既存クラスのテストを書く

・ユーザー登録処理をBDDで実装する
Expand Down
12 changes: 6 additions & 6 deletions 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
Expand Down
122 changes: 112 additions & 10 deletions 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
Expand Down
15 changes: 15 additions & 0 deletions 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
36 changes: 28 additions & 8 deletions 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
17 changes: 4 additions & 13 deletions lib/pinto/config/db.rb
@@ -1,4 +1,3 @@
# lib/pinto/config/db.rb
module Pinto
class Config
class DB
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions 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
59 changes: 59 additions & 0 deletions 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
17 changes: 17 additions & 0 deletions 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
44 changes: 16 additions & 28 deletions 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
Expand Down

0 comments on commit 3eec57c

Please sign in to comment.