Entries from 2009-08-01 to 1 month

意図がわからん

webrat0.5.1に以下のようなコードがあります。 # webrat-0.5.1/lib/webrat/core/elements/field.rb module Webrat class Field def escaped_value CGI.escape([*@value].first.to_s) end end end フィールド値(@value)をCGIパラメータに変換してるんだと思い…

RubyでHaskellのwhere句みたいなことを実現

ネタです。λかわいいよλというだけのことです。 lambda {|f| if sku.quantity sku.quantity -= 1 if sku.quantity <= 0 f[:sku_delete].call end else f[:sku_delete].call end sku.save }[{ # where :sku_delete => lambda { sku.attributes = {:deleted =>…

Webratの強力なフィールド特定能力を値のテストにも使う方法

僕らは特定のフィールドをクリックしたり値を入力したりするだけじゃなく、値の取得や検証もしたいんだ。 Then /^"([^\"]*)"がチェックされている$/ do |arg1| field_labeled(arg1).should be_checked end Then /^検索ボックスのIMEモードがオフになっている…

レスポンスがSJISのときの対応とか

WebratとCucumberを使って携帯向けサイトをテストしてたのだが、レスポンスがSJISだとうまくいかない。 今はfeatures/support/env.rbに if ENV["MOBILE"] module ActionController class CgiResponse def body_with_convert body_without_convert.toutf8 end…

Amazon Product Advertising API への対応例

2009年8月15日から何やらAmazon Web Serviceの仕様が変わるみたいです。 一つの対応例として、以下にコードを記します。これでちゃんと認証されているようです。 require "openssl" require "cgi" require "net/http" require "kconv" require "base64" requ…

webサーバを書いてみた

ふと何分で書けるのかやってみたくなった。 36分だった。 http://www.ruby-lang.org/ja/man/html/TCPServer.html のコードに毛を生やしただけ。 #!/usr/bin/env ruby require "socket" gs = TCPServer.open("", 8080) addr = gs.addr addr.shift printf("ser…