Class: Bytom::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, token: nil) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
# File 'lib/bytom/request.rb', line 10

def initialize(
    base_url: nil,
    token: nil
)
  @base_url = base_url
  @token = token
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



8
9
10
# File 'lib/bytom/request.rb', line 8

def base_url
  @base_url
end

#base_url pass by class client(passby) ⇒ Object (readonly)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bytom/request.rb', line 7

class Request
  attr_reader :base_url, :token

  def initialize(
      base_url: nil,
      token: nil
  )
    @base_url = base_url
    @token = token
  end

  ##
  # send a http request.
  #
  def make_request(path, method = 'get', params: {})
    http_client = HTTP.headers(:accept => "application/json")
    http_client.auth("Bearer " + token) if token
    if method == 'get'
      http_client.get(base_url + path).parse
    elsif method == 'post'
      http_client.post(base_url + path, :json => params).parse
    end
  end

end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/bytom/request.rb', line 8

def token
  @token
end

#token pass by class client(passby) ⇒ Object (readonly)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bytom/request.rb', line 7

class Request
  attr_reader :base_url, :token

  def initialize(
      base_url: nil,
      token: nil
  )
    @base_url = base_url
    @token = token
  end

  ##
  # send a http request.
  #
  def make_request(path, method = 'get', params: {})
    http_client = HTTP.headers(:accept => "application/json")
    http_client.auth("Bearer " + token) if token
    if method == 'get'
      http_client.get(base_url + path).parse
    elsif method == 'post'
      http_client.post(base_url + path, :json => params).parse
    end
  end

end

Instance Method Details

#make_request(path, method = 'get', params: {}) ⇒ Object

send a http request.



21
22
23
24
25
26
27
28
29
# File 'lib/bytom/request.rb', line 21

def make_request(path, method = 'get', params: {})
  http_client = HTTP.headers(:accept => "application/json")
  http_client.auth("Bearer " + token) if token
  if method == 'get'
    http_client.get(base_url + path).parse
  elsif method == 'post'
    http_client.post(base_url + path, :json => params).parse
  end
end