Module: Difi

Included in:
Brreg::Registry, Posten
Defined in:
lib/difi.rb,
lib/difi/version.rb

Overview

General methods for working with the public APIs at hotell.difi.no

This module is meant to extend a Difi registry class.

For instance the Brreg::Enhetsregisteret class

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#connObject

The API connection



20
21
22
23
24
25
26
# File 'lib/difi.rb', line 20

def conn
  @@conn ||= Faraday.new(:url => @@url) do |faraday|
    faraday.request   :url_encoded            # form-encode POST params
    faraday.response  :logger                 # log requests to STDOUT
    faraday.adapter   :excon                  # make requests with Excon
  end
end

#datasetObject

The current dataset



29
30
31
# File 'lib/difi.rb', line 29

def dataset
  self.to_s.downcase.sub("::", "/")
end

#fieldsObject

Return the fields of the current dataset



39
40
41
42
# File 'lib/difi.rb', line 39

def fields
  get "/fields"
  json
end

#get(query) ⇒ Object

Perform a GET HTTP query on the current dataset



34
35
36
# File 'lib/difi.rb', line 34

def get(query)
  @@response = conn.get("#{url}/#{dataset}#{query}")
end

#jsonObject

Return the last HTTP response body as JSON



57
58
59
# File 'lib/difi.rb', line 57

def json
  JSON.parse @@response.body
end

#responseObject

Return the last HTTP response



52
53
54
# File 'lib/difi.rb', line 52

def response
  @@response
end

#search(fields) ⇒ Object

Search for entries from the current dataset with the given fields Input is expected to be a Hash



46
47
48
49
# File 'lib/difi.rb', line 46

def search(fields)
  get "?" + URI.escape(fields.collect{|k,v| "#{k}=#{v}"}.join('&'))
  json
end

#urlObject

The API source URL



15
16
17
# File 'lib/difi.rb', line 15

def url
  @@url ||= "http://hotell.difi.no/api/json/"
end