Class: Neows::REST::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request_method, path, klass, options = {}) ⇒ Neows::REST::Request

Creates an instance of Request

Parameters:

  • request_method (Symbol)

    :get

  • path (String)
  • klass (Class)
  • options (Hash) (defaults to: {})


15
16
17
18
19
20
21
# File 'lib/neows/rest/request.rb', line 15

def initialize(client, request_method, path, klass, options = {})
  @client = client
  @request_method = request_method
  @path = path.gsub @client.base_url, ''
  @options = options
  @klass = klass
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/neows/rest/request.rb', line 6

def client
  @client
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/neows/rest/request.rb', line 6

def options
  @options
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/neows/rest/request.rb', line 6

def path
  @path
end

Instance Method Details

#performClass

Makes the request passing the response into the given class. If a class provides a coerce! method, it will be called to handle custom coercion of data.

Returns:

  • (Class)

    instance of Klass



32
33
34
35
36
37
# File 'lib/neows/rest/request.rb', line 32

def perform
  response = HTTP.with(request_headers).public_send(@request_method, uri, params: @options)
  klass = @klass.new JSON.parse(response.to_s).merge(client: @client)
  klass.coerce! if klass.respond_to?(:coerce!)
  klass
end

#uriString

Returns:

  • (String)


24
25
26
# File 'lib/neows/rest/request.rb', line 24

def uri
  @client.base_url + @path
end