Class: SgPostcode::ResponseBuilder::JsonOutput

Inherits:
Builder
  • Object
show all
Defined in:
lib/sg_postcode/response/json_output.rb

Instance Attribute Summary

Attributes inherited from Builder

#raw_data

Instance Method Summary collapse

Methods inherited from Builder

#initialize

Constructor Details

This class inherits a constructor from SgPostcode::ResponseBuilder::Builder

Instance Method Details

#dataObject

Implement #data from Builder

Combine all info fields in Config class to the result



7
8
9
10
11
12
13
14
15
# File 'lib/sg_postcode/response/json_output.rb', line 7

def data
  result = {}

  Config.fields.each do |key, value|
    result[key] = digg value
  end

  result
end

#digg(key_path) ⇒ Object

Get value from a key path

@params: key_path - array

Examples:

@raw_data =
  'results' => [
   {
     'address' => [
       {
        'short_name' => '1'
       }
     ]
   }
  ]

 so the key_path will be
 keypath = ['address', 0, 'short_name']

 response = SgPostcode::ResponseBuilder::JsonOutput.new(@raw_data)
 response.data


41
42
43
44
45
# File 'lib/sg_postcode/response/json_output.rb', line 41

def digg(key_path)
  key_path.inject(@raw_data) do |result, key|
    (result.respond_to?(:[]) && (result[key] || {})) || break
  end
end