enigma_io 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +17 -0
  2. data/.rubocop.yml +14 -0
  3. data/.travis.yml +14 -0
  4. data/Gemfile +11 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +64 -0
  7. data/Rakefile +28 -0
  8. data/enigma_io.gemspec +30 -0
  9. data/examples/data.md +94 -0
  10. data/examples/export.md +35 -0
  11. data/lib/enigma/client.rb +26 -0
  12. data/lib/enigma/download.rb +81 -0
  13. data/lib/enigma/endpoint.rb +108 -0
  14. data/lib/enigma/endpoints/data.rb +5 -0
  15. data/lib/enigma/endpoints/export.rb +22 -0
  16. data/lib/enigma/endpoints/meta.rb +5 -0
  17. data/lib/enigma/endpoints/stats.rb +5 -0
  18. data/lib/enigma/response.rb +17 -0
  19. data/lib/enigma/version.rb +6 -0
  20. data/lib/enigma.rb +49 -0
  21. data/test/client_test.rb +32 -0
  22. data/test/data_test.rb +81 -0
  23. data/test/endpoint_test.rb +55 -0
  24. data/test/export_test.rb +67 -0
  25. data/test/fixtures/download.csv +4 -0
  26. data/test/fixtures/download.zip +0 -0
  27. data/test/fixtures/vcr_cassettes/compound_average.yml +121 -0
  28. data/test/fixtures/vcr_cassettes/data_with_error.yml +38 -0
  29. data/test/fixtures/vcr_cassettes/export.yml +37 -0
  30. data/test/fixtures/vcr_cassettes/filtered_data.yml +6727 -0
  31. data/test/fixtures/vcr_cassettes/limit_data.yml +54 -0
  32. data/test/fixtures/vcr_cassettes/page_data.yml +53 -0
  33. data/test/fixtures/vcr_cassettes/selected_data.yml +541 -0
  34. data/test/fixtures/vcr_cassettes/simple_data.yml +6762 -0
  35. data/test/fixtures/vcr_cassettes/simple_metadata.yml +75 -0
  36. data/test/fixtures/vcr_cassettes/simple_stats.yml +48 -0
  37. data/test/fixtures/vcr_cassettes/sorted_data.yml +6690 -0
  38. data/test/fixtures/vcr_cassettes/sorted_data_descending.yml +6690 -0
  39. data/test/meta_test.rb +26 -0
  40. data/test/response_test.rb +13 -0
  41. data/test/stats_test.rb +27 -0
  42. data/test/test_helper.rb +37 -0
  43. metadata +223 -0
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test_helper'
4
+
5
+ class EnigmaTest < Test::Unit::TestCase
6
+ def setup
7
+ ENV['ENIGMA_KEY'] = nil
8
+ end
9
+
10
+ def test_initialize_handle
11
+ Enigma::Client.new(key: 'test')
12
+ assert_equal 'test', Enigma.key
13
+ end
14
+
15
+ def test_initialize_key_from_env
16
+ ENV['ENIGMA_KEY'] = 'test'
17
+ Enigma::Client.new
18
+ assert_equal 'test', Enigma.key
19
+ end
20
+
21
+ def test_prefers_key_from_env
22
+ ENV['ENIGMA_KEY'] = 'env_key'
23
+ Enigma::Client.new(key: 'bogus')
24
+ assert_equal 'env_key', Enigma.key
25
+ end
26
+
27
+ def test_requires_key
28
+ assert_raises ArgumentError do
29
+ Enigma::Client.new
30
+ end
31
+ end
32
+ end
data/test/data_test.rb ADDED
@@ -0,0 +1,81 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test_helper'
4
+
5
+ class DataTest < Test::Unit::TestCase
6
+ def setup
7
+ @client = Enigma::Client.new(key: TEST_KEY)
8
+ end
9
+
10
+ def test_bad_request
11
+ VCR.use_cassette('data_with_error') do
12
+ assert_raises RuntimeError do
13
+ response = @client.data('us.gov.whitehouse.salaries')
14
+ end
15
+ end
16
+ end
17
+
18
+ def test_simple_data_request
19
+ VCR.use_cassette('simple_data') do
20
+ response = @client.data('us.gov.whitehouse.visitor-list')
21
+ assert_equal 500, response.info.rows_limit
22
+ assert_equal 3427529, response.info.total_results
23
+ assert_equal 'TED', response.result.first.namefirst
24
+ end
25
+ end
26
+
27
+ def test_filter_with_search
28
+ VCR.use_cassette('filtered_data') do
29
+ response = @client.data('us.gov.whitehouse.visitor-list',
30
+ search: { namefirst: ['Steve', 'Joe'] })
31
+ assert_equal ['JOE', 'STEVE'], response.result.map(&:namefirst).uniq.sort
32
+ end
33
+ end
34
+
35
+ def test_filter_with_select
36
+ VCR.use_cassette('selected_data') do
37
+ response = @client.data('us.gov.whitehouse.visitor-list',
38
+ select: ['namefirst', 'namelast'])
39
+ assert_equal nil, response.result.first.appt_made_date
40
+ assert response.result.first.namefirst
41
+ end
42
+ end
43
+
44
+ def test_sort
45
+ VCR.use_cassette('sorted_data_descending') do
46
+ response = @client.data('us.gov.whitehouse.visitor-list',
47
+ sort: 'namefirst-')
48
+ names = response.result.map(&:firstname)
49
+ assert_equal names, names.sort.reverse
50
+ end
51
+ end
52
+
53
+ def test_limit
54
+ VCR.use_cassette('limit_data') do
55
+ response = @client.data('us.gov.whitehouse.visitor-list',
56
+ limit: 1)
57
+ assert response.info.total_results > 1
58
+ assert_equal 1, response.info.rows_limit
59
+ assert_equal 1, response.result.count
60
+ end
61
+ end
62
+
63
+ def test_page
64
+ VCR.use_cassette('page_data') do
65
+ response = @client.data('us.gov.whitehouse.visitor-list',
66
+ limit: 1, page: 2)
67
+ assert response.info.total_results > 1
68
+ assert_equal 1, response.info.rows_limit
69
+ assert_equal 2, response.info.current_page
70
+ end
71
+ end
72
+
73
+ def test_sort_defaults_ascending
74
+ VCR.use_cassette('sorted_data') do
75
+ response = @client.data('us.gov.whitehouse.visitor-list',
76
+ sort: 'namefirst')
77
+ names = response.result.map(&:firstname)
78
+ assert_equal names, names.sort
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test_helper'
4
+
5
+ class EndpointTest < Test::Unit::TestCase
6
+ def setup
7
+ ENV['ENIGMA_KEY'] = 'test_key'
8
+ @datapath = 'us.gov.whitehouse.visitor-list'
9
+ @ep = Enigma::Endpoint.new(@datapath)
10
+ end
11
+
12
+ def test_needs_datapath
13
+ assert_raises ArgumentError do
14
+ Enigma::Endpoint.new
15
+ end
16
+ end
17
+
18
+ def test_query_params
19
+ ep = Enigma::Endpoint.new(@datapath, limit: 100)
20
+ assert_equal({ limit: 100 }, ep.params)
21
+ end
22
+
23
+ def test_has_descendants
24
+ assert Enigma::Endpoint.descendants.include? Enigma::Meta
25
+ end
26
+
27
+ def test_where_clause_from_hash
28
+ assert_equal 'firstName=Steve', @ep.serialize_where({ firstName: 'Steve' })
29
+ end
30
+
31
+ def test_where_clause_from_string
32
+ assert_equal 'firstName=Steve', @ep.serialize_where('firstName=Steve')
33
+ end
34
+
35
+ def test_search_clause_from_hash
36
+ search_hash = { nameFirst: 'andrew', nameLast: ['white', 'brown', 'black'] }
37
+ assert_equal('@nameFirst (andrew) @nameLast (white|brown|black)',
38
+ @ep.serialize_search(search_hash))
39
+ end
40
+
41
+ def test_search_clause_from_string
42
+ search = '@namefirst (andrew) @namelast (white|brown)'
43
+ assert_equal(search, @ep.serialize_search(search))
44
+ end
45
+
46
+ def test_select_clause_from_string
47
+ select = 'namefirst,namelast'
48
+ assert_equal(select, @ep.serialize_select(select))
49
+ end
50
+
51
+ def test_select_clause_from_array
52
+ select = ['namefirst', 'namelast']
53
+ assert_equal('namefirst,namelast', @ep.serialize_select(select))
54
+ end
55
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ExportTest < Test::Unit::TestCase
5
+ def setup
6
+ @client = Enigma::Client.new(key: TEST_KEY)
7
+ Enigma::Download.any_instance.stubs(:sleep)
8
+
9
+ # These are identical, just one is zipped
10
+ @zipped = File.read 'test/fixtures/download.zip'
11
+ @unzipped = File.read 'test/fixtures/download.csv'
12
+
13
+ # www.example.com is the URL in the response to the export
14
+ # cassette
15
+ stub_request(:get, 'www.example.com').to_return do
16
+ if @tried
17
+ { body: @zipped }
18
+ else
19
+ @tried = true
20
+ { status: 404 }
21
+ end
22
+ end
23
+ end
24
+
25
+ def test_export_no_dl
26
+ VCR.use_cassette('export') do
27
+ dl = @client.export('us.gov.whitehouse.visitor-list')
28
+ assert dl.response.export_url
29
+ end
30
+ end
31
+
32
+ def test_export_with_dl
33
+ VCR.use_cassette('export') do
34
+ dl = @client.export('us.gov.whitehouse.visitor-list')
35
+ dl.get
36
+ assert_equal @zipped, dl.raw_download
37
+ end
38
+ end
39
+
40
+ def test_writing_zip
41
+ VCR.use_cassette('export') do
42
+ dl = @client.export('us.gov.whitehouse.visitor-list')
43
+ dl.get
44
+ testIO = StringIO.new
45
+ dl.write(testIO)
46
+ assert_equal @zipped, testIO.string
47
+ end
48
+ end
49
+
50
+ def test_writing_csv
51
+ VCR.use_cassette('export') do
52
+ dl = @client.export('us.gov.whitehouse.visitor-list')
53
+ dl.get
54
+ testIO = StringIO.new
55
+ dl.write_csv(testIO)
56
+ assert_equal @unzipped, testIO.string
57
+ end
58
+ end
59
+
60
+ def test_parse_as_csv
61
+ VCR.use_cassette('export') do
62
+ dl = @client.export('us.gov.whitehouse.visitor-list')
63
+ parsed = dl.parse
64
+ assert_equal 'Steve', parsed.first[:name]
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,4 @@
1
+ id,name
2
+ 1,Steve
3
+ 2,Joe
4
+ 3,Alice
Binary file
@@ -0,0 +1,121 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api.enigma.io/v2/stats/test-key/us.gov.whitehouse.visitor-list?by=avg&of=total_people&select=release_date
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Typhoeus - https://github.com/typhoeus/typhoeus
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Server:
18
+ - nginx/1.2.9
19
+ Date:
20
+ - Sun, 26 Jan 2014 21:27:58 GMT
21
+ Content-Type:
22
+ - application/json; charset=utf-8
23
+ Content-Length:
24
+ - '5971'
25
+ Connection:
26
+ - keep-alive
27
+ Vary:
28
+ - Accept-Encoding
29
+ Access-Control-Allow-Origin:
30
+ - ! '*'
31
+ Etag:
32
+ - ! '"1451852404"'
33
+ body:
34
+ encoding: US-ASCII
35
+ string: ! "{\n \"datapath\": \"us.gov.whitehouse.visitor-list\",\n \"success\":
36
+ true,\n \"info\": {\n \"column\": {\n \"id\": \"release_date\",\n
37
+ \ \"label\": \"Release Date\",\n \"description\": \"Release Date\",\n
38
+ \ \"type\": \"type_date\",\n \"index\": 28\n },\n \"operations\":
39
+ [\n \"compound_avg\"\n ],\n \"rows_limit\": 500,\n \"total_results\":
40
+ 52,\n \"total_pages\": 1,\n \"current_page\": 1\n },\n \"result\":
41
+ {\n \"compound_avg\": [\n {\n \"release_date\": \"2010-03-26T00:00:00.000Z\",\n
42
+ \ \"avg\": \"734.1429047671963552\"\n },\n {\n \"release_date\":
43
+ \"2013-11-08T00:00:00.000Z\",\n \"avg\": \"679.4682244439277687\"\n
44
+ \ },\n {\n \"release_date\": \"2012-06-29T00:00:00.000Z\",\n
45
+ \ \"avg\": \"533.7427734055372897\"\n },\n {\n \"release_date\":
46
+ \"2010-08-27T00:00:00.000Z\",\n \"avg\": \"455.2680450957263406\"\n
47
+ \ },\n {\n \"release_date\": \"2010-09-24T00:00:00.000Z\",\n
48
+ \ \"avg\": \"439.1832508722477381\"\n },\n {\n \"release_date\":
49
+ \"2011-12-30T00:00:00.000Z\",\n \"avg\": \"437.2926486009161654\"\n
50
+ \ },\n {\n \"release_date\": \"2011-09-30T00:00:00.000Z\",\n
51
+ \ \"avg\": \"421.4652122579903666\"\n },\n {\n \"release_date\":
52
+ \"2013-04-26T00:00:00.000Z\",\n \"avg\": \"416.6219928445120697\"\n
53
+ \ },\n {\n \"release_date\": \"2012-01-27T00:00:00.000Z\",\n
54
+ \ \"avg\": \"415.6607848640052387\"\n },\n {\n \"release_date\":
55
+ \"2010-10-29T00:00:00.000Z\",\n \"avg\": \"377.5752650405801174\"\n
56
+ \ },\n {\n \"release_date\": \"2012-09-28T00:00:00.000Z\",\n
57
+ \ \"avg\": \"354.3737179806018403\"\n },\n {\n \"release_date\":
58
+ \"2012-10-26T00:00:00.000Z\",\n \"avg\": \"340.0990311924978385\"\n
59
+ \ },\n {\n \"release_date\": \"2011-10-28T00:00:00.000Z\",\n
60
+ \ \"avg\": \"323.4379794352864371\"\n },\n {\n \"release_date\":
61
+ \"2011-01-28T00:00:00.000Z\",\n \"avg\": \"302.7656493230812774\"\n
62
+ \ },\n {\n \"release_date\": \"2010-06-25T00:00:00.000Z\",\n
63
+ \ \"avg\": \"290.8586820889530279\"\n },\n {\n \"release_date\":
64
+ \"2011-03-25T00:00:00.000Z\",\n \"avg\": \"282.7361624065521076\"\n
65
+ \ },\n {\n \"release_date\": \"2010-01-29T00:00:00.000Z\",\n
66
+ \ \"avg\": \"281.5727106178477551\"\n },\n {\n \"release_date\":
67
+ \"2012-07-27T00:00:00.000Z\",\n \"avg\": \"274.4184635378532728\"\n
68
+ \ },\n {\n \"release_date\": \"2009-12-30T00:00:00.000Z\",\n
69
+ \ \"avg\": \"264.1159100265728508\"\n },\n {\n \"release_date\":
70
+ \"2013-03-29T00:00:00.000Z\",\n \"avg\": \"262.5289941919424726\"\n
71
+ \ },\n {\n \"release_date\": \"2011-11-22T00:00:00.000Z\",\n
72
+ \ \"avg\": \"255.5073803730738037\"\n },\n {\n \"release_date\":
73
+ \"2012-03-30T00:00:00.000Z\",\n \"avg\": \"253.8343781729434669\"\n
74
+ \ },\n {\n \"release_date\": \"2010-11-26T00:00:00.000Z\",\n
75
+ \ \"avg\": \"249.2652607538964273\"\n },\n {\n \"release_date\":
76
+ \"2010-02-26T00:00:00.000Z\",\n \"avg\": \"246.0113161495506823\"\n
77
+ \ },\n {\n \"release_date\": \"2011-07-29T00:00:00.000Z\",\n
78
+ \ \"avg\": \"245.3487768177846811\"\n },\n {\n \"release_date\":
79
+ \"2010-07-30T00:00:00.000Z\",\n \"avg\": \"244.8549041386970590\"\n
80
+ \ },\n {\n \"release_date\": \"2011-06-24T00:00:00.000Z\",\n
81
+ \ \"avg\": \"237.4581660583941606\"\n },\n {\n \"release_date\":
82
+ \"2011-08-26T00:00:00.000Z\",\n \"avg\": \"236.1496245334772247\"\n
83
+ \ },\n {\n \"release_date\": \"2012-02-24T00:00:00.000Z\",\n
84
+ \ \"avg\": \"212.1851240749513992\"\n },\n {\n \"release_date\":
85
+ \"2012-11-30T00:00:00.000Z\",\n \"avg\": \"204.5262752554120889\"\n
86
+ \ },\n {\n \"release_date\": \"2013-01-25T00:00:00.000Z\",\n
87
+ \ \"avg\": \"204.5012172896871218\"\n },\n {\n \"release_date\":
88
+ \"2011-02-25T00:00:00.000Z\",\n \"avg\": \"202.2168111492909595\"\n
89
+ \ },\n {\n \"release_date\": \"2011-05-27T00:00:00.000Z\",\n
90
+ \ \"avg\": \"190.9268717016875761\"\n },\n {\n \"release_date\":
91
+ \"2013-02-23T00:00:00.000Z\",\n \"avg\": \"189.6301864506875896\"\n
92
+ \ },\n {\n \"release_date\": \"2012-08-31T00:00:00.000Z\",\n
93
+ \ \"avg\": \"185.2720662956690056\"\n },\n {\n \"release_date\":
94
+ \"2012-12-28T00:00:00.000Z\",\n \"avg\": \"179.1459236875479497\"\n
95
+ \ },\n {\n \"release_date\": \"2010-05-28T00:00:00.000Z\",\n
96
+ \ \"avg\": \"178.5372322579505924\"\n },\n {\n \"release_date\":
97
+ \"2013-06-28T00:00:00.000Z\",\n \"avg\": \"171.7002070313997623\"\n
98
+ \ },\n {\n \"release_date\": \"2011-04-29T00:00:00.000Z\",\n
99
+ \ \"avg\": \"163.3204458648374776\"\n },\n {\n \"release_date\":
100
+ \"2012-05-25T00:00:00.000Z\",\n \"avg\": \"162.8172296262254902\"\n
101
+ \ },\n {\n \"release_date\": \"2013-05-31T00:00:00.000Z\",\n
102
+ \ \"avg\": \"154.0788188835038593\"\n },\n {\n \"release_date\":
103
+ \"2012-04-27T00:00:00.000Z\",\n \"avg\": \"151.8549782515176139\"\n
104
+ \ },\n {\n \"release_date\": \"2010-04-30T00:00:00.000Z\",\n
105
+ \ \"avg\": \"150.9765423099860844\"\n },\n {\n \"release_date\":
106
+ \"2009-10-30T00:00:00.000Z\",\n \"avg\": \"147.7505197505197505\"\n
107
+ \ },\n {\n \"release_date\": \"2010-12-31T00:00:00.000Z\",\n
108
+ \ \"avg\": \"142.6299773675648914\"\n },\n {\n \"release_date\":
109
+ \"2013-09-27T00:00:00.000Z\",\n \"avg\": \"85.2565229332600934\"\n
110
+ \ },\n {\n \"release_date\": \"2013-07-26T00:00:00.000Z\",\n
111
+ \ \"avg\": \"84.6897653782523688\"\n },\n {\n \"release_date\":
112
+ \"2013-11-29T00:00:00.000Z\",\n \"avg\": \"69.5096365282628379\"\n
113
+ \ },\n {\n \"release_date\": \"2009-11-25T00:00:00.000Z\",\n
114
+ \ \"avg\": \"64.7715544367526746\"\n },\n {\n \"release_date\":
115
+ \"2013-08-30T00:00:00.000Z\",\n \"avg\": \"51.6237960052722295\"\n
116
+ \ },\n {\n \"release_date\": \"2013-12-27T00:00:00.000Z\",\n
117
+ \ \"avg\": \"48.5304894428625154\"\n },\n {\n \"release_date\":
118
+ null,\n \"avg\": \"23.0000000000000000\"\n }\n ]\n }\n}"
119
+ http_version:
120
+ recorded_at: Sun, 26 Jan 2014 21:29:03 GMT
121
+ recorded_with: VCR 2.8.0
@@ -0,0 +1,38 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api.enigma.io/v2/data/test-key/us.gov.whitehouse.salaries
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Typhoeus - https://github.com/typhoeus/typhoeus
12
+ response:
13
+ status:
14
+ code: 400
15
+ message: Bad Request
16
+ headers:
17
+ Server:
18
+ - nginx/1.2.9
19
+ Date:
20
+ - Sun, 26 Jan 2014 21:02:39 GMT
21
+ Content-Type:
22
+ - application/json; charset=utf-8
23
+ Content-Length:
24
+ - '151'
25
+ Connection:
26
+ - keep-alive
27
+ Vary:
28
+ - Accept-Encoding
29
+ Access-Control-Allow-Origin:
30
+ - ! '*'
31
+ body:
32
+ encoding: US-ASCII
33
+ string: ! "{\n \"success\": false,\n \"info\": {\n \"status\": 400,\n \"message\":
34
+ \"Bad request\",\n \"additional\": \"Datapath is not a table\",\n \"error\":
35
+ true\n }\n}"
36
+ http_version:
37
+ recorded_at: Sun, 26 Jan 2014 21:03:44 GMT
38
+ recorded_with: VCR 2.8.0
@@ -0,0 +1,37 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api.enigma.io/v2/export/test-key/us.gov.whitehouse.visitor-list
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Typhoeus - https://github.com/typhoeus/typhoeus
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Server:
18
+ - nginx/1.2.9
19
+ Date:
20
+ - Sun, 26 Jan 2014 22:10:52 GMT
21
+ Content-Type:
22
+ - application/json; charset=utf-8
23
+ Content-Length:
24
+ - '302'
25
+ Connection:
26
+ - keep-alive
27
+ Vary:
28
+ - Accept-Encoding
29
+ Access-Control-Allow-Origin:
30
+ - ! '*'
31
+ body:
32
+ encoding: US-ASCII
33
+ string: ! "{\n \"datapath\": \"us.gov.whitehouse.visitor-list\",\n \"success\":
34
+ true,\n \"export_url\": \"www.example.com\"\n}"
35
+ http_version:
36
+ recorded_at: Sun, 26 Jan 2014 22:11:57 GMT
37
+ recorded_with: VCR 2.8.0