Class: GHX::RestClient
- Inherits:
-
Object
- Object
- GHX::RestClient
- Defined in:
- lib/ghx/rest_client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#get(path) ⇒ Hash
The JSON response.
-
#initialize(api_key) ⇒ RestClient
constructor
A new instance of RestClient.
-
#patch(path, params) ⇒ Hash
The JSON response.
-
#post(path, params) ⇒ Hash
The JSON response.
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_key ⇒ Object (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.
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" = { use_ssl: uri.scheme == "https" } response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end JSON.parse(response.body) end |
#patch(path, params) ⇒ Hash
Returns 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" = { use_ssl: uri.scheme == "https" } request.body = params.to_json response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end JSON.parse(response.body) end |
#post(path, params) ⇒ Hash
Returns 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" = { use_ssl: uri.scheme == "https" } request.body = params.to_json response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end JSON.parse(response.body) end |