Class: Hubba::Client
- Inherits:
-
Object
- Object
- Hubba::Client
- Defined in:
- lib/hubba/client.rb
Instance Method Summary collapse
-
#get(request_uri) ⇒ Object
method initialize.
-
#initialize(user: nil, password: nil) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(user: nil, password: nil) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/hubba/client.rb', line 8 def initialize( user: nil, password: nil ) uri = URI.parse( "https://api.github.com" ) @http = Net::HTTP.new(uri.host, uri.port) @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_NONE ## add support for basic auth - defaults to no auth (nil/nil) @user = user ## use login like Oktokit - why? why not? @password = password end |
Instance Method Details
#get(request_uri) ⇒ Object
method initialize
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/hubba/client.rb', line 19 def get( request_uri ) puts "GET #{request_uri}" req = Net::HTTP::Get.new( request_uri ) ## req = Net::HTTP::Get.new( "/users/geraldb" ) ## req = Net::HTTP::Get.new( "/users/geraldb/repos" ) req["User-Agent"] = "ruby/hubba" ## required by GitHub API req["Accept" ] = "application/vnd.github.v3+json" ## recommend by GitHub API ## check if credentials (user/password) present - if yes, use basic auth if @user && @password puts " using basic auth - user: #{@user}, password: ***" req.basic_auth( @user, @password ) end res = @http.request(req) # Get specific header # response["content-type"] # => "text/html; charset=UTF-8" # Iterate all response headers. res.each_header do |key, value| p "#{key} => #{value}" end # => "location => http://www.google.com/" # => "content-type => text/html; charset=UTF-8" # ... json = JSON.parse( res.body ) ## pp json json end |