Class: Mirrorhub::API::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ Client



8
9
10
# File 'lib/mirrorhub/api/client.rb', line 8

def initialize(location)
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



6
7
8
# File 'lib/mirrorhub/api/client.rb', line 6

def location
  @location
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/mirrorhub/api/client.rb', line 7

def token
  @token
end

Instance Method Details

#authObject



12
13
14
15
16
17
18
# File 'lib/mirrorhub/api/client.rb', line 12

def auth
  res = post('contacts/auth',
             headers: {'Grpc-Metadata-ContactEmail': mail,
                       'Grpc-Metadata-ContactPassword': password})

  true if @token = res['token']
end

#get(resource, params: {}, headers: {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/mirrorhub/api/client.rb', line 20

def get(resource, params: {}, headers: {})
  request = Typhoeus::Request.new(
    "#{@location}/#{resource}",
    method: :get,
    params: params,
    headers: headers
  )
  res = request.run
  JSON.parse(res.response_body)
end

#post(resource, params: {}, headers: {}, body: {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mirrorhub/api/client.rb', line 31

def post(resource, params: {}, headers: {}, body: {})
  request = Typhoeus::Request.new(
    "#{@location}/#{resource}",
    method: :post,
    params: params,
    body: body.to_json,
    headers: headers
  )
  res = request.run
  JSON.parse(res.response_body)

end