rgovdata 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/.document +5 -0
  2. data/.rvmrc +2 -0
  3. data/CHANGELOG +7 -0
  4. data/Gemfile +18 -0
  5. data/Gemfile.lock +48 -0
  6. data/LICENSE +20 -0
  7. data/README.rdoc +114 -0
  8. data/Rakefile +61 -0
  9. data/bin/rgd +12 -0
  10. data/examples/all_quakes.rb +8 -0
  11. data/examples/arbitrary_data.rb +26 -0
  12. data/examples/catalog_traversal.rb +34 -0
  13. data/examples/earthquakes.rb +5 -0
  14. data/lib/rgovdata.rb +4 -0
  15. data/lib/rgovdata/catalog.rb +4 -0
  16. data/lib/rgovdata/catalog/catalog.rb +79 -0
  17. data/lib/rgovdata/catalog/dn.rb +63 -0
  18. data/lib/rgovdata/catalog/registry_strategy/internal_registry.rb +12 -0
  19. data/lib/rgovdata/catalog/registry_strategy/registry_strategy.rb +26 -0
  20. data/lib/rgovdata/config.rb +5 -0
  21. data/lib/rgovdata/config/common_config.rb +13 -0
  22. data/lib/rgovdata/config/config.rb +133 -0
  23. data/lib/rgovdata/data/config_template.yml +19 -0
  24. data/lib/rgovdata/data/sg/registry.yml +147 -0
  25. data/lib/rgovdata/data/template.rb +27 -0
  26. data/lib/rgovdata/data/us/registry.yml +12 -0
  27. data/lib/rgovdata/service.rb +10 -0
  28. data/lib/rgovdata/service/csv_service.rb +3 -0
  29. data/lib/rgovdata/service/dataset/csv_dataset.rb +43 -0
  30. data/lib/rgovdata/service/dataset/dataset.rb +91 -0
  31. data/lib/rgovdata/service/dataset/file_dataset.rb +46 -0
  32. data/lib/rgovdata/service/dataset/odata_dataset.rb +31 -0
  33. data/lib/rgovdata/service/file_service.rb +10 -0
  34. data/lib/rgovdata/service/listing.rb +47 -0
  35. data/lib/rgovdata/service/odata_service.rb +50 -0
  36. data/lib/rgovdata/service/service.rb +93 -0
  37. data/lib/rgovdata/shell/shell.rb +157 -0
  38. data/lib/rgovdata/version.rb +9 -0
  39. data/rgovdata.gemspec +128 -0
  40. data/spec/fixtures/sample.csv +821 -0
  41. data/spec/integration/service/sg/nlb_spec.rb +57 -0
  42. data/spec/integration/service/sg/places_spec.rb +73 -0
  43. data/spec/integration/service/us/eqs7day-M1_spec.rb +57 -0
  44. data/spec/spec_helper.rb +25 -0
  45. data/spec/support/config_examples.rb +8 -0
  46. data/spec/support/mocks.rb +22 -0
  47. data/spec/support/utility.rb +18 -0
  48. data/spec/unit/catalog/base_spec.rb +93 -0
  49. data/spec/unit/catalog/registry_strategy_spec.rb +28 -0
  50. data/spec/unit/config/config_spec.rb +130 -0
  51. data/spec/unit/data/template_spec.rb +32 -0
  52. data/spec/unit/service/dataset/csv_dataset_spec.rb +42 -0
  53. data/spec/unit/service/dataset/dataset_spec.rb +37 -0
  54. data/spec/unit/service/dataset/file_dataset_spec.rb +40 -0
  55. data/spec/unit/service/dataset/odata_dataset_spec.rb +36 -0
  56. data/spec/unit/service/file_service_spec.rb +25 -0
  57. data/spec/unit/service/listing_spec.rb +100 -0
  58. data/spec/unit/service/odata_service_spec.rb +42 -0
  59. data/spec/unit/service/service_spec.rb +82 -0
  60. data/spec/unit/shell/shell_spec.rb +10 -0
  61. metadata +228 -0
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ # This runs integration tests against the actual OData service
4
+ # It uses credentials from the rgovdata.conf file in the project root folder
5
+ describe "SG NLB Service" do
6
+ let(:config) { RGovData::Config.instance }
7
+ before :all do
8
+ config.load_config(integration_test_config_filename, {:generate_default => true,:required => true})
9
+ end
10
+ after :all do
11
+ config.clear
12
+ end
13
+
14
+ describe "ServiceListing" do
15
+ let(:id) { '//sg/nlb' }
16
+ let(:dataset_key) { 'LibrarySet' }
17
+ let(:example_attribute) { 'LibraryID' }
18
+ let(:record_class) { Library }
19
+
20
+ let(:service_listing) { RGovData::Catalog.get(id) }
21
+
22
+ subject { service_listing }
23
+ it { should be_a(RGovData::ServiceListing) }
24
+
25
+ describe "#service" do
26
+ let(:service) { service_listing.service }
27
+ subject { service }
28
+ it { should be_a(RGovData::Service) }
29
+ describe "#dataset_keys" do
30
+ subject { service.dataset_keys }
31
+ it { should include(dataset_key) }
32
+ end
33
+ end
34
+
35
+ describe "#find" do
36
+ let(:dataset) { service_listing.find(dataset_key) }
37
+ subject { dataset }
38
+ it { should be_a(RGovData::OdataDataSet) }
39
+ describe "#attributes" do
40
+ subject { dataset.attributes }
41
+ it { should include(example_attribute) }
42
+ end
43
+ describe "#records" do
44
+ let(:record_limit) { 3 }
45
+ before {
46
+ dataset.limit = record_limit
47
+ }
48
+ subject { dataset.records }
49
+ it { should be_a(Array) }
50
+ its(:first) { should be_a(record_class) }
51
+ its(:count) { should eql(record_limit) }
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ # This runs integration tests against the actual OData service
4
+ # It uses credentials from the rgovdata.conf file in the project root folder
5
+ # describe "Places.sg Service" do
6
+ # let(:config) { RGovData::Config.instance }
7
+ # before :all do
8
+ # config.load_config(integration_test_config_filename, {:generate_default => true,:required => true})
9
+ # end
10
+ # after :all do
11
+ # config.clear
12
+ # end
13
+ #
14
+ # describe "ServiceListing" do
15
+ # let(:id) { '//sg/places' }
16
+ # let(:dataset_key) { 'Places' }
17
+ # let(:example_attribute) { 'company_name' }
18
+ # let(:record_class) { Places }
19
+ #
20
+ # let(:service_listing) { RGovData::Catalog.get(id) }
21
+ #
22
+ # subject { service_listing }
23
+ # it { should be_a(RGovData::ServiceListing) }
24
+ #
25
+ # describe "#service" do
26
+ # let(:service) { service_listing.service }
27
+ # subject { service }
28
+ # it { should be_a(RGovData::Service) }
29
+ # describe "#dataset_keys" do
30
+ # subject { service.dataset_keys }
31
+ # it { should include(dataset_key) }
32
+ # context "map to collections" do
33
+ # let(:collections) { service.native_instance.instance_variable_get(:@collections) }
34
+ # it "should include all" do
35
+ # collections.each do |collection|
36
+ # subject.should include(collection)
37
+ # end
38
+ # end
39
+ # end
40
+ # end
41
+ # # describe "#classes" do
42
+ # # subject { service.native_instance.classes }
43
+ # # it { puts subject.inspect }
44
+ # # end
45
+ # # describe "#class_metadata" do
46
+ # # subject { service.native_instance.class_metadata }
47
+ # # it { puts subject.inspect }
48
+ # # end
49
+ # end
50
+ #
51
+ # describe "#find" do
52
+ # let(:dataset) { service_listing.find(dataset_key) }
53
+ # subject { dataset }
54
+ # it { should be_a(RGovData::OdataDataSet) }
55
+ # describe "#attributes" do
56
+ # subject { dataset.attributes }
57
+ # it { should include(example_attribute) }
58
+ # end
59
+ # describe "#records" do
60
+ # let(:record_limit) { 3 }
61
+ # before {
62
+ # dataset.limit = record_limit
63
+ # }
64
+ # subject { dataset.records }
65
+ # it { should be_a(Array) }
66
+ # its(:first) { should be_a(record_class) }
67
+ # its(:count) { should eql(record_limit) }
68
+ # end
69
+ #
70
+ # end
71
+ # end
72
+ #
73
+ # end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ # This runs integration tests against the actual csv file service
4
+ # It uses credentials from the rgovdata.conf file in the project root folder
5
+ describe "Worldwide M1+ Earthquakes" do
6
+ let(:config) { RGovData::Config.instance }
7
+ before :all do
8
+ config.load_config(integration_test_config_filename, {:generate_default => true,:required => true})
9
+ end
10
+ after :all do
11
+ config.clear
12
+ end
13
+
14
+ describe "ServiceListing" do
15
+ let(:id) { '//us/eqs7day-M1' }
16
+ let(:dataset_key) { :csv }
17
+ let(:expect_attributes) { ["Src", "Eqid", "Version", "Datetime", "Lat", "Lon", "Magnitude", "Depth", "NST", "Region"] }
18
+ let(:record_class) { CSV::Row }
19
+
20
+ let(:service_listing) { RGovData::Catalog.get(id) }
21
+
22
+ subject { service_listing }
23
+ it { should be_a(RGovData::ServiceListing) }
24
+
25
+ describe "#service" do
26
+ let(:service) { service_listing.service }
27
+ subject { service }
28
+ it { should be_a(RGovData::Service) }
29
+ describe "#dataset_keys" do
30
+ subject { service.dataset_keys }
31
+ it { should include(dataset_key) }
32
+ end
33
+ end
34
+
35
+ describe "#find" do
36
+ let(:dataset) { service_listing.find(dataset_key) }
37
+ subject { dataset }
38
+ it { should be_a(RGovData::CsvDataSet) }
39
+ describe "#attributes" do
40
+ subject { dataset.attributes }
41
+ it { should eql(expect_attributes) }
42
+ end
43
+ describe "#records" do
44
+ let(:record_limit) { 3 }
45
+ before {
46
+ dataset.limit = record_limit
47
+ }
48
+ subject { dataset.records(true) }
49
+ it { should be_a(Array) }
50
+ its(:first) { should be_a(record_class) }
51
+ its(:count) { should eql(record_limit) }
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,25 @@
1
+ require 'rgovdata'
2
+
3
+ # Requires supporting ruby files with custom matchers and macros, etc,
4
+ # in spec/support/ and its subdirectories.
5
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
6
+
7
+ RSpec.configure do |config|
8
+ # == Mock Framework
9
+ #
10
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
11
+ #
12
+ # config.mock_with :mocha
13
+ # config.mock_with :flexmock
14
+ # config.mock_with :rr
15
+ config.mock_with :rspec
16
+
17
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
18
+ # config.fixture_path = "#{::Rails.root}/spec/fixtures"
19
+
20
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
21
+ # examples within a transaction, remove the following line or assign false
22
+ # instead of true.
23
+ # config.use_transactional_fixtures = true
24
+
25
+ end
@@ -0,0 +1,8 @@
1
+ shared_examples_for "includes common config" do
2
+ # Expects on entry:
3
+ # +subject+ is set to the instance to test
4
+ describe "#config" do
5
+ it { should respond_to(:config) }
6
+ its(:config) { should be_a(RGovData::Config) }
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ require 'pathname'
2
+
3
+ module MocksHelper
4
+ def mock_file_path(key)
5
+ Pathname.new(File.dirname(__FILE__)).join('..','fixtures',key).to_s
6
+ end
7
+ def mock_text(key)
8
+ IO.read(mock_file_path(key))
9
+ end
10
+ def mock_xml(key)
11
+ Nokogiri::XML(mock_text(key))
12
+ end
13
+ def mock_rails_root
14
+ Pathname.new(File.dirname(__FILE__)).join('..','fixtures','rails_root')
15
+ end
16
+ def mock_configfile_path
17
+ Pathname.new(File.dirname(__FILE__)).join('..','fixtures','rgovdata.conf').to_s
18
+ end
19
+ def mock_configfile_path_notfound
20
+ Pathname.new(File.dirname(__FILE__)).join('..','fixtures','rgovdata-notfound.conf').to_s
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ def get_temp_file(basename)
2
+ require 'tempfile'
3
+ f = Tempfile.new(basename)
4
+ path = f.path
5
+ f.close!()
6
+ path
7
+ end
8
+
9
+ def supported_realms
10
+ [:sg,:us]
11
+ end
12
+
13
+ # Returns the config for integration testing
14
+ def integration_test_config_filename
15
+ filename = "#{File.dirname(__FILE__)}/../../#{RGovData::Config::BASE_NAME}"
16
+ STDERR.puts "Running integration tests with config: #{filename}"
17
+ filename
18
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe RGovData::Catalog do
4
+
5
+ supported_realms.each do |realm|
6
+ context "with realm #{realm}" do
7
+ let(:catalog) { RGovData::Catalog.new(realm) }
8
+ subject { catalog }
9
+ its(:realm) { should eql(realm) }
10
+ describe "#services" do
11
+ subject { catalog.services }
12
+ it { should be_a(Array) }
13
+ its(:first) { should be_a(RGovData::ServiceListing) }
14
+ end
15
+ end
16
+ end
17
+
18
+ describe "#get_service" do
19
+ let(:catalog) { RGovData::Catalog.new(:sg) }
20
+ subject { catalog.get_service(key) }
21
+ context "with multiple matches" do
22
+ let(:key) { 'l' }
23
+ it { should be_a(Array) }
24
+ its(:first) { should be_a(RGovData::ServiceListing) }
25
+ end
26
+ context "with single match" do
27
+ let(:key) { 'nlb' }
28
+ it { should be_a(RGovData::ServiceListing) }
29
+ its(:realm) { should eql(:sg) }
30
+ its(:key) { should eql(key) }
31
+ end
32
+ end
33
+
34
+ describe "#realms" do
35
+ subject { RGovData::Catalog.new(nil).realms }
36
+ it { should be_a(Array) }
37
+ it "should include supported realms" do
38
+ subject.each do |realm_cat|
39
+ supported_realms.should include(realm_cat.realm)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "#to_s" do
45
+ its(:to_s) { should be_a(String) }
46
+ end
47
+
48
+ describe "#records" do
49
+ context "without realm" do
50
+ subject { RGovData::Catalog.new(nil).records }
51
+ it { should be_a(Array) }
52
+ its(:first) { should be_a(RGovData::Catalog) }
53
+ end
54
+ context "with realm" do
55
+ subject { RGovData::Catalog.new(supported_realms.first).records }
56
+ it { should be_a(Array) }
57
+ its(:first) { should be_a(RGovData::ServiceListing) }
58
+ end
59
+ end
60
+
61
+ describe "##get" do
62
+ subject { RGovData::Catalog.get(key) }
63
+ context "with nil selector" do
64
+ let(:key) { nil }
65
+ it { should be_a(RGovData::Catalog) }
66
+ its(:realm) { should be_nil }
67
+ end
68
+ context "with blank selector" do
69
+ let(:key) { '' }
70
+ it { should be_a(RGovData::Catalog) }
71
+ its(:realm) { should be_nil }
72
+ end
73
+ context "with no realm" do
74
+ let(:key) { '//' }
75
+ it { should be_a(RGovData::Catalog) }
76
+ its(:realm) { should be_nil }
77
+ end
78
+ context "with realm only" do
79
+ ['//sg','::sg'].each do |key_test|
80
+ let(:key) { key_test}
81
+ it { should be_a(RGovData::Catalog) }
82
+ its(:realm) { should eql(:sg) }
83
+ end
84
+ end
85
+ context "with realm and service" do
86
+ let(:key) { '//sg/nlb' }
87
+ it { should be_a(RGovData::ServiceListing) }
88
+ its(:realm) { should eql(:sg) }
89
+ its(:key) { should eql('nlb') }
90
+ end
91
+ end
92
+
93
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe RGovData::RegistryStrategy do
4
+ {
5
+ :sg => {:class => RGovData::InternalRegistry}
6
+ }.each do |realm,options|
7
+ context "with realm #{realm}" do
8
+ describe "##instance_for_realm" do
9
+ subject { RGovData::RegistryStrategy.instance_for_realm(realm) }
10
+ it { should be_a(options[:class]) }
11
+ its(:realm) { should eql(realm) }
12
+ its(:load_services) { should be_a(Array) }
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "#load_services" do
18
+ subject { RGovData::RegistryStrategy.new.load_services}
19
+ it { should eql([]) }
20
+ end
21
+
22
+ describe "#realm" do
23
+ let(:realm) { :xy }
24
+ subject { RGovData::RegistryStrategy.new(realm) }
25
+ its(:realm) { should eql(realm) }
26
+ end
27
+
28
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+ include MocksHelper
3
+
4
+ describe RGovData::Config do
5
+ let(:config) { RGovData::Config.instance }
6
+
7
+ describe "##default_config_file" do
8
+ subject { RGovData::Config }
9
+ its(:default_config_file) { should match(RGovData::Config::BASE_NAME) }
10
+ it "should accept override parameter" do
11
+ subject.default_config_file('foo').should match(/foo/)
12
+ end
13
+ end
14
+
15
+ describe "##template" do
16
+ subject { RGovData::Config.template }
17
+ it { should be_a(String) }
18
+ end
19
+
20
+ describe "#show_status" do
21
+ it { config.should respond_to(:show_status) }
22
+ end
23
+
24
+ describe "#load_default_config" do
25
+ before {
26
+ ENV['rgovdata_username'] = nil
27
+ ENV['rgovdata_password'] = nil
28
+ }
29
+ context "with Rails environment" do
30
+ let(:expect) { 'rails_root_username' }
31
+ before {
32
+ config.stub(:rails_root).and_return(mock_rails_root)
33
+ config.load_default_config
34
+ }
35
+ subject { config.credentialsets['basic']['username']}
36
+ it "should load settings from the Rails.root/config/BASE_NAME file" do
37
+ should eql(expect)
38
+ end
39
+ end
40
+
41
+ context "with config file in pwd" do
42
+ let(:expect) { 'mock_username' }
43
+ before {
44
+ config.class.stub(:default_config_file).and_return(mock_configfile_path)
45
+ config.load_default_config
46
+ }
47
+ subject { config.credentialsets['basic']['username']}
48
+ it "should load settings from the pwd file" do
49
+ should eql(expect)
50
+ end
51
+ end
52
+
53
+ context "with no config file and no env setting" do
54
+ let(:expect) { nil }
55
+ before {
56
+ config.class.stub(:default_config_file).and_return(mock_configfile_path_notfound)
57
+ config.load_default_config
58
+ }
59
+ subject { config.credentialsets['basic']}
60
+ it "should load settings from the pwd file" do
61
+ should eql(expect)
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#load_config" do
67
+ let(:temp_config_file) { get_temp_file('rgovdata_config_test_') }
68
+ let(:config) { RGovData::Config.instance }
69
+ after do
70
+ File.delete(temp_config_file) if File.exists?(temp_config_file)
71
+ end
72
+ it "should not generate template file if auto-generation not enabled" do
73
+ expect {
74
+ config.load_config(temp_config_file,{:generate_default => false})
75
+ }.to raise_error(RGovData::Config::ConfigurationFileNotFound)
76
+ end
77
+ it "should generate template file if auto-generation is enabled" do
78
+ expect {
79
+ config.load_config(temp_config_file)
80
+ }.to raise_error(RGovData::Config::ConfigurationFileInitialized)
81
+ File.exists?(temp_config_file).should be_true
82
+ end
83
+ end
84
+
85
+ describe "#credentialsets" do
86
+ before :all do
87
+ config.load_config(config.class.template_path,{:generate_default => false})
88
+ end
89
+ {
90
+ "basic" => { "username" => "_insert_your_username_here_", "password" => "your_password"},
91
+ "projectnimbus" => { "AccountKey" => "_insert_your_key_here_", "UniqueUserID" => "00000000000000000000000000000001"}
92
+ }.each do |credentialset,options|
93
+ context credentialset do
94
+ subject { config.credentialsets[credentialset] }
95
+ it { should be_a(Hash)}
96
+ options.keys.each do |key|
97
+ describe key do
98
+ subject { config.credentialsets[credentialset][key] }
99
+ it { should eql(options[key]) }
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ {
107
+ 'projectnimbus_account_key' => {:credentialset=>'projectnimbus', :item=>'AccountKey', :alt=>'UniqueUserID'},
108
+ 'projectnimbus_unique_user_id' => {:credentialset=>'projectnimbus', :item=>'UniqueUserID', :alt=>'AccountKey'},
109
+ 'rgovdata_username' => {:credentialset=>'basic', :item=>'username', :alt=>'password'},
110
+ 'rgovdata_password' => {:credentialset=>'basic', :item=>'password', :alt=>'username'}
111
+ }.each do |override,options|
112
+ describe "#credentialsets ENV['#{override}'] override" do
113
+ let(:key) { 'abcdefg' }
114
+ before {
115
+ ENV[override] = key
116
+ config.load_config(config.class.template_path,{:generate_default => false})
117
+ }
118
+ after { ENV[override] = nil }
119
+ subject { config.credentialsets[options[:credentialset]][options[:item]] }
120
+ it { should eql(key) }
121
+ describe "other keys" do
122
+ subject { config.credentialsets[options[:credentialset]][options[:alt]] }
123
+ it "should not be affected" do
124
+ should_not be_nil
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ end