Class: Slash::Formats::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/slash/formats.rb

Direct Known Subclasses

WithSuffix

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Format

Returns a new instance of Format.



19
20
21
22
# File 'lib/slash/formats.rb', line 19

def initialize(options)
  @codec = options[:codec]
  @mime = options[:mime] || (@codec.respond_to?(:mime) ? @codec.mime : nil)
end

Instance Attribute Details

#codecObject (readonly)

Returns the value of attribute codec.



17
18
19
# File 'lib/slash/formats.rb', line 17

def codec
  @codec
end

#mimeObject (readonly)

Returns the value of attribute mime.



17
18
19
# File 'lib/slash/formats.rb', line 17

def mime
  @mime
end

Instance Method Details

#interpret_response(response) ⇒ Object



35
36
37
38
# File 'lib/slash/formats.rb', line 35

def interpret_response(response)
  bs = response.body_stream
  bs && codec.decode(bs)
end

#prepare_request(options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/slash/formats.rb', line 24

def prepare_request(options)
  headers = options[:headers]
  headers['Accept'] = mime if mime
  data = options.delete(:data)
  if data
    options[:body] = codec.encode(data)
    headers['Content-Type'] = mime if mime
  end
  options
end