Class: GHX::RestClient
- Inherits:
-
Object
- Object
- GHX::RestClient
- 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
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#get(path) ⇒ Hash
Make a GET request to the given path.
-
#initialize(api_key) ⇒ RestClient
constructor
A new instance of RestClient.
-
#patch(path, params) ⇒ Hash
Make a PATCH request to the given path with the given params.
-
#post(path, params) ⇒ Hash
Make a POST request to the given path with the given params.
Constructor Details
#initialize(api_key) ⇒ RestClient
Returns a new instance of RestClient.
11 12 13 |
# File 'lib/ghx/rest_client.rb', line 11 def initialize(api_key) @api_key = api_key end |
Instance Attribute Details
#api_key ⇒ Object (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
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
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
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 |