Class: Clubhouse::Client

Inherits:
Object
  • Object
show all
Includes:
APIActions
Defined in:
lib/clubhouse/client.rb

Constant Summary collapse

API_VERSION =
'v1'.freeze

Instance Method Summary collapse

Methods included from APIActions

#find

Constructor Details

#initialize(token) ⇒ Client

Returns a new instance of Client.



13
14
15
# File 'lib/clubhouse/client.rb', line 13

def initialize(token)
  @token = token
end

Instance Method Details

#basepathObject



17
18
19
# File 'lib/clubhouse/client.rb', line 17

def basepath
  @basepath ||= "https://api.clubhouse.io/api/#{API_VERSION}"
end

#delete(resource) ⇒ Object



26
27
28
29
# File 'lib/clubhouse/client.rb', line 26

def delete(resource)
  req = Net::HTTP::Delete.new(build_uri(resource))
  do_request(req)
end

#get(resource) ⇒ Object



21
22
23
24
# File 'lib/clubhouse/client.rb', line 21

def get(resource)
  req = Net::HTTP::Get.new(build_uri(resource))
  do_request(req)
end

#post(resource, body = {}) ⇒ Object



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

def post(resource, body = {})
  req = Net::HTTP::Post.new(build_uri(resource))
  req['Content-Type'] = 'application/json'
  req.body = body.to_json

  do_request(req)
end

#put(resource, body = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/clubhouse/client.rb', line 39

def put(resource, body = {})
  req = Net::HTTP::Put.new(build_uri(resource))
  req['Content-Type'] = 'application/json'
  req.body = body.to_json

  do_request(req)
end

#raise_error_to_user(response) ⇒ Object

Raises:

  • (err)


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/clubhouse/client.rb', line 47

def raise_error_to_user(response)
  code = response.code.to_i

  err = case code
    when 400 then BadRequestError
    when 401, 403 then UnauthorizedError
    when 404 then ResourceNotFoundError
    when 422 then UnprocessableError
    else UnexpectedError
  end

  raise err, response.body
end