Class: Melissa::GeoPointLive

Inherits:
GeoPoint show all
Defined in:
lib/melissa/geo_point_live.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GeoPoint

add_callback, #latitude, #longitude, #time_zone_offset, #valid?

Constructor Details

#initialize(opts) ⇒ GeoPointLive

Returns a new instance of GeoPointLive.



94
95
96
97
98
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
# File 'lib/melissa/geo_point_live.rb', line 94

def initialize(opts)
  @is_valid = false

  self.class.with_mdgeo do |mdGeo|
    if opts.kind_of?(AddrObj)
      mdGeoGeoPoint(mdGeo, opts.zip || '', opts.plus4 || '', opts.delivery_point_code || '')
    elsif opts.kind_of?(Hash)
      mdGeoGeoPoint(mdGeo, opts[:zip] || '', opts[:plus4] || '', opts[:delivery_point_code] || '')
    else
      raise "Invalid call to GeoPoint, unknown object #{opts.inspect}"
    end
    @resultcodes = mdGeoGetResults(mdGeo).split(',')
    fatals = @resultcodes & @@fatal_codes
    @is_valid = fatals.blank?
    if @is_valid
      fill_attributes(mdGeo)
      # Convert from strings to actual types
      if @latitude.blank?
        @latitude = nil
      else
        @latitude = @latitude.to_f
      end
      if @longitude.blank?
        @longitude = nil
      else
        @longitude = @longitude.to_f
      end
      if @latitude == 0.0 && @longitude == 0.0
        @latitude = nil
        @longitude = nil
        @is_valid = false
      end
    else
      fatals.each do |fatal_code|
        raise "FATAL ERROR Melissa GeoPoint returned #{fatal_code}-#{@@codes[fatal_code]}"
      end
    end
  end

  @@callbacks.each do |callback|
    callback.call
  end
end

Class Method Details

.days_until_data_expirationObject



88
89
90
91
92
# File 'lib/melissa/geo_point_live.rb', line 88

def self.days_until_data_expiration
  #I compare Date objects. I think it is more accurate.
  #self.license_expiration_date returns string in format: "YYYY-MM-DD"
  (self.expiration_date - Date.today).to_i
end

.days_until_license_expirationObject



75
76
77
78
79
# File 'lib/melissa/geo_point_live.rb', line 75

def self.days_until_license_expiration
  #I compare Date objects. I think it is more accurate.
  #self.license_expiration_date returns string in format: "YYYY-MM-DD"
  (self.license_expiration_date - Date.today).to_i
end

.expiration_dateObject

his function returns a date value representing the date when the current data files expire. This date enables you to confirm that the data files you are using are the latest available.



84
85
86
# File 'lib/melissa/geo_point_live.rb', line 84

def self.expiration_date
  Date.parse(with_mdgeo { |mdGeo| mdGeoGetExpirationDate(mdGeo)})
end

.lib_loaded?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
# File 'lib/melissa/geo_point_live.rb', line 6

def self.lib_loaded?
  return @lib_loaded if defined?(@lib_loaded)
  extend FFI::Library

  ffi_lib Melissa.config.geo_point_lib
  attr_functions = @@melissa_attributes.map { |name| ["mdGeoGet#{name}".to_sym, [:pointer], :string] }

  functions = attr_functions + [
      # method # parameters        # return
      [:mdGeoCreate, [], :pointer],
      [:mdGeoSetLicenseString, [:pointer, :string], :int],
      [:mdGeoSetPathToGeoCodeDataFiles, [:pointer, :string], :void],
      [:mdGeoSetPathToGeoPointDataFiles, [:pointer, :string], :void],
      [:mdGeoInitializeDataFiles, [:pointer], :int],
      [:mdGeoGeoPoint, [:pointer, :string, :string, :string], :int],
      [:mdGeoGetResults, [:pointer], :string],
      [:mdGeoDestroy, [:pointer], :void],
      [:mdGeoGetLicenseExpirationDate, [:pointer], :string],
      [:mdGeoGetExpirationDate, [:pointer], :string],
  ]

  functions.each do |func|
    begin
      attach_function(*func)
    rescue Object => e
      raise "Could not attach #{func}, #{e.message}"
    end
  end

  attr_reader *@@melissa_attributes.map { |name| name.underscore.to_sym }

  # Get all the attributes out up-front so we can destroy the mdGeo object
  class_eval <<-EOS
    define_method(:fill_attributes) do |mdGeo|
      #{@@melissa_attributes.map { |name| "@#{name.underscore} = mdGeoGet#{name}(mdGeo)" }.join("\n")}
    end
  EOS
rescue LoadError => e
  puts "WARNING: #{Melissa.config.geo_point_lib} could not be loaded"
  return @lib_loaded = false
else
  return @lib_loaded = true
end

.license_expiration_dateObject

This function returns a date value corresponding to the date when the current license string expires.



71
72
73
# File 'lib/melissa/geo_point_live.rb', line 71

def self.license_expiration_date
  Date.parse(with_mdgeo { |mdGeo| mdGeoGetLicenseExpirationDate(mdGeo) })
end

.with_mdgeoObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/melissa/geo_point_live.rb', line 50

def self.with_mdgeo
  raise "Unable to load melissa library #{Melissa.config.addr_obj_lib}" unless self.lib_loaded?
  raise "Unable to find the license for Melissa Data library #{Melissa.config.license}" unless Melissa.config.license.present?
  raise "Unable to find data files for Melissa Data library #{Melissa.config.data_path}" unless Melissa.config.data_path.present?
  begin
    mdGeo = mdGeoCreate
    mdGeoSetLicenseString(mdGeo, Melissa.config.license)
    mdGeoSetPathToGeoCodeDataFiles(mdGeo, Melissa.config.data_path)
    mdGeoSetPathToGeoPointDataFiles(mdGeo, Melissa.config.data_path)
    result = mdGeoInitializeDataFiles(mdGeo)
    if result != 0
      raise mdGeoGetInitializeErrorString(mdGeo)
    end
    yield mdGeo
  ensure
    mdGeoDestroy(mdGeo) if mdGeo
  end
end