apidae 0.2.6 → 0.2.7

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: a65b8e65f96393ad346b2e3e75c2d9b03fa36917
4
- data.tar.gz: c294dbec3f0fb0ce086d219a64bf4d926d6be51e
3
+ metadata.gz: 2ffef235022a3e5e342a82ff46a5dfad3d370c32
4
+ data.tar.gz: 60a0d4200c5e339be76ee86029e14f9ae6df4118
5
5
  SHA512:
6
- metadata.gz: 816137aec948bde71a5c5ebd274f630f45c72df31ec19c29c162076fa44949fb64388ae68c8be7aa38665eebef470624e58c76fff0713b736e495edcd77beb14
7
- data.tar.gz: f7ee150be2b37199b8c21092f0c8760f642cb6dbfd680aaaf2e256f9f1135be7577b81ad3b80959bdbd59c72466bea52a6e6271ef9b2c00b3bca254650429681
6
+ metadata.gz: 4630f21df2b5684d1ccc37d3d6f60781e9ef43c9d373552122bb38e8ec72c453d8c80ed2453d495a5e0360b2afdf88151c17873d0ef46df737e1b447f0a37ffb
7
+ data.tar.gz: 57e84b1d31504a0f04e613c33a503ce32ecff246ccbb1e3685fa31dccf687af07f6afb3b2bdcee57935a421de95a14b36f897ad6a3a791e5f873fdf7fb5af6f4
@@ -0,0 +1,25 @@
1
+ require_dependency "apidae/application_controller"
2
+
3
+ module Apidae
4
+ class ApiController < ApplicationController
5
+
6
+ def selection
7
+ @objects = []
8
+ unless params[:selection_id].blank?
9
+ objects_ids = Apidae::Selection.find(params[:selection_id]).api_results[:results]
10
+ @objects = Apidae::Object.where(id: objects_ids)
11
+ end
12
+ end
13
+
14
+ def object
15
+ end
16
+
17
+ def agenda
18
+ @objects = []
19
+ unless params[:selection_id].blank?
20
+ objects_ids = Apidae::Selection.find(params[:selection_id]).api_agenda(params[:from], params[:to])[:results]
21
+ @objects = Apidae::Object.where(id: objects_ids)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module Apidae
2
+ module ApiHelper
3
+ end
4
+ end
@@ -7,7 +7,7 @@ module Apidae
7
7
  has_many :selections, class_name: 'Apidae::Selection', source: :apidae_selection, through: :apidae_selection_objects
8
8
 
9
9
  store_accessor :pictures_data, :pictures
10
- store_accessor :type_data, :categories, :themes, :capacite, :classement, :labels
10
+ store_accessor :type_data, :categories, :themes, :capacite, :classement, :classementPrefectoral, :labels
11
11
  store_accessor :entity_data, :entity_id, :entity_name
12
12
  store_accessor :contact, :telephone, :email, :website
13
13
  store_accessor :address, :address_fields
@@ -3,11 +3,12 @@ module Apidae
3
3
  has_many :apidae_selection_objects, class_name: 'Apidae::SelectionObject', foreign_key: :apidae_selection_id
4
4
  has_many :objects, class_name: 'Apidae::Object', source: :apidae_object, through: :apidae_selection_objects
5
5
 
6
+ AGENDA_ENDPOINT = 'agenda/detaille/list-identifiants'
7
+ SELECTION_ENDPOINT = 'recherche/list-identifiants'
6
8
  MAX_COUNT = 100
7
9
  MAX_LOOPS = 10
8
10
 
9
11
  validates_presence_of :apidae_id, :reference
10
-
11
12
  before_validation :generate_reference, on: :create
12
13
 
13
14
  def self.add_or_update(selection_data)
@@ -33,73 +34,103 @@ module Apidae
33
34
  end
34
35
  end
35
36
 
36
- def agenda(from, to)
37
- agenda_entries = Rails.cache.read("#{apidae_id}_#{from}_#{to}")
38
- if agenda_entries.nil?
39
- query_result = {}
40
- config = {
41
- url: "#{Rails.application.config.apidae_api_url}/agenda/detaille/list-identifiants",
42
- apiKey: Rails.application.config.apidae_api_key,
43
- projetId: Rails.application.config.apidae_project_id,
44
- first: 0,
45
- count: MAX_COUNT,
46
- selectionIds: [apidae_id],
47
- dateDebut: from,
48
- dateFin: to
49
- }
37
+ def results(where_clause, offset, size)
38
+ objects.includes(:town).limit(size).offset(offset).where(where_clause)
39
+ end
50
40
 
51
- loops = 0
41
+ def total(where_clause)
42
+ objects.where(where_clause).count
43
+ end
52
44
 
53
- response = JSON.parse get_response(config), symbolize_names: false
54
- total = response['numFound']
45
+ def api_results(opts = {})
46
+ query_args = build_args(SELECTION_ENDPOINT, opts.merge({selection_ids: [apidae_id]}))
47
+ query_api(query_args, true)
48
+ end
49
+
50
+ def api_agenda(from, to)
51
+ query_args = build_args(AGENDA_ENDPOINT, {selection_ids: [apidae_id], from: from, to: to})
52
+ query_api(query_args, true)
53
+ end
54
+
55
+ private
56
+
57
+ def query_api(query_args, all_results = false)
58
+ query_result = {}
55
59
 
60
+ if all_results
61
+ loops = 0
62
+ query_args[:first] = 0
63
+ query_args[:count] = MAX_COUNT
64
+ response = JSON.parse get_response(query_args), symbolize_names: false
65
+ total = response['numFound']
56
66
  query_result[:results] = response['objetTouristiqueIds'] || {}
57
67
 
58
- while total > query_result[:results].values.flatten.length && loops < MAX_LOOPS
68
+ while total > results_count(query_result) && loops < MAX_LOOPS
59
69
  loops += 1
60
- config[:first] += MAX_COUNT
61
- response = JSON.parse get_response(config), symbolize_names: false
70
+ query_args[:first] += MAX_COUNT
71
+ response = JSON.parse get_response(query_args), symbolize_names: false
62
72
  merge_results(response, query_result)
63
73
  end
64
74
  query_result[:count] = total
65
- agenda_entries = query_result
66
- Rails.cache.write("#{apidae_id}_#{from}_#{to}", query_result)
75
+ else
76
+ response = JSON.parse get_response(query_args), symbolize_names: false
77
+ query_result[:results] = response['objetTouristiqueIds'] || {}
78
+ query_result[:count] = response['numFound']
67
79
  end
68
- agenda_entries
69
- end
70
-
71
- def results(where_clause, offset, size)
72
- objects.includes(:town).limit(size).offset(offset).where(where_clause)
80
+ query_result
73
81
  end
74
82
 
75
- def total(where_clause)
76
- objects.where(where_clause).count
83
+ def results_count(result)
84
+ if result[:results] && result[:results].is_a?(Hash)
85
+ result[:results].values.flatten.length
86
+ else
87
+ result[:results].blank? ? 0 : result[:results].length
88
+ end
77
89
  end
78
90
 
79
- private
80
-
81
- def get_response(config)
91
+ def get_response(args)
82
92
  response = ''
83
- query = JSON.generate config.except(:url)
84
- logger.info "Agenda query : #{config[:url]}?query=#{query}"
85
- open("#{config[:url]}?query=#{CGI.escape query}") { |f|
93
+ query = JSON.generate args.except(:url)
94
+ logger.info "Apidae API query : #{args[:url]}?query=#{query}"
95
+ open("#{args[:url]}?query=#{CGI.escape query}") { |f|
86
96
  f.each_line {|line| response += line if line}
87
97
  }
88
98
  response
89
99
  end
90
100
 
91
- def merge_results(response, query_results)
92
- unless response['objetTouristiqueIds'].nil? || response['objetTouristiqueIds'].empty?
93
- first_day = response['objetTouristiqueIds'].keys.first
94
- if query_results[:results].has_key?(first_day)
95
- query_results[:results][first_day] += response['objetTouristiqueIds'][first_day]
101
+ def merge_results(response, result)
102
+ ids = response['objetTouristiqueIds']
103
+ unless ids.nil? || ids.empty?
104
+ if result[:results] && result[:results].is_a?(Hash)
105
+ first_day = ids.keys.first
106
+ if result[:results].has_key?(first_day)
107
+ result[:results][first_day] += ids[first_day]
108
+ else
109
+ result[:results][first_day] = ids[first_day]
110
+ end
111
+ result[:results].merge!(ids.except(first_day))
96
112
  else
97
- query_results[:results][first_day] = response['objetTouristiqueIds'][first_day]
113
+ result[:results] += ids
98
114
  end
99
- query_results[:results].merge!(response['objetTouristiqueIds'].except(first_day))
100
115
  end
101
116
  end
102
117
 
118
+ def build_args(endpoint, opts = {})
119
+ {
120
+ url: "#{Rails.application.config.apidae_api_url}/#{endpoint}",
121
+ apiKey: Rails.application.config.apidae_api_key,
122
+ projetId: Rails.application.config.apidae_project_id,
123
+ first: opts[:first] || 0,
124
+ count: opts[:count] || MAX_COUNT,
125
+ selectionIds: opts[:selection_ids],
126
+ dateDebut: opts[:from],
127
+ dateFin: opts[:to],
128
+ center: opts[:lat] && opts[:lng] ? {type: 'Point', coordinates: [opts[:lng], opts[:lat]]} : nil,
129
+ radius: opts[:radius] ? opts[:radius].to_i : nil,
130
+ responseFields: opts[:fields] || ['id']
131
+ }
132
+ end
133
+
103
134
  def generate_reference
104
135
  self.reference ||= (self.label.parameterize || self.apidae_id)
105
136
  end
@@ -1,3 +1,3 @@
1
1
  module Apidae
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ module Apidae
4
+ class ApiControllerTest < ActionDispatch::IntegrationTest
5
+ include Engine.routes.url_helpers
6
+
7
+ test "should get selection" do
8
+ get api_selection_url
9
+ assert_response :success
10
+ end
11
+
12
+ test "should get object" do
13
+ get api_object_url
14
+ assert_response :success
15
+ end
16
+
17
+ test "should get agenda" do
18
+ get api_agenda_url
19
+ assert_response :success
20
+ end
21
+
22
+ end
23
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apidae
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Baptiste Vilain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-23 00:00:00.000000000 Z
11
+ date: 2018-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -79,11 +79,13 @@ files:
79
79
  - app/assets/images/apidae/logo-apidae-grey.svg
80
80
  - app/assets/javascripts/apidae/application.js
81
81
  - app/assets/stylesheets/apidae/application.css
82
+ - app/controllers/apidae/api_controller.rb
82
83
  - app/controllers/apidae/application_controller.rb
83
84
  - app/controllers/apidae/dashboard_controller.rb
84
85
  - app/controllers/apidae/import_controller.rb
85
86
  - app/controllers/apidae/objects_controller.rb
86
87
  - app/controllers/apidae/selections_controller.rb
88
+ - app/helpers/apidae/api_helper.rb
87
89
  - app/helpers/apidae/application_helper.rb
88
90
  - app/helpers/apidae/dashboard_helper.rb
89
91
  - app/helpers/apidae/import_helper.rb
@@ -132,6 +134,7 @@ files:
132
134
  - lib/apidae/version.rb
133
135
  - lib/tasks/apidae_tasks.rake
134
136
  - test/apidae_test.rb
137
+ - test/controllers/apidae/api_controller_test.rb
135
138
  - test/controllers/apidae/dashboard_controller_test.rb
136
139
  - test/controllers/apidae/import_controller_test.rb
137
140
  - test/controllers/apidae/objects_controller_test.rb
@@ -275,6 +278,7 @@ test_files:
275
278
  - test/test_helper.rb
276
279
  - test/apidae_test.rb
277
280
  - test/controllers/apidae/selections_controller_test.rb
281
+ - test/controllers/apidae/api_controller_test.rb
278
282
  - test/controllers/apidae/objects_controller_test.rb
279
283
  - test/controllers/apidae/dashboard_controller_test.rb
280
284
  - test/controllers/apidae/import_controller_test.rb