Class: Melissa::AddrObjLive

Inherits:
AddrObj
  • Object
show all
Defined in:
lib/melissa/addr_obj_live.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AddrObj

add_callback, #address_struct, #delivery_point, #time_zone_offset

Constructor Details

#initialize(opts) ⇒ AddrObjLive

Returns a new instance of AddrObjLive.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/melissa/addr_obj_live.rb', line 99

def initialize(opts)
  self.class.with_mdaddr do |h_addr_lib|
    # clear any properties from a previous call
    mdAddrClearProperties(h_addr_lib)

    mdAddrSetCompany(h_addr_lib, opts[:company] || '');
    mdAddrSetAddress(h_addr_lib, opts[:address] || '');
    mdAddrSetAddress2(h_addr_lib, opts[:address2] || '');
    mdAddrSetSuite(h_addr_lib, opts[:suite] || '');
    mdAddrSetCity(h_addr_lib, opts[:city] || '');
    mdAddrSetState(h_addr_lib, opts[:state] || '');
    mdAddrSetZip(h_addr_lib, opts[:zip] || '');
    mdAddrSetUrbanization(h_addr_lib, opts[:urbanization] || '');
    mdAddrSetCountryCode(h_addr_lib, opts[:country_code] || '');
    mdAddrVerifyAddress(h_addr_lib);

    @resultcodes = mdAddrGetResults(h_addr_lib).split(',')
    fill_attributes(h_addr_lib)
  end
  @@callbacks.each do |callback|
    callback.call
  end
end

Class Method Details

.data_expiration_dateObject

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



91
92
93
# File 'lib/melissa/addr_obj_live.rb', line 91

def self.data_expiration_date
  Date.strptime(with_mdaddr { |h_addr_lib| mdAddrGetExpirationDate(h_addr_lib) }, '%m-%d-%Y')
end

.days_until_data_expirationObject



95
96
97
# File 'lib/melissa/addr_obj_live.rb', line 95

def self.days_until_data_expiration
  (self.data_expiration_date - Date.today).to_i
end

.days_until_license_expirationObject



81
82
83
84
85
# File 'lib/melissa/addr_obj_live.rb', line 81

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

.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
49
50
51
52
53
54
55
56
57
# File 'lib/melissa/addr_obj_live.rb', line 6

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

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

  functions = attr_functions + [
      # method # parameters        # return
      [:mdAddrCreate, [], :pointer],
      [:mdAddrSetLicenseString, [:pointer, :string], :int],
      [:mdAddrSetPathToUSFiles, [:pointer, :string], :void],
      [:mdAddrInitializeDataFiles, [:pointer], :int],
      [:mdAddrClearProperties, [:pointer], :void],
      [:mdAddrSetCompany, [:pointer, :string], :void],
      [:mdAddrSetAddress, [:pointer, :string], :void],
      [:mdAddrSetAddress2, [:pointer, :string], :void],
      [:mdAddrSetSuite, [:pointer, :string], :void],
      [:mdAddrSetCity, [:pointer, :string], :void],
      [:mdAddrSetState, [:pointer, :string], :void],
      [:mdAddrSetZip, [:pointer, :string], :void],
      [:mdAddrSetUrbanization, [:pointer, :string], :void],
      [:mdAddrSetCountryCode, [:pointer, :string], :void],
      [:mdAddrVerifyAddress, [:pointer], :int],
      [:mdAddrGetResults, [:pointer], :string],
      [:mdAddrDestroy, [:pointer], :void],
      [:mdAddrGetLicenseExpirationDate, [:pointer], :string],
      [:mdAddrGetExpirationDate, [: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 h_addr_lib object
  class_eval <<-EOS
    define_method(:fill_attributes) do |h_addr_lib|
      #{@@melissa_attributes.map { |name| "@#{name.underscore} = mdAddrGet#{name}(h_addr_lib)" }.join("\n")}
    end
  EOS
rescue LoadError => e
  puts "WARNING: #{Melissa.config.addr_obj_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.



77
78
79
# File 'lib/melissa/addr_obj_live.rb', line 77

def self.license_expiration_date
  Date.parse(with_mdaddr { |h_addr_lib| mdAddrGetLicenseExpirationDate(h_addr_lib) })
end

.with_mdaddrObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/melissa/addr_obj_live.rb', line 59

def self.with_mdaddr
  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
    h_addr_lib = mdAddrCreate
    mdAddrSetLicenseString(h_addr_lib, Melissa.config.license)
    mdAddrSetPathToUSFiles(h_addr_lib, Melissa.config.data_path)
    mdAddrInitializeDataFiles(h_addr_lib)
    yield h_addr_lib
  ensure
    mdAddrDestroy(h_addr_lib) if h_addr_lib
  end
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


123
124
125
126
# File 'lib/melissa/addr_obj_live.rb', line 123

def valid?
  # Make sure there is at least 1 good code and no bad codes
  (@resultcodes & @@good_codes).present? && (@resultcodes & @@bad_codes).empty?
end