Class: BriteAPI::Contact

Inherits:
Object
  • Object
show all
Defined in:
lib/brite-api/contact.rb

Constant Summary collapse

FIELDS =
[:email, :phone, :address, :name, :ip]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}, data = {}) ⇒ Contact

Returns a new instance of Contact.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
# File 'lib/brite-api/contact.rb', line 19

def initialize(api_key, options = {}, data = {})
  raise ArgumentError, "Missing BriteVerify API key" if api_key.nil? || api_key == ''
  @api_key = api_key
  @options = options
  @data = {}
  FIELDS.each { |key| @data[key] = data[key] || data[key.to_s] }

  @response = {}
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/brite-api/contact.rb', line 5

def response
  @response
end

Instance Method Details

#error_codesObject



80
81
82
# File 'lib/brite-api/contact.rb', line 80

def error_codes
  @response.map{ |_, data| data['error_code'] }.compact.uniq
end

#errorsObject



72
73
74
75
76
77
78
# File 'lib/brite-api/contact.rb', line 72

def errors
  err = {}
  @response.each do |key, data|
    err[key] = data['error'] if data['error']
  end
  err
end

#statusObject



63
64
65
66
67
68
69
70
# File 'lib/brite-api/contact.rb', line 63

def status
  return if @response == {}
  states = ['valid', 'unknown', 'invalid']
  states.each_with_index do |state, index|
    return state if @response.all?{ |_, data| states[0, index + 1].include? data['status'] }
  end
  'unknown'
end

#valid?Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/brite-api/contact.rb', line 58

def valid?
  return if @response == {}
  @response.all?{ |_, data| data['status'] == 'valid' }
end

#verify!Object



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
# File 'lib/brite-api/contact.rb', line 29

def verify!
  @response = {}

  data = @data.select{ |_, v| v != nil }

  if data.size == 1
    # optimization for single-threaded requests
    data.each do |k, v|
      klass = BriteAPI.const_get(k.to_s.capitalize + "APIClient")
      @response[k] = klass.new(@api_key, @options).verify(v)
    end
  else
    # send async requests
    threads = []
    data.each do |key, value|

      threads << Thread.new(key, value) do |k, v|
        klass = BriteAPI.const_get(k.to_s.capitalize + "APIClient")
        @response[k] = klass.new(@api_key, @options).verify(v)
      end
    end

    threads.each { |thr| thr.join }
  end


  self
end