Module: Dcentralized

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

Defined Under Namespace

Classes: InvalidPostcodeException

Constant Summary collapse

VERSION =
'0.0.7'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

.api_urlObject

Returns the value of attribute api_url.



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

def api_url
  @api_url
end

Class Method Details

.auto_complete(zipcode, house_number = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dcentralized.rb', line 12

def self.auto_complete(zipcode, house_number = nil)
  # 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: 'json')
  params.merge!(streetnumber: house_number) if house_number

  # Perform request
  response = JSON.parse(RestClient.get "#{@api_url}/autocomplete", {params: params})

  if response["status"] == "ok"
    return OpenStruct.new response["results"].first
  elsif response["status"] == "error"
    raise InvalidPostcodeException, response["error"]["message"]
  else
    raise Exception.new("Unknown exception in Pro6PP auto_complete response")
  end
end

.configure(&blk) ⇒ Object



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

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

.format_zipcode(zipcode = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/dcentralized.rb', line 33

def self.format_zipcode(zipcode = nil)
  if zipcode.nil? || zipcode == ""
    raise InvalidPostcodeException.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 InvalidPostcodeException.new("Wrong zipcode format (1234AB format expected)")
  else
    zipcode.gsub(" ", "").upcase
  end
end

.stringify(obj) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/dcentralized.rb', line 43

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



51
52
53
# File 'lib/dcentralized.rb', line 51

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