commcare_api 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a1a094c4aabb3912660bcc1781b4ca9e161328f
4
- data.tar.gz: 76f4b639d4b5ffa84b3b2dcf5873c43ccf016a11
3
+ metadata.gz: 198967693f77f06157318d0091075344dd8f54e4
4
+ data.tar.gz: 9793c8b2f0c86809e228b6d43618978f8ed8a4b5
5
5
  SHA512:
6
- metadata.gz: 84162557fad10cc384b6c07129181ea90d98b73dd7a69517c6bf95d840503074c5f63de9702941289ec7d994892849340e88fb2fa4617577670dd4e7e0cc2897
7
- data.tar.gz: fed262d16c1f1d84d30b56d99104c9843a03373af1c70ce496d1d2ffb472efa888c8be00d2e1d79e9aa8c152faf40c49ab5d11abeaf8ff6600dffd8ccd78cf0f
6
+ metadata.gz: 2e4e78a4b6dd1eabc1a45c0c9462777ee8a9cbbb50ceefdb6d7536905941c6e315c139400479d72fb3bbd1604f080d133239143731b8c3d6772a5726dd47c363
7
+ data.tar.gz: 44d9a1a88059b7cf835f85ae1bd8f6cac0cbc48fed940c8ff824560d8ca79c9a5d112938fa91cd8c7116dd63145653451c8d47b76e0632810846173340291582
data/README.md CHANGED
@@ -32,13 +32,29 @@ Or install it yourself as:
32
32
  ## Usage
33
33
 
34
34
  require 'commcare_api'
35
-
35
+
36
36
  ccc = CommcareApi::CommcareConnector.new(username, password)
37
37
  response = ccc.get_cases("my_domain", type: "my_case_type", date_modified_start: "2014-05-30", limit: 15)
38
38
  puts response.body
39
39
 
40
40
  Filters are added to the request with an options hash as shown above. See [CommCare documentation](https://wiki.commcarehq.org/display/commcarepublic/CommCare+HQ+APIs) for possible filters.
41
41
 
42
+ ## Getting data in multiple queries
43
+
44
+ If the total amount of data you want cannot be retrieved with a single query you will have to perform multiple queries.
45
+
46
+ require 'commcare_api'
47
+
48
+ ccc = CommcareApi::CommcareConnector.new(username, password)
49
+ response = ccc.get_cases("my_domain", type: "my_case_type", date_modified_start: "2014-05-30", limit: 15)
50
+
51
+ while !response.nil? do
52
+ # Get the data you need
53
+ response = ccc.get_next_data
54
+ end
55
+
56
+ You can also use the `get_previous_data` method similarly.
57
+
42
58
  ## Contributing
43
59
 
44
60
  1. Fork it (http://github.com/gdeflaux/commcare_api/fork)
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'rspec'
24
24
  spec.add_dependency "httpi"
25
25
  spec.add_dependency "httpclient"
26
+ spec.add_dependency "json"
26
27
  end
@@ -1,6 +1,7 @@
1
1
  require "commcare_api/version"
2
2
  require "httpi"
3
3
  require "httpclient"
4
+ require "json"
4
5
 
5
6
  module CommcareApi
6
7
 
@@ -12,6 +13,16 @@ module CommcareApi
12
13
  @user = user
13
14
  @password = password
14
15
  @version = "v#{version}"
16
+ @last_response = nil
17
+ @last_request = nil
18
+ end
19
+
20
+ def get_next_data
21
+ get_contiguous_data("next")
22
+ end
23
+
24
+ def get_previous_data
25
+ get_contiguous_data("previous")
15
26
  end
16
27
 
17
28
  def get_cases(domain, options = {})
@@ -90,6 +101,8 @@ module CommcareApi
90
101
  request = HTTPI::Request.new(url)
91
102
  request.auth.digest(@user, @password)
92
103
  response = HTTPI.get(request, :httpclient)
104
+ @last_response = response
105
+ @last_request = request
93
106
  response
94
107
  end
95
108
 
@@ -100,6 +113,15 @@ module CommcareApi
100
113
  url
101
114
  end
102
115
 
116
+ def get_contiguous_data(direction)
117
+ return nil if @last_response.nil?
118
+ j = JSON.parse(@last_response.body)
119
+ return nil if j["meta"][direction].nil?
120
+ @last_request.url.query = j["meta"][direction].gsub(/\?/, "")
121
+ url = @last_request.url.to_s
122
+ get_request(url)
123
+ end
124
+
103
125
  end # Class
104
126
 
105
127
  end # Module
@@ -1,3 +1,3 @@
1
1
  module CommcareApi
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -1,8 +1,26 @@
1
+ # The commcare domain must have a least 2 cases
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe CommcareApi do
4
6
  ccc = CommcareApi::CommcareConnector.new(ENV["CC_USER"], ENV["CC_PWD"])
5
7
 
8
+ r = ccc.get_cases(ENV["CC_DOMAIN"], offset: 100, limit: 1)
9
+ puts r.code
10
+ j = JSON.parse(r.body)
11
+
12
+ puts JSON.pretty_generate(j)
13
+
14
+ it "get_next_data (nil)" do
15
+ r = ccc.get_next_data
16
+ expect(r.class).to eq nil.class
17
+ end
18
+
19
+ it "get_previous_data (nil)" do
20
+ r = ccc.get_previous_data
21
+ expect(r.class).to eq nil.class
22
+ end
23
+
6
24
  case_id = ""
7
25
  it "get_cases" do
8
26
  r = ccc.get_cases(ENV["CC_DOMAIN"])
@@ -27,6 +45,18 @@ describe CommcareApi do
27
45
  expect(r.code).to eq 200
28
46
  end
29
47
 
48
+ it "get_previous_data" do
49
+ r = ccc.get_cases(ENV["CC_DOMAIN"], limit: 1)
50
+ r = ccc.get_next_data
51
+ expect(r.code).to eq 200
52
+ end
53
+
54
+ it "get_previous_data" do
55
+ r = ccc.get_cases(ENV["CC_DOMAIN"], limit: 1, offset: 1)
56
+ r = ccc.get_previous_data
57
+ expect(r.code).to eq 200
58
+ end
59
+
30
60
  it "get_groups" do
31
61
  r = ccc.get_groups(ENV["CC_DOMAIN"])
32
62
  expect(r.code).to eq 200
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commcare_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillaume Deflaux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-03 00:00:00.000000000 Z
11
+ date: 2016-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: A CommCare API wrapper in Ruby
84
98
  email:
85
99
  - deflaux.guillaume@gmail.com
@@ -117,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
131
  version: '0'
118
132
  requirements: []
119
133
  rubyforge_project:
120
- rubygems_version: 2.2.1
134
+ rubygems_version: 2.6.6
121
135
  signing_key:
122
136
  specification_version: 4
123
137
  summary: A CommCare API wrapper in Ruby