Module: Zzlink::Client

Included in:
Service
Defined in:
lib/zzlink/client.rb

Overview

The HTTP client.

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.access_keyObject

Returns the value of attribute access_key.



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

def access_key
  @access_key
end

.api_urlObject

Returns the value of attribute api_url.



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

def api_url
  @api_url
end

.secret_keyObject

Returns the value of attribute secret_key.



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

def secret_key
  @secret_key
end

Instance Method Details

#delete(path, query_params = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/zzlink/client.rb', line 59

def delete(path, query_params = {})
  uri = uri_of(path, query_params)
  resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
    req = Net::HTTP::Delete.new(uri.request_uri)
    set_request_headers(req)
    http.request(req)
  end
  check_and_return(resp)
end

#get(path, query_params = {}) ⇒ Object

Do a GET request.



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

def get(path, query_params = {})
  uri = uri_of(path, query_params)
  resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
    req = Net::HTTP::Get.new(uri.request_uri)
    set_request_headers(req)
    http.request(req)
  end
  check_and_return(resp)
end

#post(path, data = {}) ⇒ Object



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

def post(path, data = {})
  uri = uri_of(path)
  resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
    req = Net::HTTP::Post.new(uri.request_uri)
    set_request_headers(req)
    unless data.empty?
      req['Content-Type'] = 'application/json'
      req.body = data.to_json
    end
    http.request(req)
  end
  check_and_return(resp)
end

#put(path, data = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zzlink/client.rb', line 45

def put(path, data = {})
  uri = uri_of(path)
  resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
    req = Net::HTTP::Put.new(uri.request_uri)
    set_request_headers(req)
    unless data.empty?
      req['Content-Type'] = 'application/json'
      req.body = data.to_json
    end
    http.request(req)
  end
  check_and_return(resp)
end