Class: Pipl::Address

Inherits:
Field
  • Object
show all
Defined in:
lib/pipl/fields.rb

Instance Attribute Summary collapse

Attributes inherited from Field

#current, #inferred, #last_seen, #valid_since

Instance Method Summary collapse

Methods inherited from Field

base_params_from_hash, extra_metadata, from_hash

Methods included from Utils

alnum_chars, alpha_chars, date_to_str, extract_rate_limits, is_valid_url?, str_to_date, titleize, to_utf8

Constructor Details

#initialize(params = {}) ⇒ Address

Returns a new instance of Address.


133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/pipl/fields.rb', line 133

def initialize(params={})
  super params
  @country = params[:country]
  @state = params[:state]
  @city = params[:city]
  @street = params[:street]
  @house = params[:house]
  @apartment = params[:apartment]
  @zip_code = params[:zip_code]
  @po_box = params[:po_box]
  @type = params[:type]
  @raw = params[:raw]
  @display = params[:display]
end

Instance Attribute Details

#apartmentString

Returns Apartment.

Returns:

  • (String)

    Apartment


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#cityString

Returns City.

Returns:

  • (String)

    City


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#countryString

Returns 2 letters country code.

Returns:

  • (String)

    2 letters country code


131
132
133
# File 'lib/pipl/fields.rb', line 131

def country
  @country
end

#displayString

Returns well formatted representation of this address for display purposes.

Returns:

  • (String)

    well formatted representation of this address for display purposes.


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#houseString

Returns House.

Returns:

  • (String)

    House


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#po_boxString

Returns Post Office box number.

Returns:

  • (String)

    Post Office box number


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#rawString

Returns Unparsed address.

Returns:

  • (String)

    Unparsed address.


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#stateString

Returns 2 letters state code.

Returns:

  • (String)

    2 letters state code


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#streetString

Returns Street.

Returns:

  • (String)

    Street


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#typeString

Returns Type of association of this address to a person. One of ‘home`, `work` or `old`.

Returns:

  • (String)

    Type of association of this address to a person. One of ‘home`, `work` or `old`.


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

#zip_codeString

Returns Zip Code.

Returns:

  • (String)

    Zip Code


131
# File 'lib/pipl/fields.rb', line 131

attr_accessor :country, :state, :city, :street, :house, :apartment, :zip_code, :po_box, :type, :raw, :display

Instance Method Details

#country_fullObject


171
172
173
# File 'lib/pipl/fields.rb', line 171

def country_full
  Pipl::COUNTRIES[@country.upcase.to_sym] if @country
end

#is_searchable?Boolean

Returns:

  • (Boolean)

163
164
165
# File 'lib/pipl/fields.rb', line 163

def is_searchable?
  [@raw, @country, @state, @city].any? {|x| ! x.to_s.empty?}
end

#is_sole_searchable?Boolean

Returns:

  • (Boolean)

167
168
169
# File 'lib/pipl/fields.rb', line 167

def is_sole_searchable?
  ! @raw.to_s.empty? || [@city, @street, @house].all? {|x| ! x.to_s.empty?}
end

#is_valid_country?Boolean

Returns:

  • (Boolean)

148
149
150
# File 'lib/pipl/fields.rb', line 148

def is_valid_country?
  @country && Pipl::COUNTRIES.key?(@country.upcase.to_sym)
end

#is_valid_state?Boolean

Returns:

  • (Boolean)

152
153
154
155
# File 'lib/pipl/fields.rb', line 152

def is_valid_state?
  is_valid_country? && Pipl::STATES.key?(@country.upcase.to_sym) and
      @state && Pipl::STATES[@country.upcase.to_sym].key?(@state.upcase.to_sym)
end

#state_fullObject


175
176
177
# File 'lib/pipl/fields.rb', line 175

def state_full
  Pipl::STATES[@country.upcase.to_sym][@state.upcase.to_sym] if is_valid_state?
end

#to_hashObject


157
158
159
160
161
# File 'lib/pipl/fields.rb', line 157

def to_hash
  {country: @country, state: @state, city: @city, street: @street, house: @house, apartment: @apartment,
   zip_code: @zip_code, po_box: @po_box, raw: @raw}
      .reject { |_, value| value.nil? }
end

#to_sObject


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pipl/fields.rb', line 179

def to_s
  return @display if @display

  country = @state ? @country : country_full
  state = @city ? @state : state_full
  vals = [@street, @city, state, country]
  s = vals.any? ? vals.select { |v| v }.join(', ') : ''

  if @street && (@house || @apartment)
    prefix = [@house, @apartment].select { |v| v && ! v.empty? }.join('-')
    s = prefix + ' ' + (s || '')
  end

  if @po_box && @street.nil?
    s = "P.O. Box #{@po_box} " + (s || '')
  end

  s ? Pipl::Utils.to_utf8(s) : ''
end