Class: GraphQLDocs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql-docs/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphql-docs/client.rb', line 8

def initialize(options)
  @login = options[:login]
  @password = options[:password]

  if @login.nil? && !@password.nil?
    fail ArgumentError, 'Client provided a login, but no password!'
  end

  if !@login.nil? && @password.nil?
    fail ArgumentError, 'Client provided a password, but no login!'
  end

  @access_token = options[:access_token]

  @url = options[:url]
  @faraday = Faraday.new(url: @url)

  if @login && @password
    @faraday.basic_auth(@login, @password)
  elsif  @access_token
    @faraday.authorization('token', @access_token)
  end
end

Instance Attribute Details

#faradayObject

Returns the value of attribute faraday.



6
7
8
# File 'lib/graphql-docs/client.rb', line 6

def faraday
  @faraday
end

Instance Method Details

#fetchObject



32
33
34
35
36
37
38
# File 'lib/graphql-docs/client.rb', line 32

def fetch
  res = @faraday.post do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = "{ \"query\": \"#{GraphQL::Introspection::INTROSPECTION_QUERY.gsub("\n", '')}\" }"
  end
  res.body
end

#inspectObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/graphql-docs/client.rb', line 40

def inspect
  inspected = super

  # mask password
  inspected = inspected.gsub! @password, '*******' if @password

  # Only show last 4 of token, secret
  if @access_token
    inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
  end

  inspected
end