Class: HlrLookupsSDK::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, log_file = NIL) ⇒ Client

HLR Lookups API client constructor, initialize this with the API key and secret as given by www.hlr-lookups.com/en/api-settings

Parameters:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_hlr_client/client.rb', line 19

def initialize(key, secret, log_file = NIL)

  # @string The API Key as given by https://www.hlr-lookups.com/en/api-settings
  @key = key

  # @string The API Secret as given by https://www.hlr-lookups.com/en/api-settings
  @secret = secret

  # @string The API version to which we connect (leave it as is)
  @api_version = HlrLookupsSDK::API_VERSION

  # @string Used in the HTTP user agent (leave it as is)
  @client_name = HlrLookupsSDK::CLIENT_NAME

  # @string The current version of this SDK, used in the HTTP user agent (leave it as is)
  @client_version = HlrLookupsSDK::CLIENT_VERSION

  # @string HLR Lookup connect url
  @connect_url = HlrLookupsSDK::CONNECT_URL

  # @string|NIL Specifies the log file to which to write, if any.
  @log_file = log_file ? log_file : NIL

end

Instance Method Details

#delete(endpoint, params = {}) ⇒ Object

Use this method to communicate with DELETE endpoints

Parameters:

  • endpoint (string)
  • params (hash) (defaults to: {})

    , a list of parameters to be included in the request

Returns:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ruby_hlr_client/client.rb', line 123

def delete(endpoint, params = {})

  path = build_connect_url(endpoint)
  headers = build_headers(endpoint, 'DELETE', params)

  log "DELETE " + path + " " + params.to_s
  log headers.to_s

  begin
    response = RestClient::Request.execute(method: :delete, url: path, payload: params.to_json, headers: headers, timeout: 15)
  rescue RestClient::ExceptionWithResponse => e
    log e.http_code.to_s + " " + e.response.to_s
    return e.response
  end

  log response.code.to_s + " " + response.to_s

  response

end

#get(endpoint, params = {}) ⇒ Object

Use this method to communicate with GET endpoints

Parameters:

  • endpoint (string)

    , e.g. GET /auth-test

  • params (hash) (defaults to: {})

    , a list of GET parameters to be included in the request

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_hlr_client/client.rb', line 48

def get(endpoint, params = {})

  path = build_connect_url(endpoint) + '?' +  URI.encode_www_form(params)
  headers = build_headers(endpoint, 'GET', params)

  log "GET " + path
  log headers.to_s

  begin
    response = RestClient::Request.execute(method: :get, url: path, headers: headers, timeout: 15)
  rescue RestClient::ExceptionWithResponse => e
    log e.http_code.to_s + " " + e.response.to_s
    return e.response
  end

  log response.code.to_s + " " + response.to_s

  response

end

#post(endpoint, params = {}) ⇒ Object

Use this method to communicate with POST endpoints

Parameters:

  • endpoint (string)

    , e.g. POST /hlr-lookup

  • params (hash) (defaults to: {})

    , a list of parameters to be included in the request

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_hlr_client/client.rb', line 73

def post(endpoint, params = {})

  path = build_connect_url(endpoint)
  headers = build_headers(endpoint, 'POST', params)

  log "POST " + path + " " + params.to_s
  log headers.to_s

  begin
    response = RestClient::Request.execute(method: :post, url: path, payload: params.to_json, headers: headers, timeout: 15)
  rescue RestClient::ExceptionWithResponse => e
    log e.http_code.to_s + " " + e.response.to_s
    return e.response
  end

  log response.code.to_s + " " + response.to_s

  response

end

#put(endpoint, params = {}) ⇒ Object

Use this method to communicate with PUT endpoints

Parameters:

  • endpoint (string)
  • params (hash) (defaults to: {})

    , a list of parameters to be included in the request

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ruby_hlr_client/client.rb', line 98

def put(endpoint, params = {})

  path = build_connect_url(endpoint)
  headers = build_headers(endpoint, 'PUT', params)

  log "PUT " + path + " " + params.to_s
  log headers.to_s

  begin
    response = RestClient::Request.execute(method: :put, url: path, payload: params.to_json, headers: headers, timeout: 15)
  rescue RestClient::ExceptionWithResponse => e
    log e.http_code.to_s + " " + e.response.to_s
    return e.response
  end

  log response.code.to_s + " " + response.to_s

  response

end