Class: GHX::RestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ghx/rest_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ RestClient

Returns a new instance of RestClient.



7
8
9
# File 'lib/ghx/rest_client.rb', line 7

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/ghx/rest_client.rb', line 5

def api_key
  @api_key
end

Instance Method Details

#get(path) ⇒ Hash

Returns the JSON response.

Returns:

  • (Hash)

    the JSON response



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ghx/rest_client.rb', line 12

def get(path)
  uri = URI.parse("https://api.github.com/#{path}")
  request = Net::HTTP::Get.new(uri)
  request["Accept"] = "application/vnd.github+json"
  request["Authorization"] = "Bearer #{@api_key}"
  request["X-Github-Api-Version"] = "2022-11-28"

  req_options = {
    use_ssl: uri.scheme == "https"
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  JSON.parse(response.body)
end

#patch(path, params) ⇒ Hash

Returns the JSON response.

Returns:

  • (Hash)

    the JSON response



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ghx/rest_client.rb', line 52

def patch(path, params)
  uri = URI.parse("https://api.github.com/#{path}")
  request = Net::HTTP::Patch.new(uri)
  request["Accept"] = "application/vnd.github+json"
  request["Authorization"] = "Bearer #{@api_key}"
  request["X-Github-Api-Version"] = "2022-11-28"

  req_options = {
    use_ssl: uri.scheme == "https"
  }

  request.body = params.to_json

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  JSON.parse(response.body)
end

#post(path, params) ⇒ Hash

Returns the JSON response.

Returns:

  • (Hash)

    the JSON response



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ghx/rest_client.rb', line 31

def post(path, params)
  uri = URI.parse("https://api.github.com/#{path}")
  request = Net::HTTP::Post.new(uri)
  request["Accept"] = "application/vnd.github+json"
  request["Authorization"] = "Bearer #{@api_key}"
  request["X-Github-Api-Version"] = "2022-11-28"

  req_options = {
    use_ssl: uri.scheme == "https"
  }

  request.body = params.to_json

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  JSON.parse(response.body)
end