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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FuncCallRequest.



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hyperb/request.rb', line 140

def initialize(client, path, query = {}, verb = 'GET', body = '')
  @client = client

  set_base_url

  @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.



138
139
140
# File 'lib/hyperb/request.rb', line 138

def body
  @body
end

#clientObject

Returns the value of attribute client.



138
139
140
# File 'lib/hyperb/request.rb', line 138

def client
  @client
end

#headersObject

Returns the value of attribute headers.



138
139
140
# File 'lib/hyperb/request.rb', line 138

def headers
  @headers
end

#pathObject

Returns the value of attribute path.



138
139
140
# File 'lib/hyperb/request.rb', line 138

def path
  @path
end

#queryObject

Returns the value of attribute query.



138
139
140
# File 'lib/hyperb/request.rb', line 138

def query
  @query
end

#verbObject

Returns the value of attribute verb.



138
139
140
# File 'lib/hyperb/request.rb', line 138

def verb
  @verb
end

Instance Method Details

#fail_or_return(code, body) ⇒ Object

Raises:

  • (error)


165
166
167
168
169
# File 'lib/hyperb/request.rb', line 165

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

#performObject



157
158
159
160
161
162
163
# File 'lib/hyperb/request.rb', line 157

def perform
  final_url = @base_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

#set_base_urlObject



152
153
154
155
# File 'lib/hyperb/request.rb', line 152

def set_base_url
  @host = "#{client.region}.hyperfunc.io".freeze
  @base_url = "https://#{@host}/".freeze
end