Class: Dommy::Resources::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/resources.rb

Instance Method Summary collapse

Constructor Details

#initialize(map) ⇒ Static

Returns a new instance of Static.



114
115
116
# File 'lib/dommy/resources.rb', line 114

def initialize(map)
  @map = map || {}
end

Instance Method Details

#get(url, headers: {}) ⇒ Object



118
# File 'lib/dommy/resources.rb', line 118

def get(url, headers: {}) = request(method: "GET", url: url, headers: headers)

#request(method:, url:, headers: {}, body: nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dommy/resources.rb', line 120

def request(method:, url:, headers: {}, body: nil)
  entry = @map[url.to_s] || @map[Pathing.path_of(url)]
  return nil unless entry

  if entry.is_a?(Hash)
    ct = entry["content_type"] || entry["contentType"]
    Response.new(
      status: (entry["status"] || 200).to_i,
      status_text: entry["status_text"].to_s,
      headers: entry["headers"] || (ct ? {"Content-Type" => ct} : {}),
      body: entry["body"].to_s,
      url: url.to_s,
      redirected: false
    )
  else
    Response.new(status: 200, status_text: "OK", headers: {}, body: entry.to_s, url: url.to_s, redirected: false)
  end
end