-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganization.rb
44 lines (35 loc) · 1.15 KB
/
organization.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'rubygems'
require 'data_mapper'
require 'open-uri'
DataMapper::setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/allabolag.sqlite3.db")
class Organization
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
property :orgnum, String
validates_length_of :name, :min => 1
validates_uniqueness_of :name
def self.search_on_web(name)
url = "http://www.allabolag.se/?what=#{CGI.escape(name)}"
doc = Nokogiri::HTML(open(url))
org_elem = doc.css("td.text11grey6").first
orgnum = org_elem && org_elem.text =~ /Org\.nummer: (\d+-\d+)/ && $1 || nil
Organization.new(:name => name, :orgnum => orgnum)
end
def self.search_in_cache(name)
Organization.first(:name => name)
end
def self.search(name)
name = normalize(name)
raise "Enter a valid organizaion name." if name.nil? || name.length == 0
org = search_in_cache(name) || search_on_web(name) unless name.nil?
org.save if org && org.new?
org
end
private
def self.normalize(name)
name.downcase.strip unless name.nil?
end
end
DataMapper.finalize
Organization.auto_upgrade!