Sha256: aad0ea3139f2ce6164230c0ce6a33520f0b8f571ed3e02c110a4f7d10f305e2e

Contents?: true

Size: 1.79 KB

Versions: 112

Compression:

Stored size: 1.79 KB

Contents

require 'time'

module Aws
  module Rest
    module Response
      class Headers

        include Seahorse::Model::Shapes

        # @param [Seahorse::Model::ShapeRef] rules
        def initialize(rules)
          @rules = rules
        end

        # @param [Seahorse::Client::Http::Response] http_resp
        # @param [Hash, Struct] target
        def apply(http_resp, target)
          headers = http_resp.headers
          @rules.shape.members.each do |name, ref|
            case ref.location
            when 'header' then extract_header_value(headers, name, ref, target)
            when 'headers' then extract_header_map(headers, name, ref, target)
            end
          end
        end

        def extract_header_value(headers, name, ref, data)
          if headers.key?(ref.location_name)
            data[name] = cast_value(ref, headers[ref.location_name])
          end
        end

        def cast_value(ref, value)
          case ref.shape
          when StringShape then value
          when IntegerShape then value.to_i
          when FloatShape then value.to_f
          when BooleanShape then value == 'true'
          when TimestampShape
            if value =~ /\d+(\.\d*)/
              Time.at(value.to_f)
            else
              begin
                Time.parse(value)
              rescue
                nil
              end
            end
          else raise "unsupported shape #{ref.shape.class}"
          end
        end

        def extract_header_map(headers, name, ref, data)
          data[name] = {}
          prefix = ref.location_name || ''
          headers.each do |header_name, header_value|
            if match = header_name.match(/^#{prefix}(.+)/i)
              data[name][match[1]] = header_value
            end
          end
        end

      end
    end
  end
end

Version data entries

112 entries across 112 versions & 2 rubygems

Version Path
aws-sdk-core-2.3.23 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.14 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.13 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.12 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.11 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.10 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.9 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.8 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.7 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.6 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.5 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.4 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-3.0.0.rc7 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.3 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-3.0.0.rc6 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.2 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-3.0.0.rc5 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.1 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.8.0 lib/aws-sdk-core/rest/response/headers.rb
aws-sdk-core-2.7.16 lib/aws-sdk-core/rest/response/headers.rb