hdo-storting-importer 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -14,3 +14,4 @@ spec/reports
14
14
  test/tmp
15
15
  test/version_tmp
16
16
  tmp
17
+ .tm_properties
@@ -1,10 +1,23 @@
1
1
  module Hdo
2
2
  module StortingImporter
3
3
  class ApiDataSource < DataSource
4
- USER_AGENT = "holderdeord-storting-importer"
4
+ USER_AGENT = "holderdeord-storting-importer v#{Hdo::StortingImporter::VERSION}"
5
5
 
6
- def initialize(url)
7
- @resource = RestClient::Resource.new(URI.parse(url), )
6
+ def self.default
7
+ new "http://data.stortinget.no"
8
+ end
9
+
10
+ def initialize(url_or_resource)
11
+ @resource = case url_or_resource
12
+ when RestClient::Resource
13
+ url_or_resource
14
+ when URI
15
+ RestClient::Resource.new(url_or_resource)
16
+ when String
17
+ RestClient::Resource.new(URI.parse(url_or_resource))
18
+ else
19
+ raise ArgumentError, "invalid argument #{url_or_resource.inspect}"
20
+ end
8
21
  end
9
22
 
10
23
  def representatives(period = DEFAULT_PERIOD)
@@ -0,0 +1,78 @@
1
+ module Hdo
2
+ module StortingImporter
3
+ class CachingDataSource < DataSource
4
+ def initialize(delegate, cache = InMemoryCache.new)
5
+ @delegate = delegate
6
+ @cache = cache
7
+ end
8
+
9
+ def representatives(period = DEFAULT_PERIOD)
10
+ cache :representatives, period do
11
+ @delegate.representatives(period)
12
+ end
13
+ end
14
+
15
+ def representatives_today
16
+ cache :representatives_today do
17
+ @delegate.representatives_today
18
+ end
19
+ end
20
+
21
+ def parties(session_id = DEFAULT_SESSION)
22
+ cache :parties, session_id do
23
+ @delegate.parties(session_id)
24
+ end
25
+ end
26
+
27
+ def committees(session_id = DEFAULT_SESSION)
28
+ cache :committees, session_id do
29
+ @delegate.committees session_id
30
+ end
31
+ end
32
+
33
+ def districts
34
+ cache :districts do
35
+ @delegate.districts
36
+ end
37
+ end
38
+
39
+ def categories
40
+ cache :categories do
41
+ @delegate.categories
42
+ end
43
+ end
44
+
45
+ def issues(session_id = DEFAULT_SESSION)
46
+ cache :issues, session_id do
47
+ @delegate.issues session_id
48
+ end
49
+ end
50
+
51
+ def votes_for(issue_id)
52
+ cache :votes_for, issue_id do
53
+ @delegate.votes_for issue_id
54
+ end
55
+ end
56
+
57
+ def propositions_for(vote_id)
58
+ cache :propositions_for, vote_id do
59
+ @delegate.propositions_for vote_id
60
+ end
61
+ end
62
+
63
+ def vote_results_for(vote_id)
64
+ cache :vote_results_for, vote_id do
65
+ @delegate.vote_results_for vote_id
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def cache(*args, &blk)
72
+ @cache.fetch(args, &blk)
73
+ end
74
+
75
+ end
76
+ end
77
+ end
78
+
@@ -0,0 +1,13 @@
1
+ module Hdo
2
+ module StortingImporter
3
+ class InMemoryCache
4
+ def initialize
5
+ @cache = {}
6
+ end
7
+
8
+ def fetch(key, &blk)
9
+ @cache[key] ||= yield
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  module Hdo
2
2
  module StortingImporter
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
@@ -104,17 +104,22 @@ module Hdo
104
104
  end
105
105
  end
106
106
 
107
+ require 'hdo/storting_importer/version'
108
+
107
109
  require 'hdo/storting_importer/core_ext/enumerable'
108
110
  require 'hdo/storting_importer/ivar_equality'
109
111
  require 'hdo/storting_importer/inspectable'
110
112
  require 'hdo/storting_importer/has_json_schema'
111
113
  require 'hdo/storting_importer/util'
112
114
  require 'hdo/storting_importer/fusion_table'
115
+ require 'hdo/storting_importer/in_memory_cache'
113
116
 
114
117
  require 'hdo/storting_importer/data_source'
115
118
  require 'hdo/storting_importer/disk_data_source'
116
119
  require 'hdo/storting_importer/api_data_source'
117
120
  require 'hdo/storting_importer/parsing_data_source'
121
+ require 'hdo/storting_importer/caching_data_source'
122
+
118
123
 
119
124
  require 'hdo/storting_importer/category'
120
125
  require 'hdo/storting_importer/committee'
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ module Hdo
5
+ module StortingImporter
6
+ describe CachingDataSource do
7
+
8
+ it 'caches requests' do
9
+ delegate = mock(DataSource)
10
+
11
+ ads = CachingDataSource.new(delegate)
12
+
13
+ delegate.should_receive(:representatives).once.and_return "data"
14
+
15
+ ads.representatives # not cached
16
+ ads.representatives # cached
17
+ end
18
+
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hdo-storting-importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-09 00:00:00.000000000 Z
12
+ date: 2012-08-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder
@@ -240,6 +240,7 @@ files:
240
240
  - hdo-storting-importer.gemspec
241
241
  - lib/hdo/storting_importer.rb
242
242
  - lib/hdo/storting_importer/api_data_source.rb
243
+ - lib/hdo/storting_importer/caching_data_source.rb
243
244
  - lib/hdo/storting_importer/category.rb
244
245
  - lib/hdo/storting_importer/cli.rb
245
246
  - lib/hdo/storting_importer/committee.rb
@@ -251,6 +252,7 @@ files:
251
252
  - lib/hdo/storting_importer/fusion_table.rb
252
253
  - lib/hdo/storting_importer/governing_period.rb
253
254
  - lib/hdo/storting_importer/has_json_schema.rb
255
+ - lib/hdo/storting_importer/in_memory_cache.rb
254
256
  - lib/hdo/storting_importer/inspectable.rb
255
257
  - lib/hdo/storting_importer/issue.rb
256
258
  - lib/hdo/storting_importer/ivar_equality.rb
@@ -292,6 +294,7 @@ files:
292
294
  - spec/fixtures/output/parties.json
293
295
  - spec/fixtures/output/representatives.json
294
296
  - spec/fixtures/output/votes.json
297
+ - spec/hdo/storting_importer/caching_data_source_spec.rb
295
298
  - spec/hdo/storting_importer/category_spec.rb
296
299
  - spec/hdo/storting_importer/committee_spec.rb
297
300
  - spec/hdo/storting_importer/converter_spec.rb
@@ -352,6 +355,7 @@ test_files:
352
355
  - spec/fixtures/output/parties.json
353
356
  - spec/fixtures/output/representatives.json
354
357
  - spec/fixtures/output/votes.json
358
+ - spec/hdo/storting_importer/caching_data_source_spec.rb
355
359
  - spec/hdo/storting_importer/category_spec.rb
356
360
  - spec/hdo/storting_importer/committee_spec.rb
357
361
  - spec/hdo/storting_importer/converter_spec.rb