class Seahorse::Client::Plugins::RestfulBindings::Handler

@api private

Public Instance Methods

call(context) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 14
def call(context)
  build_request(context)
  @handler.call(context).on(200..299) do |response|
    parse_response(response)
  end
end

Private Instance Methods

build_request(context) click to toggle source

Populates the HTTP request method and headers.

# File lib/seahorse/client/plugins/restful_bindings.rb, line 24
def build_request(context)
  populate_http_request_method(context)
  populate_http_headers(context)
end
each_member(ref, &block) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 116
def each_member(ref, &block)
  ref.shape.members.each(&block) if ref
end
extract_header(headers, ref) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 84
def extract_header(headers, ref)
  parse_header_value(ref, headers[ref.location_name])
end
extract_header_map(headers, ref) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 88
def extract_header_map(headers, ref)
  prefix = ref.location_name || ''
  hash = {}
  headers.each do |header, value|
    if match = header.match(/^#{prefix}(.+)/i)
      hash[match[1]] = parse_header_value(ref.shape.value, value)
    end
  end
  hash
end
parse_header_value(ref, value) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 99
def parse_header_value(ref, value)
  if value
    case ref.shape
    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
        Time.parse(value)
      end
    else value
    end
  end
end
parse_response(response) click to toggle source

Extracts HTTP response headers and status code.

# File lib/seahorse/client/plugins/restful_bindings.rb, line 67
def parse_response(response)
  headers = response.context.http_response.headers
  each_member(response.context.operation.output) do |key, ref|
    case ref.location
    when 'statusCode'
      status_code = response.context.http_response.status_code
      response.data[key] = status_code
    when 'header'
      if headers.key?(ref.location_name)
        response.data[key] = extract_header(headers, ref)
      end
    when 'headers'
      response.data[key] = extract_header_map(headers, ref)
    end
  end
end
populate_http_headers(context) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 33
def populate_http_headers(context)
  params = context.params
  headers = context.http_request.headers
  each_member(context.operation.input) do |member_name, member_ref|
    value = params[member_name]
    next if value.nil?
    case member_ref.location
    when 'header'  then serialize_header(headers, member_ref, value)
    when 'headers' then serialize_header_map(headers, member_ref, value)
    end
  end
end
populate_http_request_method(context) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 29
def populate_http_request_method(context)
  context.http_request.http_method = context.operation.http_method
end
serialize_header(headers, ref, value) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 46
def serialize_header(headers, ref, value)
  headers[ref.location_name] = serialize_header_value(ref, value)
end
serialize_header_map(headers, ref, values) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 50
def serialize_header_map(headers, ref, values)
  prefix = ref.location_name || ''
  values.each_pair do |name, value|
    value = serialize_header_value(ref.shape.value, value)
    headers["#{prefix}#{name}"] = value
  end
end
serialize_header_value(ref, value) click to toggle source
# File lib/seahorse/client/plugins/restful_bindings.rb, line 58
def serialize_header_value(ref, value)
  if TimestampShape === ref.shape
    value.utc.httpdate
  else
    value.to_s
  end
end