Module: Resto::Request::Header

Included in:
Base
Defined in:
lib/resto/request/header.rb

Instance Method Summary collapse

Instance Method Details

#accept(accept) ⇒ Object



29
30
31
# File 'lib/resto/request/header.rb', line 29

def accept(accept)
  tap { composed_headers.store('accept', accept) }
end

#basic_auth(options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/resto/request/header.rb', line 37

def basic_auth(options)
  options = AssertHash.keys(options, :username, :password)

  username = options.fetch(:username)
  password = options.fetch(:password)
  base64_encode = ["#{username}:#{password}"].pack('m').delete("\r\n")
  basic_encode = 'Basic ' + base64_encode

  tap { composed_headers.store('authorization', basic_encode) }
end

#composed_headersObject



21
22
23
# File 'lib/resto/request/header.rb', line 21

def composed_headers
  @headers ||= { 'accept'=> '*/*' , 'user-agent'=> 'Ruby' }
end

#content_type(content_type) ⇒ Object



33
34
35
# File 'lib/resto/request/header.rb', line 33

def content_type(content_type)
  tap { composed_headers.store('content-type', content_type) }
end

#format(symbol, options = nil) ⇒ Object



9
10
11
# File 'lib/resto/request/header.rb', line 9

def format(symbol, options=nil)
  formatter(Resto::Format.get(@symbol = symbol), options)
end

#formatter(formatter, options = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/resto/request/header.rb', line 13

def formatter(formatter, options=nil)
  options = AssertHash.keys(options, :extension)
  @add_extension = options.fetch(:extension) { false }
  @formatter = formatter
  accept(formatter.accept)
  content_type(formatter.content_type)
end

#headers(headers) ⇒ Object



25
26
27
# File 'lib/resto/request/header.rb', line 25

def headers(headers)
  tap { composed_headers.merge!(headers) }
end