Class: Wip::Client

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

Constant Summary collapse

API_ENDPOINT =
"https://wip.co/graphql"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: Wip::Auth.api_key) ⇒ Client

Returns a new instance of Client.



12
13
14
# File 'lib/wip/client.rb', line 12

def initialize(api_key: Wip::Auth.api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/wip/client.rb', line 10

def api_key
  @api_key
end

#jsonObject (readonly)

Returns the value of attribute json.



10
11
12
# File 'lib/wip/client.rb', line 10

def json
  @json
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/wip/client.rb', line 10

def response
  @response
end

Instance Method Details

#data(key) ⇒ Object



38
39
40
# File 'lib/wip/client.rb', line 38

def data(key)
  json["data"][key] if json
end

#headerObject



31
32
33
34
35
36
# File 'lib/wip/client.rb', line 31

def header
  {
    "Authorization": "bearer #{@api_key}",
    "Content-Type": "application/json"
  }
end

#request(query) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wip/client.rb', line 16

def request(query)
  uri = URI.parse(API_ENDPOINT)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri, header)
  request.body = { query: query }.to_json
  @response = http.request(request)

  @json = JSON.parse(@response.body)
  if @json.has_key? "errors"
    raise @json["errors"].first["message"]
  end
  @json
end