Class: VerifiData::SingleAddress

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(street: street_in, city: city_in, state: state_in, zip: zip_in, detail: detail_in) ⇒ SingleAddress

Returns a new instance of SingleAddress.



9
10
11
12
13
14
15
16
17
18
# File 'lib/verifi_data.rb', line 9

def initialize(street: street_in, city: city_in, state: state_in, zip: zip_in, detail: detail_in)
  @street = street
  @city = city
  @state = state
  @zip = zip 
  @detail = detail
  @errors = ActiveModel::Errors.new(self)
  @response = {}

end

Instance Attribute Details

#cityObject (readonly)

Returns the value of attribute city.



7
8
9
# File 'lib/verifi_data.rb', line 7

def city
  @city
end

#detailObject (readonly)

Returns the value of attribute detail.



7
8
9
# File 'lib/verifi_data.rb', line 7

def detail
  @detail
end

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/verifi_data.rb', line 7

def errors
  @errors
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/verifi_data.rb', line 7

def response
  @response
end

#stateObject (readonly)

Returns the value of attribute state.



7
8
9
# File 'lib/verifi_data.rb', line 7

def state
  @state
end

#streetObject (readonly)

Returns the value of attribute street.



7
8
9
# File 'lib/verifi_data.rb', line 7

def street
  @street
end

#zipObject (readonly)

Returns the value of attribute zip.



7
8
9
# File 'lib/verifi_data.rb', line 7

def zip
  @zip
end

Class Method Details

.human_attribute_name(attr, options = {}) ⇒ Object



60
61
62
# File 'lib/verifi_data.rb', line 60

def self.human_attribute_name(attr, options = {})
  attr
end

.lookup_ancestorsObject



64
65
66
# File 'lib/verifi_data.rb', line 64

def self.lookup_ancestors
  [self]
end

Instance Method Details

#api_keyObject



52
53
54
# File 'lib/verifi_data.rb', line 52

def api_key
  ENV["VERIFI_DATA_API_KEY"]
end

#get_urlObject



48
49
50
# File 'lib/verifi_data.rb', line 48

def get_url
  "http://api.verifi.io/v1/validate/address?api_key=#{api_key}&street=#{street}&city=#{city}&state=#{state}&zip=#{zip}&detail=#{detail}"
end

#read_attribute_for_validation(attr) ⇒ Object



56
57
58
# File 'lib/verifi_data.rb', line 56

def read_attribute_for_validation(attr)
  send(attr)
end

#validateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/verifi_data.rb', line 21

def validate
  if api_key.blank?
    errors[:base] << "No API Key Found"
    return {errors: "No API Key Found"}
  end

  url = URI.encode(get_url)

  RestClient.get(url){ |response, request, result, &block|
    @code = response.code
    case response.code
    when 200
      @response = JSON.parse(response)
      #ap @response[0]["components"]
    else
      
      Rails.logger.info {"Error in #{self.class.name} #{@code}"}
      Rails.logger.info { response  }
      return response

    end
  }    

  return response

end