Exception: EacEnvs::Http::Response

Inherits:
StandardError
  • Object
show all
Defined in:
lib/eac_envs/http/response.rb

Constant Summary collapse

COMMON_HEADERS =
%w[Content-Type].freeze
HEADER_LINE_PARSER =
/\A([^:]+):(.*)\z/.to_parser do |m|
  [m[1].strip, m[2].strip]
end
'Link'
/\A\<(.+)\>\s*;\s*rel\s*=\s*\"(.*)\"\z/.to_parser do |m|
  [m[2], m[1]]
end

Instance Method Summary collapse

Instance Method Details

#body_dataObject



28
29
30
31
32
33
34
35
# File 'lib/eac_envs/http/response.rb', line 28

def body_data
  r = performed.headers['Accept'].if_present(body_str) do |v|
    method_name = "body_data_from_#{v.parameterize.underscore}"
    respond_to?(method_name) ? send(method_name) : body_str
  end
  r = response_body_data_proc.call(r) if response_body_data_proc.present?
  r
end

#body_data_or_raiseObject



37
38
39
40
41
# File 'lib/eac_envs/http/response.rb', line 37

def body_data_or_raise
  raise_unless_200

  body_data
end

#body_strString

Returns:

  • (String)


44
45
46
# File 'lib/eac_envs/http/response.rb', line 44

def body_str
  performed.body
end

#body_str_or_raiseObject



48
49
50
51
52
# File 'lib/eac_envs/http/response.rb', line 48

def body_str_or_raise
  raise_unless_200

  body_str
end

#header(name) ⇒ Object



54
55
56
# File 'lib/eac_envs/http/response.rb', line 54

def header(name)
  hash_search(headers, name)
end

#headersHash<String, String>

Returns:

  • (Hash<String, String>)


59
60
61
# File 'lib/eac_envs/http/response.rb', line 59

def headers
  performed.headers.to_hash
end


63
64
65
# File 'lib/eac_envs/http/response.rb', line 63

def link(rel)
  hash_search(links, rel)
end


67
68
69
70
71
# File 'lib/eac_envs/http/response.rb', line 67

def links
  header(LINKS_HEADER_NAME).if_present({}) do |v|
    v.split(',').map { |w| LINK_PARSER.parse!(w.strip) }.to_h
  end
end

#raise_unless_200Object

Raises:

  • (self)


73
74
75
76
77
# File 'lib/eac_envs/http/response.rb', line 73

def raise_unless_200
  return nil if status == 200

  raise self
end

#statusObject



79
80
81
# File 'lib/eac_envs/http/response.rb', line 79

def status
  performed.status.to_i
end

#to_sObject



85
86
87
# File 'lib/eac_envs/http/response.rb', line 85

def to_s
  "URL: #{url}\nStatus: #{status}\nBody:\n\n#{body_str}"
end