Module: Dcentralized

Defined in:
lib/dcentralized.rb,
lib/dcentralized/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/dcentralized.rb', line 9

def api_key
  @api_key
end

.api_urlObject

Returns the value of attribute api_url.



9
10
11
# File 'lib/dcentralized.rb', line 9

def api_url
  @api_url
end

Class Method Details

.auto_complete(zipcode) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dcentralized.rb', line 13

def self.auto_complete(zipcode)
  # Format the zipcode
  zipcode = format_zipcode(zipcode)
  # Setup request
  params  = {auth_key: @api_key} 
  zipcode.length == 6 ? params.merge!(nl_sixpp: zipcode) : params.merge!(nl_fourpp: zipcode)
  params.merge!(format: 'xml', pretty: 'True')
  # Perform request
  response = RestClient.get "#{@api_url}/autocomplete", {params: params}
  hash     = XmlSimple.xml_in(response)["results"][0]["result"][0].symbolize_keys(true)
  # Return openstruct output
  OpenStruct.new stringify(hash)
end

.configure(&blk) ⇒ Object



10
# File 'lib/dcentralized.rb', line 10

def configure(&blk); class_eval(&blk); end

.format_zipcode(zipcode = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/dcentralized.rb', line 27

def self.format_zipcode(zipcode = nil)
  if zipcode.nil? || zipcode == ""
    raise Exception.new("No zipcode provided")
  elsif (zipcode =~ /^[0-9]{4}$|[0-9]{4}[\ ]{1}[a-zA-Z]{2}$|[0-9]{4}[a-zA-Z]{2}$/).nil?
    raise Exception.new("Wrong zipcode format (1234AB format expected)")
  else
    zipcode.gsub(" ", "").upcase
  end
end

.stringify(obj) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/dcentralized.rb', line 37

def self.stringify(obj)
  if obj.is_a? Array
    obj.join
  elsif obj.is_a? Hash
    obj.merge( obj ) {|k, val| stringify val }
  end
end

.version_stringObject



45
46
47
# File 'lib/dcentralized.rb', line 45

def self.version_string
  "Dcentralized version #{Dcentralized::VERSION}"
end