Class: Hachi::Clients::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/hachi/clients/base.rb

Direct Known Subclasses

Alert, Artifact, Case, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint:, api_key:) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/hachi/clients/base.rb', line 12

def initialize(api_endpoint:, api_key:)
  @api_endpoint = URI(api_endpoint)
  @api_key = api_key
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



10
11
12
# File 'lib/hachi/clients/base.rb', line 10

def api_endpoint
  @api_endpoint
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/hachi/clients/base.rb', line 10

def api_key
  @api_key
end

Instance Method Details

#delete(path, params: {}, json: {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/hachi/clients/base.rb', line 39

def delete(path, params: {}, json: {}, &block)
  url = url_for(path)
  url.query = URI.encode_www_form(params) unless params.empty?

  delete = Net::HTTP::Delete.new(url)
  delete.body = json.is_a?(Hash) ? json.to_json : json.to_s

  delete.add_field "Authorization", "Bearer #{api_key}"
  request(delete, &block)
end

#get(path, params: {}, &block) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/hachi/clients/base.rb', line 17

def get(path, params: {}, &block)
  url = url_for(path)
  url.query = URI.encode_www_form(params) unless params.empty?

  get = Net::HTTP::Get.new(url)
  get.add_field "Authorization", "Bearer #{api_key}"
  request(get, &block)
end

#patch(path, params: {}, json: {}, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hachi/clients/base.rb', line 50

def patch(path, params: {}, json: {}, &block)
  url = url_for(path)
  url.query = URI.encode_www_form(params) unless params.empty?

  patch = Net::HTTP::Patch.new(url)
  patch.body = json.is_a?(Hash) ? json.to_json : json.to_s

  patch.add_field "Content-Type", "application/json"
  patch.add_field "Authorization", "Bearer #{api_key}"

  request(patch, &block)
end

#post(path, params: {}, json: {}, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hachi/clients/base.rb', line 26

def post(path, params: {}, json: {}, &block)
  url = url_for(path)
  url.query = URI.encode_www_form(params) unless params.empty?

  post = Net::HTTP::Post.new(url)
  post.body = json.is_a?(Hash) ? json.to_json : json.to_s

  post.add_field "Content-Type", "application/json"
  post.add_field "Authorization", "Bearer #{api_key}"

  request(post, &block)
end