sitra_client 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sitra_client.rb CHANGED
@@ -6,6 +6,11 @@ require 'logger'
6
6
 
7
7
  module SitraClient
8
8
 
9
+ MAX_COUNT = 100
10
+
11
+ # Safety net
12
+ MAX_LOOPS = 5
13
+
9
14
  # Configuration defaults
10
15
  @config = {
11
16
  :base_url => 'http://api.sitra-tourisme.com/api/v001',
@@ -25,13 +30,35 @@ module SitraClient
25
30
  @config
26
31
  end
27
32
 
28
- def self.query(criteria = {})
33
+ def self.query(criteria, all_results = false)
34
+ if all_results
35
+ loops = 0
36
+ criteria[:first] = 0
37
+ criteria[:count] = MAX_COUNT
38
+ response = get_response(criteria)
39
+ results = response.as_array
40
+ while response.results_count > results.length && loops < MAX_LOOPS
41
+ loops += 1
42
+ criteria[:first] += MAX_COUNT
43
+ results += get_response(criteria).as_array
44
+ end
45
+ else
46
+ response = get_response(criteria)
47
+ results = response.as_array
48
+ end
49
+ results
50
+ end
51
+
52
+ private
53
+
54
+ def self.get_response(criteria)
29
55
  response = SitraResponse.new
30
56
  query = SitraQuery.new(@config[:api_key], @config[:site_identifier], criteria)
31
57
  @logger.info "Search query : #{@config[:base_url]}/recherche/list-objets-touristiques?query=#{query.to_params}"
32
58
  open("#{@config[:base_url]}/recherche/list-objets-touristiques?query=#{CGI.escape query.to_params}") { |f|
33
59
  f.each_line {|line| response.append_line(line)}
34
60
  }
61
+ @logger.info "Retrieved #{response.returned_count} of #{response.results_count} results"
35
62
  response
36
63
  end
37
64
 
@@ -6,14 +6,26 @@ class SitraResponse
6
6
 
7
7
  def initialize
8
8
  @json_response = ''
9
+ @response_hash = {}
9
10
  end
10
11
 
11
12
  def append_line(line)
12
13
  @json_response += line unless line.nil?
13
14
  end
14
15
 
16
+ def returned_count
17
+ [as_hash[:query][:count], results_count - as_hash[:query][:first]].min
18
+ end
19
+
20
+ def results_count
21
+ as_hash[:numFound]
22
+ end
23
+
15
24
  def as_hash
16
- JSON.parse @json_response, :symbolize_names => true
25
+ if @response_hash.empty?
26
+ @response_hash = JSON.parse @json_response, :symbolize_names => true
27
+ end
28
+ @response_hash
17
29
  end
18
30
 
19
31
  def as_raw_json
@@ -21,11 +33,10 @@ class SitraResponse
21
33
  end
22
34
 
23
35
  def as_array
24
- response = as_hash
25
- if response[:objetsTouristiques].nil?
36
+ if as_hash[:objetsTouristiques].nil?
26
37
  results = []
27
38
  else
28
- results = response[:objetsTouristiques].collect {|obj_hash| TouristicObject.new(obj_hash)}
39
+ results = as_hash[:objetsTouristiques].collect {|obj_hash| TouristicObject.new(obj_hash)}
29
40
  end
30
41
  results
31
42
  end
@@ -1,3 +1,3 @@
1
1
  module SitraClient
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -59,6 +59,8 @@ class SitraResponseTest < Test::Unit::TestCase
59
59
  sitra_response.append_line(json_response)
60
60
 
61
61
  touristic_objects = sitra_response.as_array
62
+
63
+ assert_equal 2, sitra_response.results_count
62
64
  assert_empty touristic_objects
63
65
 
64
66
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sitra_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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: 2014-07-24 00:00:00.000000000 Z
12
+ date: 2014-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler