datacatalog-importer 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datacatalog-importer}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David James"]
12
- s.date = %q{2010-03-03}
12
+ s.date = %q{2010-03-05}
13
13
  s.description = %q{This framework makes it easier to write importers for the National Data Catalog.}
14
14
  s.email = %q{djames@sunlightfoundation.com}
15
15
  s.extra_rdoc_files = [
@@ -39,7 +39,7 @@ Gem::Specification.new do |s|
39
39
  s.homepage = %q{http://github.com/djsun/datacatalog-importer}
40
40
  s.rdoc_options = ["--charset=UTF-8"]
41
41
  s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.3.5}
42
+ s.rubygems_version = %q{1.3.6}
43
43
  s.summary = %q{A framework to write National Data Catalog importers}
44
44
  s.test_files = [
45
45
  "spec/datacatalog-importer_spec.rb",
data/lib/pusher.rb CHANGED
@@ -34,12 +34,14 @@ module DataCatalog
34
34
 
35
35
  def push_organizations
36
36
  read_data(:organization) do |data|
37
+ link_to_existing_organization!(data, :parent_id)
37
38
  create_or_update_organization(data)
38
39
  end
39
40
  end
40
41
 
41
42
  def push_sources
42
43
  read_data(:source) do |data|
44
+ link_to_existing_organization!(data, :organization_id)
43
45
  source = create_or_update_source(data)
44
46
  data[:downloads].each do |download_data|
45
47
  create_or_update_download(source, download_data)
@@ -68,25 +70,42 @@ module DataCatalog
68
70
  # ---
69
71
 
70
72
  def create_or_update_organization(data)
71
- docs = DataCatalog::Organization.all(:url => data[:url])
73
+ url, name = data[:url], data[:name]
74
+ raise "#{name} has blank URL" if url.blank?
75
+ docs = DataCatalog::Organization.all(:url => url)
72
76
  n = docs.length
73
77
  case n
74
78
  when 0
75
- puts "Creating Organization: #{data[:name]}"
79
+ puts "Creating Organization: #{name}"
76
80
  DataCatalog::Organization.create(data)
77
81
  when 1
78
- puts "Updating Organization: #{data[:name]}"
82
+ puts "Updating Organization: #{name}"
79
83
  DataCatalog::Organization.update(docs[0].id, data)
80
84
  else
81
85
  multiple_matches("Organization", n, data)
82
86
  end
83
87
  end
84
88
 
89
+ def find_organization_by(field, name)
90
+ docs = DataCatalog::Organization.all(field => name)
91
+ n = docs.length
92
+ case n
93
+ when 0
94
+ nil
95
+ when 1
96
+ docs[0]
97
+ else
98
+ multiple_matches("Organization", n, { field => name })
99
+ nil
100
+ end
101
+ end
102
+
85
103
  # Important: do not modify data
86
104
  def create_or_update_source(data)
87
105
  data = data.reject do |key, value|
88
106
  [:organization, :downloads].include?(key)
89
107
  end
108
+ puts "data[:url] : #{data[:url]}"
90
109
  docs = DataCatalog::Source.all(:url => data[:url])
91
110
  n = docs.length
92
111
  case n
@@ -121,6 +140,28 @@ module DataCatalog
121
140
  end
122
141
  end
123
142
 
143
+ # Try to link to an existing organization, first by
144
+ # using an URL, then by name.
145
+ #
146
+ # Note: modifies data (that is why I use the !)
147
+ def link_to_existing_organization!(data, organization_id_key)
148
+ organization_hash = data.delete(:organization)
149
+ raise "Could not find :organization key" unless organization_hash
150
+ name = organization_hash[:name]
151
+ url = organization_hash[:url]
152
+ organization = if url
153
+ find_organization_by(:url, url)
154
+ elsif name
155
+ find_organization_by(:name, name)
156
+ end
157
+
158
+ if organization
159
+ data[organization_id_key] = organization.id
160
+ else
161
+ puts " - Could not find organization named: #{name}"
162
+ end
163
+ end
164
+
124
165
  # ---
125
166
 
126
167
  def multiple_matches(model, n, data)
data/lib/utility.rb CHANGED
@@ -28,7 +28,7 @@ module DataCatalog
28
28
 
29
29
  def self.headers
30
30
  {
31
- "UserAgent" => "National Data Catalog Importer/0.1.5",
31
+ "UserAgent" => "National Data Catalog Importer/0.1.6",
32
32
  }
33
33
  end
34
34
 
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datacatalog-importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 6
9
+ version: 0.1.6
5
10
  platform: ruby
6
11
  authors:
7
12
  - David James
@@ -9,29 +14,37 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-03 00:00:00 -05:00
17
+ date: 2010-03-05 00:00:00 -05:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: nokogiri
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 4
30
+ - 1
23
31
  version: 1.4.1
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: rspec
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 2
44
+ - 9
33
45
  version: 1.2.9
34
- version:
46
+ type: :development
47
+ version_requirements: *id002
35
48
  description: This framework makes it easier to write importers for the National Data Catalog.
36
49
  email: djames@sunlightfoundation.com
37
50
  executables: []
@@ -73,18 +86,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
86
  requirements:
74
87
  - - ">="
75
88
  - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
76
91
  version: "0"
77
- version:
78
92
  required_rubygems_version: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
82
98
  version: "0"
83
- version:
84
99
  requirements: []
85
100
 
86
101
  rubyforge_project:
87
- rubygems_version: 1.3.5
102
+ rubygems_version: 1.3.6
88
103
  signing_key:
89
104
  specification_version: 3
90
105
  summary: A framework to write National Data Catalog importers