Module: CoachClient::Request

Defined in:
lib/coach_client/request.rb

Overview

Request methods for the HTTP verbs GET, PUT, POST, DELETE.

Constant Summary collapse

DEFAULT_HEADER =

The default header.

{ accept: :json }

Class Method Summary collapse

Class Method Details

.delete(url, username: nil, password: nil, **header) ⇒ CoachClient::Response

DELETE request to the RESTful service.

Parameters:

  • url (String)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • header (Hash)

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/coach_client/request.rb', line 88

def self.delete(url, username: nil, password: nil, **header)
  header.merge!(DEFAULT_HEADER)
  begin
    response = RestClient::Request.execute(method: :delete, url: url,
                                           user: username,
                                           password: password,
                                           headers: header)
  rescue RestClient::ResourceNotFound => e
    raise CoachClient::NotFound, e.message
  rescue RestClient::Unauthorized => e
    raise CoachClient::Unauthorized, e.message
  end
  CoachClient::Response.new(response.headers, response.body, response.code)
end

.get(url, username: nil, password: nil, **header) ⇒ CoachClient::Response

GET request to the RESTful service.

Parameters:

  • url (String)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • header (Hash)

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coach_client/request.rb', line 14

def self.get(url, username: nil, password: nil, **header)
  header.merge!(DEFAULT_HEADER)
  begin
    response = RestClient::Request.execute(method: :get, url: url,
                                           user: username,
                                           password: password,
                                           headers: header)
  rescue RestClient::ResourceNotFound => e
    raise CoachClient::NotFound, e.message
  rescue RestClient::Unauthorized => e
    raise CoachClient::Unauthorized, e.message
  end
  CoachClient::Response.new(response.headers, response.body, response.code)
end

.post(url, username: nil, password: nil, payload:, **header) ⇒ CoachClient::Response

POST request to the RESTful service.

Parameters:

  • url (String)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • payload (String)

    required

  • header (Hash)

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/coach_client/request.rb', line 63

def self.post(url, username: nil, password: nil, payload:, **header)
  header.merge!(DEFAULT_HEADER)
  begin
    response = RestClient::Request.execute(method: :post, url: url,
                                           user: username,
                                           password: password,
                                           payload: payload,
                                           headers: header)
  rescue RestClient::ResourceNotFound => e
    raise CoachClient::NotFound, e.message
  rescue RestClient::Unauthorized => e
    raise CoachClient::Unauthorized, e.message
  rescue RestClient::Conflict
    raise CoachClient::IncompleteInformation, 'Incomplete Information'
  end
  CoachClient::Response.new(response.headers, response.body, response.code)
end

.put(url, username: nil, password: nil, payload:, **header) ⇒ CoachClient::Response

PUT request to the RESTful service.

Parameters:

  • url (String)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • payload (String)

    required

  • header (Hash)

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/coach_client/request.rb', line 37

def self.put(url, username: nil, password: nil, payload:, **header)
  header.merge!(DEFAULT_HEADER)
  begin
    response = RestClient::Request.execute(method: :put, url: url,
                                           user: username,
                                           password: password,
                                           payload: payload,
                                           headers: header)
  rescue RestClient::ResourceNotFound => e
    raise CoachClient::NotFound, e.message
  rescue RestClient::Unauthorized => e
    raise CoachClient::Unauthorized, e.message
  rescue RestClient::Conflict
    raise CoachClient::IncompleteInformation, 'Incomplete Information'
  end
  CoachClient::Response.new(response.headers, response.body, response.code)
end