Class: MelissaData::NativeObject::Base
- Inherits:
-
Object
- Object
- MelissaData::NativeObject::Base
- Defined in:
- lib/melissadata/native_object/base.rb
Instance Attribute Summary collapse
- #data_dir ⇒ Object
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#obj ⇒ Object
readonly
Returns the value of attribute obj.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#result_codes ⇒ Object
readonly
Returns the value of attribute result_codes.
-
#results_string ⇒ Object
readonly
Returns the value of attribute results_string.
Instance Method Summary collapse
- #assign_values ⇒ Object
-
#initialize(opts = {}) ⇒ Base
constructor
A new instance of Base.
- #license ⇒ Object
- #parse_input ⇒ Object
-
#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.
- #process_result_codes ⇒ Object
- #valid_input? ⇒ Boolean
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_dir ⇒ Object
88 89 90 |
# File 'lib/melissadata/native_object/base.rb', line 88 def data_dir @data_dir ||= '/opt/melissadata/data' end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
4 5 6 |
# File 'lib/melissadata/native_object/base.rb', line 4 def input @input end |
#obj ⇒ Object (readonly)
Returns the value of attribute obj.
4 5 6 |
# File 'lib/melissadata/native_object/base.rb', line 4 def obj @obj end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
4 5 6 |
# File 'lib/melissadata/native_object/base.rb', line 4 def output @output end |
#result_codes ⇒ Object (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_string ⇒ Object (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_values ⇒ Object
58 59 60 |
# File 'lib/melissadata/native_object/base.rb', line 58 def assign_values @output = {} end |
#license ⇒ Object
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_input ⇒ Object
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_codes ⇒ Object
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 = [] = [] @results_string.split(",").each do |lookup| if (match = @result_codes.detect{ |c,m| c == lookup }) code, = *match if code[1] == 'E' << else << end end end output[:result_codes] = @results_string output[:messages] = {} output[:messages][:status] = if .present? output[:messages][:error] = if .present? end |
#valid_input? ⇒ 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 |