Class: Parasut::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/parasut/http_client.rb

Constant Summary collapse

@@url_base =

 The url base

"#{Parasut.options.api_base_url}/v4/#{Parasut.options.company_id}"

Class Method Summary collapse

Class Method Details

.get(endpoint, query_parameters = {}, headers = {}, parse_json = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/parasut/http_client.rb', line 43

def get(endpoint, query_parameters={}, headers={}, parse_json=true)

    # The url
    url="#{@@url_base}/#{endpoint}"

    handle_headers(headers)

    # Request
    request=Faraday.get(url, query_parameters, headers)

    # Return
    parse_json ? JSON.parse(request.body).with_indifferent_access : request.body    

end

.handle_headers(headers) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/parasut/http_client.rb', line 10

def handle_headers(headers)

    # Append json content type if not exists
    headers.merge!({'Content-type': 'application/json'}) if headers['Content-type'].nil?

    # Handle access token header
    Her::Middleware::OAuthProviderHeader.add_header(headers)
    
end

.post(endpoint, body = {}, headers = {}, parse_json = true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/parasut/http_client.rb', line 22

def post(endpoint, body={}, headers={}, parse_json=true)

    # The url
    url="#{@@url_base}/#{endpoint}"

    handle_headers(headers)

    # Convert body to json if hash provided
    body=body.to_json if body.is_a?(Hash)

    # Execute the request
    request=Faraday.post(url, body, headers)

    # Return
    parse_json ? JSON.parse(request.body).with_indifferent_access : request.body    

end

.put(endpoint, query_parameters = {}, headers = {}, parse_json = true) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/parasut/http_client.rb', line 60

def put(endpoint, query_parameters={}, headers={}, parse_json=true)

    # The url
    url="#{@@url_base}/#{endpoint}"

    handle_headers(headers)

    # Request
    request=Faraday.put(url, query_parameters, headers)

    # Return
    parse_json ? JSON.parse(request.body).with_indifferent_access : request.body    

end