Sha256: 100aed5be346e128760e5fe87b0ff686a2067585283301686735dbae6967e19a

Contents?: true

Size: 1.5 KB

Versions: 10

Compression:

Stored size: 1.5 KB

Contents

require 'hyperion/formats'
require 'logatron/logatron'

class Hyperion
  module Headers
    # constructs and destructures HTTP headers

    def route_headers(route)
      headers = Logatron.http_headers
      rd = route.response_descriptor
      pd = route.payload_descriptor
      headers['Expect'] = nil # this overrides default libcurl behavior.
                              # see http://devblog.songkick.com/2012/11/27/a-second-here-a-second-there/
                              # and http://stackoverflow.com/questions/17383089/libcurl-delays-for-1-second-before-uploading-data-command-line-curl-does-not
      if rd
        headers['Accept'] = "application/vnd.#{Hyperion.config.vendor_string}.#{short_mimetype(rd)}"
      end
      if pd
        headers['Content-Type'] = content_type_for(pd.format)
      end
      headers
    end

    def short_mimetype(response_descriptor)
      x = response_descriptor
      "#{x.type}-v#{x.version}+#{x.format}"
    end

    ContentTypes = [[:json, 'application/json'],
                    [:protobuf, 'application/x-protobuf'],
                    [Multipart.format, Multipart.content_type]]

    def content_type_for(format)
      format = Hyperion::Formats.get_from(format)
      ct = ContentTypes.detect{|x| x.first == format}
      ct ? ct.last : 'application/octet-stream'
    end

    def format_for(content_type)
      ct = ContentTypes.detect{|x| x.last == content_type.split(';')[0]}
      fail "Unsupported content type: #{content_type}" unless ct
      ct.first
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
hyperion_http-0.6.0 lib/hyperion/headers.rb
hyperion_http-0.5.0 lib/hyperion/headers.rb
hyperion_http-0.3.0 lib/hyperion/headers.rb
hyperion_http-0.2.4 lib/hyperion/headers.rb
hyperion_http-0.2.3 lib/hyperion/headers.rb
hyperion_http-0.2.2 lib/hyperion/headers.rb
hyperion_http-0.2.1 lib/hyperion/headers.rb
hyperion_http-0.1.9 lib/hyperion/headers.rb
hyperion_http-0.1.8 lib/hyperion/headers.rb
hyperion_http-0.1.7 lib/hyperion/headers.rb