Search code examples
rubyoctokit

How to get status code response from a Github repo using Octokit?


I'm currently trying to retrieve the status code returned by a Github repo. I have tried to use the #response_status doc, but the documentation isn't very clear on how to create an instance of Octokit:: Error class. And, I can't seem to invoke the method to retrieve the status code.

      #create an instance of the class  
      @git_client_error = Octokit::Error.new
      ....
      # sample usage
      @git_client_error.response_status

Any examples of implementation using the response_status in ruby would be much appreciated.


Solution

  • If we assume you followed the instructions in the github repo readme, you should have a client object. From there, you make requests on the client. To check the status code you can check responses. Each client request makes an http request.

    client = Octokit::Client.new(:login => 'somebody', :password => 'something!')
    # now make any request
    user = client.user
    # client.user should return a user object instance of Sawyer::Resource class
    response = client.last_response
    response.status
    => 200