Class: GHX::RestClient

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

Overview

RestClient is a simple wrapper around Net::HTTP to make it easier to make API calls to the GitHub REST API.

This is necessary because not all GitHub API endpoints are covered by Octokit.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ RestClient

Returns a new instance of RestClient.

Parameters:

  • api_key (String)

    the GitHub API key



11
12
13
# File 'lib/ghx/rest_client.rb', line 11

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

Instance Method Details

#get(path) ⇒ Hash

Make a GET request to the given path

Parameters:

  • path (String)

    the path to the API endpoint

Returns:

  • (Hash)

    the parsed JSON response



18
19
20
21
22
23
24
25
# File 'lib/ghx/rest_client.rb', line 18

def get(path)
  uri = URI.parse("https://api.github.com/#{path}")
  request = Net::HTTP::Get.new(uri)

  response = _http_request(uri: uri, request: request)

  JSON.parse(response.body)
end

#patch(path, params) ⇒ Hash

Make a PATCH request to the given path with the given params

Parameters:

  • path (String)

    the path to the API endpoint

  • params (Hash)

    the request body

Returns:

  • (Hash)

    the parsed JSON response



46
47
48
49
50
51
52
53
54
55
# File 'lib/ghx/rest_client.rb', line 46

def patch(path, params)
  uri = URI.parse("https://api.github.com/#{path}")
  request = Net::HTTP::Patch.new(uri)

  request.body = params.to_json

  response = _http_request(uri: uri, request: request)

  JSON.parse(response.body)
end

#post(path, params) ⇒ Hash

Make a POST request to the given path with the given params

Parameters:

  • path (String)

    the path to the API endpoint

  • params (Hash)

    the request body

Returns:

  • (Hash)

    the parsed JSON response



31
32
33
34
35
36
37
38
39
40
# 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.body = params.to_json

  response = _http_request(uri: uri, request: request)

  JSON.parse(response.body)
end