Class: Cubes::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(conn, options = {}) ⇒ Cubes::Request

Initialize a Request

Parameters:

  • conn (Faraday::Connection)
  • options (Hash) (defaults to: {})


10
11
12
13
# File 'lib/cubes/request.rb', line 10

def initialize(conn, options = {})
  @conn = conn
  @prefix = options.fetch(:prefix, '/')
end

Instance Method Details

#get(path, params = {}) ⇒ JSON

Send a get request

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

Returns:

  • (JSON)


20
21
22
23
# File 'lib/cubes/request.rb', line 20

def get(path, params = {})
  path = File.join(@prefix, path)
  JSON.parse @conn.get(path, params).body
end

#post(path, data = {}, params = {}) ⇒ JSON

Send a post request

Parameters:

  • path (String)
  • data (Hash) (defaults to: {})
  • params (Hash) (defaults to: {})

Returns:

  • (JSON)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cubes/request.rb', line 31

def post(path, data = {}, params = {})
  body = data.to_json
  path = File.join(@prefix, path)

  response = @conn.post(path, body) do |req|
    req.params = params
    req.headers['Content-Type'] = 'application/json'
  end

  JSON.parse response.body
end