| 1 | # ktai_spec.rb - specifications of ktai terminals |
|---|
| 2 | # |
|---|
| 3 | # Author:: MIZOGUCHI Coji <mizoguchi.coji at gmail.com> |
|---|
| 4 | # License:: Distributes under the same terms as Ruby |
|---|
| 5 | # |
|---|
| 6 | # $Id$ |
|---|
| 7 | # |
|---|
| 8 | require 'uri' |
|---|
| 9 | |
|---|
| 10 | module SSB |
|---|
| 11 | class KtaiSpec |
|---|
| 12 | include Enumerable |
|---|
| 13 | |
|---|
| 14 | unless defined?(CARRIER_DOCOMO) |
|---|
| 15 | CARRIER_DOCOMO = 'DoCoMo' |
|---|
| 16 | CARRIER_KDDI = 'KDDI' |
|---|
| 17 | CARRIER_SOFTBANK = 'SoftBank' |
|---|
| 18 | CARRIER_WILLCOM = 'WILLCOM' |
|---|
| 19 | CARRIER_PC = 'PC' |
|---|
| 20 | end |
|---|
| 21 | |
|---|
| 22 | def initialize(props) |
|---|
| 23 | @props = Hash.new |
|---|
| 24 | |
|---|
| 25 | @props[:useragent] = '' |
|---|
| 26 | @props[:uid] = '' |
|---|
| 27 | @props[:hid] = '' |
|---|
| 28 | @props[:icc] = '' |
|---|
| 29 | @props[:exheader] = '' |
|---|
| 30 | |
|---|
| 31 | if props.class == Hash |
|---|
| 32 | props.each do |key,value| |
|---|
| 33 | unless value.nil? |
|---|
| 34 | @props[key.to_sym] = value |
|---|
| 35 | else |
|---|
| 36 | @props[key.to_sym] = '' |
|---|
| 37 | end |
|---|
| 38 | end |
|---|
| 39 | else |
|---|
| 40 | @props[:useragent] = props.to_s |
|---|
| 41 | end |
|---|
| 42 | end |
|---|
| 43 | |
|---|
| 44 | # get property |
|---|
| 45 | # 'hid' Hardware ID |
|---|
| 46 | # 'icc' FOMA CARD NO |
|---|
| 47 | # 'useragent' User-Agent |
|---|
| 48 | # 'uid' Terminal UID |
|---|
| 49 | def [](key) |
|---|
| 50 | @props[key] |
|---|
| 51 | end |
|---|
| 52 | |
|---|
| 53 | # set property |
|---|
| 54 | def []=(key, value) |
|---|
| 55 | @props[key] = value |
|---|
| 56 | end |
|---|
| 57 | |
|---|
| 58 | # get user-agent by carrier, hid and icc |
|---|
| 59 | def get_useragent(hid = false, no_flash = false) |
|---|
| 60 | if get_carrier() != CARRIER_DOCOMO || hid == false || (not has_key?(:hid)) |
|---|
| 61 | return @props[:useragent] |
|---|
| 62 | end |
|---|
| 63 | |
|---|
| 64 | ua = @props[:useragent].dup |
|---|
| 65 | unless ua.to_s == '' |
|---|
| 66 | if ua =~ /DoCoMo\/1.0/ # PDC |
|---|
| 67 | ua.concat '/' + self['hid'] unless self['hid'].nil? |
|---|
| 68 | else # FOMA |
|---|
| 69 | param_start = ua.index('(') |
|---|
| 70 | ua = ua[0, param_start] unless param_start.nil? |
|---|
| 71 | |
|---|
| 72 | ua.concat '(c100;TB' |
|---|
| 73 | ua.concat ';' + self[:hid] |
|---|
| 74 | ua.concat ';' + self[:icc] if has_key?(:icc) |
|---|
| 75 | ua.concat ')' |
|---|
| 76 | |
|---|
| 77 | # ua.gsub!(/;TB/, ';TC') unless no_flash |
|---|
| 78 | end |
|---|
| 79 | end |
|---|
| 80 | return ua |
|---|
| 81 | end |
|---|
| 82 | |
|---|
| 83 | # carrier detection |
|---|
| 84 | def get_carrier |
|---|
| 85 | ua = @props[:useragent] |
|---|
| 86 | if ua =~ /DoCoMo/ |
|---|
| 87 | CARRIER_DOCOMO |
|---|
| 88 | elsif ua =~ /KDDI/ |
|---|
| 89 | CARRIER_KDDI |
|---|
| 90 | elsif ua =~ /Vodafone/ |
|---|
| 91 | CARRIER_SOFTBANK |
|---|
| 92 | elsif ua =~ /SoftBank/ |
|---|
| 93 | CARRIER_SOFTBANK |
|---|
| 94 | elsif ua =~ /J-PHONE/ |
|---|
| 95 | CARRIER_SOFTBANK |
|---|
| 96 | elsif ua =~ /UP.Browser/ |
|---|
| 97 | CARRIER_KDDI |
|---|
| 98 | elsif ua =~ /WILLCOM/ |
|---|
| 99 | CARRIER_WILLCOM |
|---|
| 100 | elsif ua =~ /DDIPOCKET/ |
|---|
| 101 | CARRIER_WILLCOM |
|---|
| 102 | else |
|---|
| 103 | CARRIER_PC |
|---|
| 104 | end |
|---|
| 105 | end |
|---|
| 106 | |
|---|
| 107 | # get transformed URL if exists UID and DoCoMo |
|---|
| 108 | def get_transformed_uri(uri) |
|---|
| 109 | return nil if uri.nil? |
|---|
| 110 | uri = URI.parse(uri) unless uri.kind_of?(URI) |
|---|
| 111 | |
|---|
| 112 | return uri unless get_carrier == CARRIER_DOCOMO |
|---|
| 113 | return uri unless has_key?(:uid) |
|---|
| 114 | return uri if uri.query.nil? |
|---|
| 115 | |
|---|
| 116 | uri.query.gsub!(/uid=NULLGWDOCOMO/, "uid=#{self[:uid]}") |
|---|
| 117 | uri |
|---|
| 118 | end |
|---|
| 119 | |
|---|
| 120 | # suitable request header by carrier |
|---|
| 121 | def get_request_header(hid = false) |
|---|
| 122 | header = Hash.new |
|---|
| 123 | |
|---|
| 124 | case get_carrier() |
|---|
| 125 | when CARRIER_KDDI |
|---|
| 126 | header['X-UP-SUBNO'] = self[:uid] |
|---|
| 127 | when CARRIER_SOFTBANK |
|---|
| 128 | header['X-JPHONE-UID'] = self[:uid] |
|---|
| 129 | end if has_key?('uid') |
|---|
| 130 | |
|---|
| 131 | header['User-Agent'] = get_useragent(hid) |
|---|
| 132 | |
|---|
| 133 | @props[:exheader].split("\r\n").each do |field| |
|---|
| 134 | key,value = field.split(':') |
|---|
| 135 | unless key.nil? || value.nil? |
|---|
| 136 | header[key.strip] = value.strip |
|---|
| 137 | end |
|---|
| 138 | end |
|---|
| 139 | |
|---|
| 140 | header |
|---|
| 141 | end |
|---|
| 142 | |
|---|
| 143 | def size |
|---|
| 144 | @props.size |
|---|
| 145 | end |
|---|
| 146 | alias length size |
|---|
| 147 | |
|---|
| 148 | def has_key?(key) |
|---|
| 149 | @props.has_key?(key) |
|---|
| 150 | end |
|---|
| 151 | |
|---|
| 152 | def each(&block) |
|---|
| 153 | @props.each { |key, value| yield key, value } |
|---|
| 154 | end |
|---|
| 155 | |
|---|
| 156 | def inspect |
|---|
| 157 | '{' + @props.collect { |key,value| "#{key}=>#{value.inspect}" }.join(",") +'}' |
|---|
| 158 | end |
|---|
| 159 | end |
|---|
| 160 | end |
|---|