clever-ruby 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +13 -5
  2. data/CHANGELOG.md +4 -0
  3. data/README.md +1 -1
  4. data/lib/clever-ruby.rb +1 -0
  5. data/lib/clever-ruby/api_operations/list.rb +24 -0
  6. data/lib/clever-ruby/api_operations/pagelist.rb +6 -2
  7. data/lib/clever-ruby/api_operations/results_list.rb +20 -0
  8. data/lib/clever-ruby/util.rb +4 -0
  9. data/lib/clever-ruby/version.rb +1 -1
  10. data/test/data/vcr_cassettes/district_count.yml +44 -0
  11. data/test/data/vcr_cassettes/district_find_by_page.yml +85 -0
  12. data/test/data/vcr_cassettes/district_find_multiple.yml +85 -0
  13. data/test/data/vcr_cassettes/district_find_one.yml +85 -0
  14. data/test/data/vcr_cassettes/districts.yml +46 -5
  15. data/test/data/vcr_cassettes/districts_event_pages.yml +134 -134
  16. data/test/data/vcr_cassettes/districts_events.yml +44 -44
  17. data/test/data/vcr_cassettes/districts_school_pages.yml +50 -50
  18. data/test/data/vcr_cassettes/districts_schools.yml +44 -44
  19. data/test/data/vcr_cassettes/districts_section_pages.yml +303 -303
  20. data/test/data/vcr_cassettes/districts_sections.yml +44 -44
  21. data/test/data/vcr_cassettes/districts_student_pages.yml +184 -184
  22. data/test/data/vcr_cassettes/districts_students.yml +44 -44
  23. data/test/data/vcr_cassettes/districts_students_filtered.yml +25 -25
  24. data/test/data/vcr_cassettes/districts_teacher_pages.yml +100 -100
  25. data/test/data/vcr_cassettes/districts_teachers.yml +44 -44
  26. data/test/data/vcr_cassettes/error_handling.yml +24 -24
  27. data/test/data/vcr_cassettes/event_count.yml +44 -0
  28. data/test/data/vcr_cassettes/event_find_by_page.yml +110 -0
  29. data/test/data/vcr_cassettes/event_find_multiple.yml +124 -0
  30. data/test/data/vcr_cassettes/event_find_one.yml +114 -0
  31. data/test/data/vcr_cassettes/events.yml +110 -0
  32. data/test/data/vcr_cassettes/school_count.yml +44 -0
  33. data/test/data/vcr_cassettes/school_find_by_page.yml +96 -0
  34. data/test/data/vcr_cassettes/school_find_multiple.yml +104 -0
  35. data/test/data/vcr_cassettes/school_find_one.yml +99 -0
  36. data/test/data/vcr_cassettes/schools.yml +48 -7
  37. data/test/data/vcr_cassettes/schools_optional_attributes.yml +7 -7
  38. data/test/data/vcr_cassettes/section_count.yml +44 -0
  39. data/test/data/vcr_cassettes/section_find_by_page.yml +1146 -0
  40. data/test/data/vcr_cassettes/section_find_multiple.yml +147 -0
  41. data/test/data/vcr_cassettes/section_find_one.yml +352 -0
  42. data/test/data/vcr_cassettes/sections.yml +69 -28
  43. data/test/data/vcr_cassettes/student_count.yml +44 -0
  44. data/test/data/vcr_cassettes/student_find_by_page.yml +1171 -0
  45. data/test/data/vcr_cassettes/student_find_multiple.yml +111 -0
  46. data/test/data/vcr_cassettes/student_find_one.yml +160 -0
  47. data/test/data/vcr_cassettes/students.yml +118 -77
  48. data/test/data/vcr_cassettes/teacher_count.yml +44 -0
  49. data/test/data/vcr_cassettes/teacher_find_by_page.yml +176 -0
  50. data/test/data/vcr_cassettes/teacher_find_multiple.yml +119 -0
  51. data/test/data/vcr_cassettes/teacher_find_one.yml +177 -0
  52. data/test/data/vcr_cassettes/teachers.yml +15 -3712
  53. data/test/integration/api_operations/list_test.rb +84 -28
  54. metadata +82 -30
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4bb241ee16e9429513d5f73121b0ba2d422219a2
4
- data.tar.gz: 57d5a3e97eed2e039ff0af060e78118086403509
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTI0OGY0NTY3MDg0ODc1NGE0OTJlNmE1NGQxMjVmZGJlNGFjMzFlMA==
5
+ data.tar.gz: !binary |-
6
+ ZTJjNGFhNTlkZTMxYWM0NjMxNjVjOGZmNmI1NjUyMTg4ZjIwZWI0YQ==
5
7
  SHA512:
6
- metadata.gz: c5ad379292df7b04672d323a37d4472d9465a2aafa50e1d82fbacf6c637540b7e08dbf714483ab3003bff3e3f78481c1e30e9eaeb53f19c37159bbcc58682875
7
- data.tar.gz: b4e6f23589a1a7add9041fbf2a151e3f7b5a80458576196552a882d5cc02ca88877cffaef2d47a614bbb3d2d078014e5d0856834087ad5bb0c334a17c685afb5
8
+ metadata.gz: !binary |-
9
+ NGNiNGM5OGM3ZjYwNjYyZGIyMmZlM2VjZmFmNWQxNjdhMjBmY2M1ZDBlMDY0
10
+ NWZhZTM4MTE3MmIyOTkxNTQ0ODdhODAxODVhNGI0N2U1OGY3NjdhMWM1Njc3
11
+ ZTlmZTY0NjQxZjUyNTliNGRiZjI1MTIzZDllYzRiNDU2MjVjOWM=
12
+ data.tar.gz: !binary |-
13
+ NGZjMjNkYjgxNGQxYWE0NWY2MGZkNjgzNzZmMThlZDIyZGI5MTk3OTcwMzI2
14
+ OTJlZGMwZjQwZjI1NDMyNjVjZGZjN2E3ZTVhMDMyNzg3OWVjOGM1NGViNTBk
15
+ MzdkNWE3ODhkZGExZGVlNzk4NWMyZGYwMDQwOGE5NjlkMDUyOTY=
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.6.0 (2014-09-09)
2
+
3
+ * Add ActiveRecord-style find and count methods to all APIResources (#37)
4
+
1
5
  ## 0.5.0 (2014-09-09)
2
6
 
3
7
  * Handle pagination through rel links, not manual numeric pagination (#36)
data/README.md CHANGED
@@ -60,6 +60,6 @@ end
60
60
 
61
61
  1. Fork it
62
62
  2. Create your feature branch (`git checkout -b my-new-feature`)
63
- 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 3. Commit your changes (`git add -p ./path/to/files; git commit -m 'Add some feature'`)
64
64
  4. Push to the branch (`git push origin my-new-feature`)
65
65
  5. Create new Pull Request
data/lib/clever-ruby.rb CHANGED
@@ -11,6 +11,7 @@ require 'clever-ruby/version'
11
11
  # API operations
12
12
  require 'clever-ruby/api_operations/list'
13
13
  require 'clever-ruby/api_operations/pagelist'
14
+ require 'clever-ruby/api_operations/results_list'
14
15
  require 'clever-ruby/api_operations/page'
15
16
 
16
17
  # Helpers
@@ -11,6 +11,30 @@ module Clever
11
11
  end
12
12
  accum
13
13
  end
14
+
15
+ def find(id = nil, filters = {})
16
+ if id.is_a? Array
17
+ unless id.select { |e| !Clever::Util.valid_id? e }.empty?
18
+ fail ArgumentError, 'Array of IDs must only contain valid ObjectIDs'
19
+ end
20
+
21
+ filters[:where] = JSON.dump(_id: { :'$in' => id })
22
+ end
23
+
24
+ if id.nil? || id.is_a?(Array)
25
+ Clever::APIOperations::PageList.new(url, filters).to_results_list
26
+ elsif Clever::Util.valid_id? id
27
+ retrieve id
28
+ else
29
+ fail ArgumentError, 'id must be a valid ObjectID, an array of them, or nil.'
30
+ end
31
+ end
32
+
33
+ def count(filters = {})
34
+ filters[:count] = true
35
+ response = Clever.request :get, url, filters
36
+ response[:count]
37
+ end
14
38
  end
15
39
 
16
40
  def self.included(base)
@@ -1,9 +1,9 @@
1
1
  module Clever
2
2
  module APIOperations
3
3
  # Handles paginated requests.
4
- # TODO: use rel links
5
- # TODO: build functionality elsewhere
6
4
  class PageList
5
+ include Enumerable
6
+
7
7
  def initialize(uri, filters = {})
8
8
  @uri = uri
9
9
  @filters = filters
@@ -16,6 +16,10 @@ module Clever
16
16
  page = page.next
17
17
  end
18
18
  end
19
+
20
+ def to_results_list
21
+ Clever::APIOperations::ResultsList.new self
22
+ end
19
23
  end
20
24
  end
21
25
  end
@@ -0,0 +1,20 @@
1
+ module Clever
2
+ module APIOperations
3
+ # Represents a list of results for a paged request.
4
+ class ResultsList
5
+ include Enumerable
6
+
7
+ def initialize(pagelist)
8
+ @pages = pagelist
9
+ end
10
+
11
+ def each
12
+ @pages.each do |page|
13
+ page.each do |elem|
14
+ yield elem
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,6 +1,10 @@
1
1
  module Clever
2
2
  # LibraryhHelper methods
3
3
  module Util
4
+ def self.valid_id?(id)
5
+ id.is_a?(String) && !(/^[0-9a-fA-F]{24}$/.match(id).nil?)
6
+ end
7
+
4
8
  def self.objects_to_ids(h)
5
9
  case h
6
10
  when APIResource
@@ -1,4 +1,4 @@
1
1
  # Clever Ruby library
2
2
  module Clever
3
- VERSION = '0.5.0'
3
+ VERSION = '0.6.0'
4
4
  end
@@ -0,0 +1,44 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://DEMO_KEY:@api.clever.com/v1.1/districts?count=true
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - "*/*; q=0.5, application/xml"
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Access-Control-Allow-Headers:
22
+ - Content-Type,Authorization,X-Requested-With,Accept,Origin,Referer,User-Agent
23
+ Access-Control-Allow-Methods:
24
+ - GET,PATCH,POST,DELETE
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Date:
30
+ - Tue, 09 Sep 2014 21:48:49 GMT
31
+ Server:
32
+ - nginx/1.4.7
33
+ X-Powered-By:
34
+ - Express
35
+ Content-Length:
36
+ - '71'
37
+ Connection:
38
+ - keep-alive
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{"count":1,"links":[{"rel":"self","uri":"/v1.1/districts?count=true"}]}'
42
+ http_version:
43
+ recorded_at: Tue, 09 Sep 2014 21:48:49 GMT
44
+ recorded_with: VCR 2.4.0
@@ -0,0 +1,85 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://DEMO_KEY:@api.clever.com/v1.1/districts
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - "*/*; q=0.5, application/xml"
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Access-Control-Allow-Headers:
22
+ - Content-Type,Authorization,X-Requested-With,Accept,Origin,Referer,User-Agent
23
+ Access-Control-Allow-Methods:
24
+ - GET,PATCH,POST,DELETE
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Date:
30
+ - Tue, 09 Sep 2014 21:48:53 GMT
31
+ Server:
32
+ - nginx/1.4.7
33
+ X-Powered-By:
34
+ - Express
35
+ Content-Length:
36
+ - '217'
37
+ Connection:
38
+ - keep-alive
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{"data":[{"data":{"name":"Demo District","id":"4fd43cc56d11340000000005"},"uri":"/v1.1/districts/4fd43cc56d11340000000005"}],"paging":{"current":1,"total":1,"count":1},"links":[{"rel":"self","uri":"/v1.1/districts"}]}'
42
+ http_version:
43
+ recorded_at: Tue, 09 Sep 2014 21:48:53 GMT
44
+ - request:
45
+ method: get
46
+ uri: https://DEMO_KEY:@api.clever.com/v1.1/districts?count=true
47
+ body:
48
+ encoding: US-ASCII
49
+ string: ''
50
+ headers:
51
+ Accept:
52
+ - "*/*; q=0.5, application/xml"
53
+ Accept-Encoding:
54
+ - gzip, deflate
55
+ User-Agent:
56
+ - Ruby
57
+ response:
58
+ status:
59
+ code: 200
60
+ message: OK
61
+ headers:
62
+ Access-Control-Allow-Headers:
63
+ - Content-Type,Authorization,X-Requested-With,Accept,Origin,Referer,User-Agent
64
+ Access-Control-Allow-Methods:
65
+ - GET,PATCH,POST,DELETE
66
+ Access-Control-Allow-Origin:
67
+ - "*"
68
+ Content-Type:
69
+ - application/json; charset=utf-8
70
+ Date:
71
+ - Tue, 09 Sep 2014 21:48:53 GMT
72
+ Server:
73
+ - nginx/1.4.7
74
+ X-Powered-By:
75
+ - Express
76
+ Content-Length:
77
+ - '71'
78
+ Connection:
79
+ - keep-alive
80
+ body:
81
+ encoding: UTF-8
82
+ string: '{"count":1,"links":[{"rel":"self","uri":"/v1.1/districts?count=true"}]}'
83
+ http_version:
84
+ recorded_at: Tue, 09 Sep 2014 21:48:53 GMT
85
+ recorded_with: VCR 2.4.0
@@ -0,0 +1,85 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://DEMO_KEY:@api.clever.com/v1.1/districts?limit=20
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - "*/*; q=0.5, application/xml"
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Access-Control-Allow-Headers:
22
+ - Content-Type,Authorization,X-Requested-With,Accept,Origin,Referer,User-Agent
23
+ Access-Control-Allow-Methods:
24
+ - GET,PATCH,POST,DELETE
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Date:
30
+ - Tue, 09 Sep 2014 21:48:53 GMT
31
+ Server:
32
+ - nginx/1.4.7
33
+ X-Powered-By:
34
+ - Express
35
+ Content-Length:
36
+ - '226'
37
+ Connection:
38
+ - keep-alive
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{"data":[{"data":{"name":"Demo District","id":"4fd43cc56d11340000000005"},"uri":"/v1.1/districts/4fd43cc56d11340000000005"}],"paging":{"current":1,"total":1,"count":1},"links":[{"rel":"self","uri":"/v1.1/districts?limit=20"}]}'
42
+ http_version:
43
+ recorded_at: Tue, 09 Sep 2014 21:48:53 GMT
44
+ - request:
45
+ method: get
46
+ uri: https://DEMO_KEY:@api.clever.com/v1.1/districts?where=%7B%22_id%22:%7B%22$in%22:%5B%224fd43cc56d11340000000005%22%5D%7D%7D
47
+ body:
48
+ encoding: US-ASCII
49
+ string: ''
50
+ headers:
51
+ Accept:
52
+ - "*/*; q=0.5, application/xml"
53
+ Accept-Encoding:
54
+ - gzip, deflate
55
+ User-Agent:
56
+ - Ruby
57
+ response:
58
+ status:
59
+ code: 200
60
+ message: OK
61
+ headers:
62
+ Access-Control-Allow-Headers:
63
+ - Content-Type,Authorization,X-Requested-With,Accept,Origin,Referer,User-Agent
64
+ Access-Control-Allow-Methods:
65
+ - GET,PATCH,POST,DELETE
66
+ Access-Control-Allow-Origin:
67
+ - "*"
68
+ Content-Type:
69
+ - application/json; charset=utf-8
70
+ Date:
71
+ - Tue, 09 Sep 2014 21:48:53 GMT
72
+ Server:
73
+ - nginx/1.4.7
74
+ X-Powered-By:
75
+ - Express
76
+ Content-Length:
77
+ - '288'
78
+ Connection:
79
+ - keep-alive
80
+ body:
81
+ encoding: UTF-8
82
+ string: '{"data":[{"data":{"name":"Demo District","id":"4fd43cc56d11340000000005"},"uri":"/v1.1/districts/4fd43cc56d11340000000005"}],"paging":{"current":1,"total":1,"count":1},"links":[{"rel":"self","uri":"/v1.1/districts?where=%7B%22_id%22:%7B%22$in%22:[%224fd43cc56d11340000000005%22]%7D%7D"}]}'
83
+ http_version:
84
+ recorded_at: Tue, 09 Sep 2014 21:48:53 GMT
85
+ recorded_with: VCR 2.4.0
@@ -0,0 +1,85 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://DEMO_KEY:@api.clever.com/v1.1/districts
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - "*/*; q=0.5, application/xml"
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Access-Control-Allow-Headers:
22
+ - Content-Type,Authorization,X-Requested-With,Accept,Origin,Referer,User-Agent
23
+ Access-Control-Allow-Methods:
24
+ - GET,PATCH,POST,DELETE
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Date:
30
+ - Tue, 09 Sep 2014 21:48:51 GMT
31
+ Server:
32
+ - nginx/1.4.7
33
+ X-Powered-By:
34
+ - Express
35
+ Content-Length:
36
+ - '217'
37
+ Connection:
38
+ - keep-alive
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{"data":[{"data":{"name":"Demo District","id":"4fd43cc56d11340000000005"},"uri":"/v1.1/districts/4fd43cc56d11340000000005"}],"paging":{"current":1,"total":1,"count":1},"links":[{"rel":"self","uri":"/v1.1/districts"}]}'
42
+ http_version:
43
+ recorded_at: Tue, 09 Sep 2014 21:48:51 GMT
44
+ - request:
45
+ method: get
46
+ uri: https://DEMO_KEY:@api.clever.com/v1.1/districts/4fd43cc56d11340000000005
47
+ body:
48
+ encoding: US-ASCII
49
+ string: ''
50
+ headers:
51
+ Accept:
52
+ - "*/*; q=0.5, application/xml"
53
+ Accept-Encoding:
54
+ - gzip, deflate
55
+ User-Agent:
56
+ - Ruby
57
+ response:
58
+ status:
59
+ code: 200
60
+ message: OK
61
+ headers:
62
+ Access-Control-Allow-Headers:
63
+ - Content-Type,Authorization,X-Requested-With,Accept,Origin,Referer,User-Agent
64
+ Access-Control-Allow-Methods:
65
+ - GET,PATCH,POST,DELETE
66
+ Access-Control-Allow-Origin:
67
+ - "*"
68
+ Content-Type:
69
+ - application/json; charset=utf-8
70
+ Date:
71
+ - Tue, 09 Sep 2014 21:48:51 GMT
72
+ Server:
73
+ - nginx/1.4.7
74
+ X-Powered-By:
75
+ - Express
76
+ Content-Length:
77
+ - '518'
78
+ Connection:
79
+ - keep-alive
80
+ body:
81
+ encoding: UTF-8
82
+ string: '{"data":{"name":"Demo District","id":"4fd43cc56d11340000000005"},"links":[{"rel":"self","uri":"/v1.1/districts/4fd43cc56d11340000000005"},{"rel":"schools","uri":"/v1.1/districts/4fd43cc56d11340000000005/schools"},{"rel":"teachers","uri":"/v1.1/districts/4fd43cc56d11340000000005/teachers"},{"rel":"students","uri":"/v1.1/districts/4fd43cc56d11340000000005/students"},{"rel":"sections","uri":"/v1.1/districts/4fd43cc56d11340000000005/sections"},{"rel":"events","uri":"/v1.1/districts/4fd43cc56d11340000000005/events"}]}'
83
+ http_version:
84
+ recorded_at: Tue, 09 Sep 2014 21:48:51 GMT
85
+ recorded_with: VCR 2.4.0