Class: BriteAPI::Contact
- Inherits:
-
Object
- Object
- BriteAPI::Contact
- Defined in:
- lib/brite-api/contact.rb
Constant Summary collapse
- FIELDS =
[:email, :phone, :address, :name, :ip]
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #error_codes ⇒ Object
- #errors ⇒ Object
-
#initialize(api_key, options = {}, data = {}) ⇒ Contact
constructor
A new instance of Contact.
- #status ⇒ Object
- #valid? ⇒ Boolean
- #verify! ⇒ Object
Constructor Details
#initialize(api_key, options = {}, data = {}) ⇒ Contact
Returns a new instance of Contact.
19 20 21 22 23 24 25 26 27 |
# File 'lib/brite-api/contact.rb', line 19 def initialize(api_key, = {}, data = {}) raise ArgumentError, "Missing BriteVerify API key" if api_key.nil? || api_key == '' @api_key = api_key @options = @data = {} FIELDS.each { |key| @data[key] = data[key] || data[key.to_s] } @response = {} end |
Instance Attribute Details
#response ⇒ Object (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_codes ⇒ Object
80 81 82 |
# File 'lib/brite-api/contact.rb', line 80 def error_codes @response.map{ |_, data| data['error_code'] }.compact.uniq end |
#errors ⇒ Object
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 |
#status ⇒ Object
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
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 |