Class: Hyperb::FuncCallRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperb/request.rb

Overview

func requests are very simple, they do not require signing

Constant Summary collapse

REGION =
'us-west-1'.freeze
URL =
"https://#{REGION}.hyperfunc.io/".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path, query = {}, verb = 'GET', body = '') ⇒ FuncCallRequest

Returns a new instance of FuncCallRequest.



137
138
139
140
141
142
143
144
# File 'lib/hyperb/request.rb', line 137

def initialize(client, path, query = {}, verb = 'GET', body = '')
  @client = client
  @path = path
  @verb = verb
  @query = URI.encode_www_form(query)
  @body = body.empty? ? body : body.to_json
  @headers = { content_type: 'application/json' }
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



135
136
137
# File 'lib/hyperb/request.rb', line 135

def body
  @body
end

#headersObject

Returns the value of attribute headers.



135
136
137
# File 'lib/hyperb/request.rb', line 135

def headers
  @headers
end

#pathObject

Returns the value of attribute path.



135
136
137
# File 'lib/hyperb/request.rb', line 135

def path
  @path
end

#queryObject

Returns the value of attribute query.



135
136
137
# File 'lib/hyperb/request.rb', line 135

def query
  @query
end

#verbObject

Returns the value of attribute verb.



135
136
137
# File 'lib/hyperb/request.rb', line 135

def verb
  @verb
end

Instance Method Details

#fail_or_return(code, body) ⇒ Object

Raises:

  • (error)


154
155
156
157
158
# File 'lib/hyperb/request.rb', line 154

def fail_or_return(code, body)
  error = Hyperb::Error::ERRORS[code]
  raise(error.new(body, code)) if error
  body
end

#performObject



146
147
148
149
150
151
152
# File 'lib/hyperb/request.rb', line 146

def perform
  final_url = URL + @path + '?' + @query
  options = {}
  options[:body] = @body unless @body.empty?
  response = HTTP.headers(@headers).public_send(@verb.downcase.to_sym, final_url, options)
  fail_or_return(response.code, response.body)
end