Class: Greip::GreipHandler

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

Overview

GreipHandler class

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ GreipHandler

Returns a new instance of GreipHandler.



24
25
26
# File 'lib/greip.rb', line 24

def initialize(token)
  @token = token
end

Instance Method Details

#asn(options) ⇒ Object

Raises:

  • (StandardError)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/greip.rb', line 221

def asn(options)
  asn = (options[:asn] || "").upcase
  mode = (options[:mode] || "live").downcase

  # Validate the asn variable
  raise StandardError, ErrorMessages::ASN if asn.empty?

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI("#{BASE_URL}/ASNLookup?asn=#{asn}&mode=#{mode}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#bulk_lookup(options) ⇒ Object

Raises:

  • (StandardError)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/greip.rb', line 99

def bulk_lookup(options)
  ips = options[:ips] || []
  params = options[:params] || []
  lang = (options[:lang] || "EN").upcase
  mode = (options[:mode] || "live").downcase

  ips = [] unless ips.is_a?(Array)

  # Validate the ips variable
  raise StandardError, ErrorMessages::IPS if ips.empty?

  ips.each do |ip|
    raise StandardError, ErrorMessages::VALID_IPS if ip.length < 7
  end

  # Validate the params variable items
  params.each do |param|
    if !param.empty? && !AVAILABLE_GEOIP_PARAMS.include?(param)
      raise StandardError, "The '#{param}' module you used is unknown.\n" \
                           "You can use: `location`, `security`, `timezone`, `currency`, and/or `device`."
    end
  end

  # Validate the lang variable
  !AVAILABLE_LANGUAGES.include?(lang) && (raise StandardError, ErrorMessages::LANG)

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI("#{BASE_URL}/BulkLookup?ips=#{ips.join(",")}&params=#{params.join(",")}&lang=#{lang}&mode=#{mode}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#country(options) ⇒ Object

Raises:

  • (StandardError)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/greip.rb', line 147

def country(options)
  country_code = options[:countryCode] || ""
  params = options[:params] || []
  lang = (options[:lang] || "EN").upcase
  mode = (options[:mode] || "live").downcase

  # Validate the params variable items
  params.each do |param|
    next unless param.empty? || !AVAILABLE_COUNTRY_PARAMS.include?(param)

    raise StandardError, "The '#{param}' module you used is unknown.\n" \
                         "You can use: `language`, `flag`, `currency`, and/or `timezone`.\n" \
                         "Read more at: https://docs.greip.io/api-reference/endpoint/other-services/country-data"
  end

  # Validate the lang variable
  raise StandardError, ErrorMessages::LANG if !AVAILABLE_LANGUAGES.include?(lang)

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI("#{BASE_URL}/Country?CountryCode=#{country_code}&params=#{params.join(",")}&lang=#{lang}&mode=#{mode}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#email_validation(options) ⇒ Object

Raises:

  • (StandardError)


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/greip.rb', line 250

def email_validation(options)
  email = (options[:email] || "").downcase
  mode = (options[:mode] || "live").downcase

  # Validate the email variable
  raise StandardError, ErrorMessages::EMAIL if email.empty?

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI("#{BASE_URL}/validateEmail?email=#{email}&mode=#{mode}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#iban_validation(options) ⇒ Object

Raises:

  • (StandardError)


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/greip.rb', line 309

def iban_validation(options)
  iban = (options[:iban] || "").upcase
  mode = (options[:mode] || "live").downcase

  # Validate the iban variable
  raise StandardError, "You should pass the `iban` parameter." if iban.empty?

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI("#{BASE_URL}/validateIBAN?iban=#{iban}&mode=#{mode}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#lookup(options) ⇒ Object

Raises:

  • (StandardError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/greip.rb', line 28

def lookup(options)
  ip = options[:ip] || ""
  params = options[:params] || []
  lang = (options[:lang] || "EN").upcase
  mode = (options[:mode] || "live").downcase

  # Validate the ip variable
  raise StandardError, ErrorMessages::IP unless ip.length > 5

  # Validate the params variable items
  params.each do |per_param|
    if !per_param.empty? && !AVAILABLE_GEOIP_PARAMS.include?(per_param)
      raise StandardError, "The '#{per_param}' module you used is unknown.\n" \
                           "You can use: `location`, `security`, `timezone`, `currency`, and/or `device`."
    end
  end

  # Validate the lang variable
  !AVAILABLE_LANGUAGES.include?(lang) && (raise StandardError, ErrorMessages::LANG)

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI(BASE_URL + "/IPLookup?ip=#{ip}&params=#{params.join(",")}&lang=#{lang}&mode=#{mode}&source=Ruby-Gem")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#payment_fraud(options) ⇒ Object

Raises:

  • (StandardError)


338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/greip.rb', line 338

def payment_fraud(options)
  data = options[:data] || []
  mode = (options[:mode] || "live").downcase

  # Validate the mode variable
  raise StandardError, ErrorMessages::MODE unless %w[live test].include?(mode)

  # Validate the data variable
  raise StandardError, ErrorMessages::DATA unless data.is_a?(Hash) && !data.empty?

  url = URI("#{BASE_URL}/paymentFraud")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Bearer #{@token}"
  request.body = data.to_json

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)
  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#phone_validation(options) ⇒ Object

Raises:

  • (StandardError)


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/greip.rb', line 279

def phone_validation(options)
  phone = options[:phone] || ""
  country_code = (options[:countryCode] || "").upcase
  mode = (options[:mode] || "live").downcase

  # Validate the phone and countryCode variables
  (phone.empty? || country_code.empty?) && (raise StandardError, ErrorMessages::COUNTRY_CODE_PHONE)

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI("#{BASE_URL}/validatePhone?phone=#{phone}&countryCode=#{country_code}&mode=#{mode}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#profanity(options) ⇒ Object

Raises:

  • (StandardError)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/greip.rb', line 187

def profanity(options)
  text = options[:text] || ""
  params = options[:params] || []
  lang = (options[:lang] || "EN").upcase
  mode = (options[:mode] || "live").downcase

  # Validate the text variable
  raise StandardError, ErrorMessages::TEXT if text.empty?

  # Validate the lang variable
  raise StandardError, ErrorMessages::LANG if !AVAILABLE_LANGUAGES.include?(lang)

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI("#{BASE_URL}/badWords?text=#{text}&params=#{params.join(",")}&format=JSON&lang=#{lang}&mode=#{mode}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end

#threats(options) ⇒ Object

Raises:

  • (StandardError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/greip.rb', line 70

def threats(options)
  ip = options[:ip] || ""
  mode = (options[:mode] || "live").downcase

  # Validate the ip variable
  raise StandardError, ErrorMessages::IP unless ip.length > 5

  # Validate the mode variable
  mode != "live" && mode != "test" && (raise StandardError, ErrorMessages::MODE)

  url = URI(BASE_URL + "/threats?ip=#{ip}&mode=#{mode}&source=Ruby-Gem")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https")

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{@token}"

  response = http.request(request)

  raise StandardError, "Error: #{response.code} - #{response.message}" unless response.code == "200"

  parsed_response = JSON.parse(response.body)

  raise StandardError, "Error: #{parsed_response["description"]}" unless parsed_response["status"] == "success"

  parsed_response["data"]
end