Class: Hubba::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hubba/client.rb

Constant Summary collapse

BASE_URL =
'https://api.github.com'

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, user: nil, password: nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
# File 'lib/hubba/client.rb', line 9

def initialize( token: nil,
                user:  nil, password: nil )
  ## add support for (personal access) token
  @token     = token

  ## add support for basic auth - defaults to no auth (nil/nil)
  ##   remove - deprecated (use token) - why? why not?
  @user      = user    ## use login like Oktokit - why? why not?
  @password  = password
end

Instance Method Details

#get(request_uri) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hubba/client.rb', line 22

def get( request_uri )
  puts "GET #{request_uri}"

  ## note: request_uri ALWAYS starts with leading /, thus use + for now!!!
  #          e.g. /users/geraldb
  #               /users/geraldb/repos
  url = BASE_URL + request_uri

  headers = {}
  headers['User-Agent'] = 'ruby/hubba'                      ## required by GitHub API
  headers['Accept']     = 'application/vnd.github.v3+json'  ## recommend by GitHub API

  auth = []
  ## check if credentials (user/password) present - if yes, use basic auth
  if @token
    puts "  using (personal access) token - starting with: #{@token[0..6]}**********"
    headers['Authorization'] = "token #{@token}"
    ## token works like:
    ##  curl -H 'Authorization: token my_access_token' https://api.github.com/user/repos
  elsif @user && @password
    puts "  using basic auth - user: #{@user}, password: ***"
    ## use credential auth "tuple" (that is, array with two string items) for now
    ##  or use Webclient::HttpBasicAuth or something - why? why not?
    auth = [@user, @password]
    # req.basic_auth( @user, @password )
  else
    puts "  using no credentials (no token, no user/password)"
  end

  res = Webclient.get( url,
                       headers: headers,
                       auth:    auth )

  # Get specific header
  # response["content-type"]
  # => "text/html; charset=UTF-8"

  # Iterate all response headers.
  # puts "HTTP HEADERS:"
  # res.headers.each do |key, value|
  #  puts "  #{key}: >#{value}<"
  # end
  # puts

  # => "location => http://www.google.com/"
  # => "content-type => text/html; charset=UTF-8"
  # ...

  if res.status.ok?
    res.json
  else
    puts "!! HTTP ERROR: #{res.status.code} #{res.status.message}:"
    pp res.raw
    exit 1
  end
end