Class: MelissaData::NativeObject::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/melissadata/native_object/base.rb

Direct Known Subclasses

Address, Email, Geo, IpLocator, Name, Phone

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
14
15
16
# File 'lib/melissadata/native_object/base.rb', line 7

def initialize(opts={})
  unless obj.SetLicenseString(license)
    @license = "DEMO"
    puts "Invalid License: Running in DEMO MODE."
  end

  if obj.respond_to?(:InitializeDataFiles) && obj.InitializeDataFiles != 0
    raise obj.GetInitializeErrorString
  end
end

Instance Attribute Details

#data_dirObject



88
89
90
# File 'lib/melissadata/native_object/base.rb', line 88

def data_dir
  @data_dir ||= '/opt/melissadata/data'
end

#inputObject (readonly)

Returns the value of attribute input.



4
5
6
# File 'lib/melissadata/native_object/base.rb', line 4

def input
  @input
end

#objObject (readonly)

Returns the value of attribute obj.



4
5
6
# File 'lib/melissadata/native_object/base.rb', line 4

def obj
  @obj
end

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/melissadata/native_object/base.rb', line 4

def output
  @output
end

#result_codesObject (readonly)

Returns the value of attribute result_codes.



4
5
6
# File 'lib/melissadata/native_object/base.rb', line 4

def result_codes
  @result_codes
end

#results_stringObject (readonly)

Returns the value of attribute results_string.



4
5
6
# File 'lib/melissadata/native_object/base.rb', line 4

def results_string
  @results_string
end

Instance Method Details

#assign_valuesObject



58
59
60
# File 'lib/melissadata/native_object/base.rb', line 58

def assign_values
  @output = {}
end

#licenseObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/melissadata/native_object/base.rb', line 40

def license
  @license ||= begin
    default_filename = '/opt/melissadata/license.txt'

    if File.exists?(default_filename)
      File.read(default_filename).chomp
    elsif (env_license = ENV['MELISSADATA_LICENSE'])
      env_license
    else
      'DEMO'
    end
  end
end

#parse_inputObject



54
55
56
# File 'lib/melissadata/native_object/base.rb', line 54

def parse_input
  raise "Needs to be overridden"
end

#process(opts = {}) ⇒ Object

Example: geo.process(:zip => ‘84108’)

- If invalid input (required fields are missing), return {}
- Call subclass's parse_input - this should call methods on the MelissaData object
- Call subclass's assign_values - pull out information from the object into the output hash
- Processes result codes


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/melissadata/native_object/base.rb', line 23

def process(opts={})
  @input = @defaults.merge(opts)
  return {} unless valid_input?

  parse_input
  @results_string = obj.GetResults.to_s
  assign_values
  process_result_codes

  output.reduce({}) do |hsh,pair|
    val = pair.last
    val.strip! if val.respond_to?(:strip)
    hsh[pair.first] = val if val.present?
    hsh
  end
end

#process_result_codesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/melissadata/native_object/base.rb', line 66

def process_result_codes
  status_messages = []
  error_messages = []

  @results_string.split(",").each do |lookup|
    if (match = @result_codes.detect{ |c,m| c == lookup })
      code, message = *match

      if code[1] == 'E'
        error_messages << message
      else
        status_messages << message
      end
    end
  end

  output[:result_codes] = @results_string
  output[:messages] = {}
  output[:messages][:status] = status_messages if status_messages.present?
  output[:messages][:error] = error_messages if error_messages.present?
end

#valid_input?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/melissadata/native_object/base.rb', line 62

def valid_input?
  @required_fields.blank? || input.any?{ |k,v| @required_fields.include?(k) && v.present? }
end