Class: Sinew::Response

Inherits:
Object show all
Defined in:
lib/sinew/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



10
11
12
# File 'lib/sinew/response.rb', line 10

def body
  @body
end

#codeObject

Returns the value of attribute code.



10
11
12
# File 'lib/sinew/response.rb', line 10

def code
  @code
end

#headersObject

Returns the value of attribute headers.



10
11
12
# File 'lib/sinew/response.rb', line 10

def headers
  @headers
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/sinew/response.rb', line 10

def request
  @request
end

#uriObject

Returns the value of attribute uri.



10
11
12
# File 'lib/sinew/response.rb', line 10

def uri
  @uri
end

Class Method Details

.from_cache(request, body, head) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sinew/response.rb', line 26

def self.from_cache(request, body, head)
  Response.new.tap do |response|
    response.request = request
    response.body = body

    # defaults
    response.uri = request.uri
    response.code = 200
    response.headers = {}

    # overwrite with cached response headers
    if head
      if head !~ /^{/
        return from_legacy_head(response, head)
      end
      head = JSON.parse(head, symbolize_names: true)
      response.uri = URI.parse(head[:uri])
      response.code = head[:code]
      response.headers = head[:headers]
    end
  end
end

.from_error(request, error) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/sinew/response.rb', line 49

def self.from_error(request, error)
  Response.new.tap do |response|
    response.request = request
    response.uri = request.uri
    response.body = error.to_s
    response.code = 999
    response.headers = {}
  end
end

.from_legacy_head(response, head) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sinew/response.rb', line 59

def self.from_legacy_head(response, head)
  response.tap do |r|
    case head
    when /\ACURLER_ERROR/
      # error
      r.code = 999
    when /\AHTTP/
      # redirect
      location = head.scan(/Location: ([^\r\n]+)/).flatten.last
      r.uri += location
    else
      $stderr.puts "unknown cached /head for #{r.uri}"
    end
  end
end

.from_network(request, party_response) ⇒ Object

factory methods



16
17
18
19
20
21
22
23
24
# File 'lib/sinew/response.rb', line 16

def self.from_network(request, party_response)
  Response.new.tap do |response|
    response.request = request
    response.uri = party_response.request.last_uri
    response.code = party_response.code
    response.headers = party_response.headers.to_h
    response.body = process_body(party_response)
  end
end

.process_body(response) ⇒ Object

helper for decoding bodies before parsing



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sinew/response.rb', line 76

def self.process_body(response)
  body = response.body

  # inflate if necessary
  bits = body[0, 10].force_encoding('BINARY')
  if bits =~ /\A\x1f\x8b/n
    body = Zlib::GzipReader.new(StringIO.new(body)).read
  end

  # force to utf-8 if we think this could be text
  if body.encoding != Encoding::UTF_8
    if content_type = response.headers['content-type']
      if content_type =~ /\b(html|javascript|json|text|xml)\b/
        body = body.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
      end
    end
  end

  body
end

Instance Method Details

#error?Boolean

accessors

Returns:

  • (Boolean)


101
102
103
# File 'lib/sinew/response.rb', line 101

def error?
  code >= 400
end

#error_500?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/sinew/response.rb', line 105

def error_500?
  code / 100 >= 5
end

#head_as_jsonObject



113
114
115
116
117
118
119
# File 'lib/sinew/response.rb', line 113

def head_as_json
  {
    uri: uri,
    code: code,
    headers: headers,
  }
end

#redirected?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/sinew/response.rb', line 109

def redirected?
  request.uri != uri
end