Class: INat::Query
Instance Method Summary
collapse
#radius, radius, #sector, sector
#debug, debug, echo, #echo, error, #error, #info, info, log, #log, #warning, warning
Constructor Details
#initialize(**params) ⇒ Query
Returns a new instance of Query.
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
|
# File 'lib/inat/data/query.rb', line 854
def initialize **params
@@counter ||= 0
@@counter += 1
@int_key = @@counter
@api_params = {}
@db_where = []
@r_match = []
debug "Initializing Query: #{ params.inspect }"
params.each do |key, value|
key = key.intern
case key
when :acc, :accuracy, :positional_accuracy
parse_accuracy value
when :acc_above
parse_acc_above value
when :acc_below
parse_acc_below value
when :acc_below_or_unknown
parse_acc_below_or_unknown value
when :captive
parse_captive value
when :endemic
parse_endemic value
when :introduced
parse_introduced value
when :native
parse_native value
when :mappable
parse_mappable value
when :geo, :location
parse_location value
when :lat, :lng, :radius
raise ArgumentError, "Query parameter '#{ key }' is unsupported. Use 'location' with Radius value instead!", caller
when :nelat, :nelng, :swlat, :swlng
raise ArgumentError, "Query parameter '#{ key }' is unsupported. Use 'location' with Sector value instead!", caller
when :popular
parse_popular value
when :photos
parse_photos value
when :sounds
parse_sounds value
when :taxon_is_active
parse_taxon_is_active value
when :threatened
parse_threatened value
when :verifiable
parse_verifiable value
when :licensed
parse_licensed value
when :photo_licensed
parse_photo_licensed value
when :id
parse_id value
when :not_id
parse_not_id value
when :license
parse_license value
when :photo_license
parse_photo_license value
when :sound_license
parse_sound_license value
when :place_id
parse_place_id value
when :place
parse_place value
when :project_id
parse_project_id value
when :project
parse_project value
when :rank
parse_rank value
when :hrank
parse_hrank value
when :lrank
parse_lrank value
when :taxon_id
parse_taxon_id value
when :taxon
parse_taxon value
when :without_taxon_id
parse_without_taxon_id value
when :without_taxon
parse_without_taxon value
when :taxon_name
parse_taxon_name value
when :user_id, :user_login
parse_user_login value
when :user
parse_user value
when :ident_user_id
parse_ident_user_id value
when :ident_user
parse_ident_user value
when :day
parse_day value
when :month
parse_month value
when :year
parse_year value
when :d1
parse_d1 value
when :d2
parse_d2 value
when :observed_on, :date
parse_observed_on value
when :created_d1
parse_created_d1 value
when :created_d2
parse_created_d2 value
when :created_on, :created_at
parse_created_on value
when :geoprivacy
parse_geoprivacy value
when :taxon_geoprivacy
parse_taxon_geoprivacy value
when :iconic_taxa
parse_iconic_taxa value
when :quality_grade
parse_quality_grade value
else
raise ArgumentError, "Query parameter '#{ key }' is unknown or not supported!", caller
end
end
end
|
Instance Method Details
#===(observation) ⇒ Object
1067
1068
1069
|
# File 'lib/inat/data/query.rb', line 1067
def === observation
match? observation
end
|
#api_query ⇒ Object
1048
1049
1050
1051
|
# File 'lib/inat/data/query.rb', line 1048
def api_query
@api_params.map { |k, v| "#{ k }=#{ v.to_query }" }.sort.join("&")
end
|
#array_covers(own, other) ⇒ Object
1014
1015
1016
1017
1018
|
# File 'lib/inat/data/query.rb', line 1014
private def array_covers own, other
own = own.map { |i| i.to_s }
other = other.map { |i| i.to_s }
other.all? { |i| own.include?(i) }
end
|
#db_where ⇒ Object
1053
1054
1055
1056
1057
1058
1059
1060
1061
|
# File 'lib/inat/data/query.rb', line 1053
def db_where
sql = []
sql_args = []
@db_where.each do |item|
sql << item[0]
sql_args << item[1]
end
[ sql.map { |s| "(#{ s })" }.join(' AND '), sql_args.flatten ]
end
|
#iconic_taxa(value) ⇒ Object
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
|
# File 'lib/inat/data/query.rb', line 823
private def iconic_taxa value
case value
when IconicTaxa
@api_params[:iconic_taxa] = value
warning "DB selector for 'iconic_taxa' is not implemented..."
@r_match << lambda { |o| o.taxon.iconic_taxon_name == value }
when Array
@api_params[:iconic_taxa] = value
warning "DB selector for 'iconic_taxa' is not implemented..."
@r_match << lambda { |o| value.include?(o.taxon.iconic_taxon_name) }
else
raise TypeError, "Invalid 'iconic_taxa' type: #{ value.inspect }!", caller[1..]
end
end
|
#in?(query_string) ⇒ Boolean
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
|
# File 'lib/inat/data/query.rb', line 1020
def in? query_string
query_params = parse_query query_string
query_params.each do |key, value|
own_param = @api_params[key]
case own_param
when nil
return false
when Array
if key.start_with?('not_') || key.start_with?('without')
return false unless array_covers(own_param, value.split(','))
else
return false unless array_covers(value.split(','), own_param)
end
when Date
value = Date::parse value
if key.end_with?('1')
return false unless own_param <= value
elsif key.end_with?('2')
return false unless own_param >= value
end
else
return false unless own_param.to_query == value
end
end
return true
end
|
#match?(observation) ⇒ Boolean
1063
1064
1065
|
# File 'lib/inat/data/query.rb', line 1063
def match? observation
@r_match.all? { |m| m === observation }
end
|
#observations ⇒ Object
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
|
# File 'lib/inat/data/query.rb', line 1071
def observations
request = nil
current_time = nil
mode = G.config[:data][:update]
mode = UpdateMode[mode] if Symbol === mode || Integer === mode
mode = UpdateMode::parse(mode) if String === mode
if mode != UpdateMode::OFFLINE
actuals = []
if mode != UpdateMode::RELOAD
actual_time = 0
if mode != UpdateMode::MINIMAL
actual_time = Time::new - Period::parse(G.config[:data][:update_period])
end
actuals = Entity::Request::from_db_rows(DB.execute("SELECT * FROM requests WHERE time >= ?", actual_time.to_db)).select { |rq| self.in?(rq.query) }
end
if actuals.empty? || mode == UpdateMode::FORCE
request = Entity::Request::from_db_rows(DB.execute("SELECT * FROM requests WHERE query = ?", api_query)).first
updated_since = nil
if request == nil
query_string = api_query
project_id = @api_params[:project_id]
project_id = nil unless Integer === project_id
request = Entity::Request::create query_string, project_id
request.save
else
updated_since = request.time if mode != UpdateMode::RELOAD
end
if request.active
Status::status nil, "Query \##{ @int_key } : waiting for request R\##{ request.id }..."
while request.active do
sleep 1.0
end
else
request.active = true
params = @api_params.dup
params[:updated_since] = updated_since if updated_since && updated_since != Time::at(0)
tt = nil
cc = 0
current_time = Time::new
INat::API::query(:observations, **params) do |json, total|
tt ||= total
cc += 1
pc = cc * 100 / tt
td = (Time::new - current_time) / cc
te = (td * (tt - cc)).to_i
pe = Period::make seconds: te
pt = Period::make seconds: (Time::new - current_time).to_i
Status::status nil, "Query \##{ @int_key } : R\##{ request.id } : parsed #{ format("%d of %d : %3d%% : time %s remain %s", cc, tt, pc, pt.to_hs, pe.to_hs) }"
obs = Entity::Observation::parse json
obs.save
DB.execute "INSERT OR REPLACE INTO request_observations (request_id, observation_id) VALUES (?, ?);", request.id, obs.id
end
request.active = false
end
request = Entity::Request::read(request.id).first
end
end
sql, sql_args = db_where
result = Entity::Observation::from_db_rows(DB.execute("SELECT * FROM observations o#{ sql.empty? && '' || ' WHERE ' }#{ sql };", *sql_args))
if !@r_match.empty?
result = result.filter { |o| self.match?(o) }
end
if request != nil
request.update do
request.time = current_time if current_time
end
request.save
end
Status::status nil, "Query \##{ @int_key } : DONE"
result
end
|
#parse_acc_above(value) ⇒ Object
51
52
53
54
55
56
57
58
59
|
# File 'lib/inat/data/query.rb', line 51
private def parse_acc_above value
case value
when Integer
@api_params[:acc_above] = value
@db_where << [ "o.positional_accuracy > ?", [ value ] ]
else
raise TypeError, "Invalid 'acc_above' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_acc_below(value) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/inat/data/query.rb', line 61
private def parse_acc_below value
case value
when Integer
@api_params[:acc_below] = value
@db_where << [ "o.positional_accuracy < ?", [ value ] ]
else
raise TypeError, "Invalid 'acc_below' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_acc_below_or_unknown(value) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/inat/data/query.rb', line 71
private def parse_acc_below_or_unknown value
case value
when Integer
@api_params[:acc_below_or_unknown] = value
@db_where << [ "o.positional_accuracy < ? OR o.positional_accuracy IS NULL", [ value ] ]
else
raise TypeError, "Invalid 'acc_below_or_unknown' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_accuracy(value) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/inat/data/query.rb', line 26
private def parse_accuracy value
case value
when true, false
@api_params[:acc] = value
@db_where << [ "o.positional_accuracy IS#{ value && ' NOT' || '' } NULL", [] ]
when Integer
@api_params[:acc_above] = value - 1
@api_params[:acc_below] = value + 1
@db_where << [ "o.positional_accuracy = ?", [ value.to_i ] ]
when Range
min = value.begin
max = value.end
if min != nil
@api_params[:acc_above] = min.to_i - 1
@db_where << [ "o.positional_accuracy >= ?", [ min ] ]
end
if max != nil
@api_params[:acc_below] = max.to_i + 1
@db_where << [ "o.positional_accuracy <#{ value.exclude_end? && '' || '=' } ?", [ max ] ]
end
else
raise TypeError, "Invalid 'accuracy' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_captive(value) ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/inat/data/query.rb', line 81
private def parse_captive value
case value
when true, false
@api_params[:captive] = value
@db_where << [ "o.captive = ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'captive' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_created_d1(value) ⇒ Object
746
747
748
749
750
751
752
753
754
755
756
757
758
759
|
# File 'lib/inat/data/query.rb', line 746
private def parse_created_d1 value
case value
when Time
@api_params[:created_d1] = value
@db_where << [ "o.created_at >= ?", [ value.to_db ] ]
when Date
@api_params[:created_d1] = value
@db_where << [ "o.created_at >= ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'created_d1' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_created_d2(value) ⇒ Object
761
762
763
764
765
766
767
768
769
770
771
772
773
774
|
# File 'lib/inat/data/query.rb', line 761
private def parse_created_d2 value
case value
when Time
@api_params[:created_d2] = value
@db_where << [ "o.created_at <= ?", [ value.to_db ] ]
when Date
@api_params[:created_d2] = value
@db_where << [ "o.created_at <= ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'created_d2' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_created_on(value) ⇒ Object
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
|
# File 'lib/inat/data/query.rb', line 776
private def parse_created_on value
case value
when Date, Time
value = value.to_date
@api_params[:created_on] = value
@db_where << [ "o.created_at >= ? AND o.created_at < ?", value.to_db, value.to_db + 24 * 60 * 60 ]
when Range
min = value.begin
max = value.end
parse_created_d1 min if min
parse_created_d2 max if max
else
raise TypeError, "Invalid 'created_on' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_d1(value) ⇒ Object
708
709
710
711
712
713
714
715
716
717
|
# File 'lib/inat/data/query.rb', line 708
private def parse_d1 value
case value
when Date
@api_params[:d1] = value
@db_where << [ "o.observed_on >= ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'd1' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_d2(value) ⇒ Object
719
720
721
722
723
724
725
726
727
728
|
# File 'lib/inat/data/query.rb', line 719
private def parse_d2 value
case value
when Date
@api_params[:d2] = value
@db_where << [ "o.observed_on <= ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'd2' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_day(value) ⇒ Object
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
# File 'lib/inat/data/query.rb', line 657
private def parse_day value
case value
when Integer, String
value = value.to_i
@api_params[:day] = value
@db_where << [ "o.day = ?", [ value ] ]
when Array
value = value.map(&:to_i)
@api_params[:day] = value
@db_where << [ "o.day IN (#{(['?'] * value.size).join(',')})", value ]
else
raise TypeError, "Invalid 'day' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_endemic(value) ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'lib/inat/data/query.rb', line 91
private def parse_endemic value
case value
when true, false
@api_params[:endemic] = value
@db_where << [ "o.endemic = ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'endemic' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_geoprivacy(value) ⇒ Object
793
794
795
796
797
798
799
800
801
802
803
804
805
806
|
# File 'lib/inat/data/query.rb', line 793
private def parse_geoprivacy value
case value
when GeoPrivacy
@api_params[:geoprivacy] = value
@db_where << [ "o.geoprivacy = ?", [ value.to_db ] ]
when Array
@api_params[:geoprivacy] = value
@db_where << [ "o.geoprivacy IN (#{ (['?'] * value.size).join(',') })", value.map(&:to_db) ]
else
raise TypeError, "Invalid 'geoprivacy' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_hrank(value) ⇒ Object
516
517
518
|
# File 'lib/inat/data/query.rb', line 516
private def parse_hrank value
parse_rank(value..)
end
|
#parse_id(value) ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/inat/data/query.rb', line 248
private def parse_id value
case value
when Integer
@api_params[:id] = value
@db_where << [ "o.id == ?", [ value.to_db ] ]
when Array
raise TypeError, "Invalid 'id' type: #{ value.inspect }!", caller[1..] if value.any? { |v| !(Integer === v) }
@api_params[:id] = value
@db_where << [ "o.id IN (#{ (['?'] * value.size).join(',') })", value ]
else
raise TypeError, "Invalid 'id' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_ident_user(value) ⇒ Object
648
649
650
651
652
653
654
655
|
# File 'lib/inat/data/query.rb', line 648
private def parse_ident_user value
case value
when User
parse_ident_user_id value.id
else
raise TypeError, "Invalid 'ident_user' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_ident_user_id(value) ⇒ Object
637
638
639
640
641
642
643
644
645
646
|
# File 'lib/inat/data/query.rb', line 637
private def parse_ident_user_id value
case value
when Integer
@api_params[:ident_user_id] = value
@db_where << [ "EXISTS (SELECT * FROM identifications WHERE observation_id = o.id AND user_id = ?)", [ value ] ]
else
raise TypeError, "Invalid 'ident_user_id' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_introduced(value) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/inat/data/query.rb', line 101
private def parse_introduced value
case value
when true, false
@api_params[:introduced] = value
@db_where << [ "o.introduced = ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'introduced' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_license(value) ⇒ Object
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/inat/data/query.rb', line 280
private def parse_license value
if Array === value
value = value.map { |v| LicenseCode::parse(value) }
else
value = LicenseCode::parse(value)
end
case value
when LicenseCode
@api_params[:license] = value
@db_where << [ "o.license_code = ?", [ value.to_db ] ]
when Array
@api_params[:license] = value
@db_where << [ "o.license_code IN (#{ (['?'] * value.size).join(',') })", value.map(&:to_db) ]
else
raise TypeError, "Invalid 'license' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_licensed(value) ⇒ Object
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/inat/data/query.rb', line 226
private def parse_licensed value
case value
when true, false
@api_params[:licensed] = value
@db_where << [ "o.license_code IS#{ value && ' NOT' || '' } NULL", [] ]
else
raise TypeError, "Invalid 'licensed' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_location(value) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/inat/data/query.rb', line 131
private def parse_location value
case value
when true, false
@api_params[:geo] = value
@db_where << [ "o.location_latitude IS#{ value && ' NOT' || '' } NULL", [] ]
when Radius
@api_params[:lat] = value.latitude
@api_params[:lng] = value.longitude
@api_params[:radius] = value.radius
@db_where << [ "o.location_latitude IS NOT NULL AND o.location_longitude IS NOT NULL AND (6371 * acos(sin(o.location_latitude*pi()/180)*sin(?*pi()/180) + cos(o.location_latitude*pi()/180)*cos(?*pi()/180))*cos((o.location_longitude - ?)*pi()/180) <= ?)", [ value.latitude, value.latitude, value.longitude, value.radius ] ]
when Sector
@api_params[:nelat] = value.north
@api_params[:nelng] = value.east
@api_params[:swlat] = value.south
@api_params[:swlng] = value.west
@db_where << [ "o.location_latitude IS NOT NULL AND o.location_longitude IS NOT NULL AND o.location_latitude >= ? AND o.location_latitude <= ? AND o.location_longitude >= ? AND o.location_longitude <= ?",
[ value.south, value.north, value.west, value.east ] ]
else
raise TypeError, "Invalid 'location' type: #{ value.inspect }!"
end
end
|
#parse_lrank(value) ⇒ Object
520
521
522
|
# File 'lib/inat/data/query.rb', line 520
private def parse_lrank value
parse_rank(..value)
end
|
#parse_mappable(value) ⇒ Object
121
122
123
124
125
126
127
128
129
|
# File 'lib/inat/data/query.rb', line 121
private def parse_mappable value
case value
when true, false
@api_params[:mappable] = value
@db_where << [ "o.mappable = ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'mappable' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_month(value) ⇒ Object
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
# File 'lib/inat/data/query.rb', line 674
private def parse_month value
case value
when Integer, String
value = value.to_i
@api_params[:month] = value
@db_where << [ "o.month = ?", [ value ] ]
when Array
value = value.map(&:to_i)
@api_params[:month] = value
@db_where << [ "o.month IN (#{(['?'] * value.size).join(',')})", value ]
else
raise TypeError, "Invalid 'month' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_native(value) ⇒ Object
111
112
113
114
115
116
117
118
119
|
# File 'lib/inat/data/query.rb', line 111
private def parse_native value
case value
when true, false
@api_params[:native] = value
@db_where << [ "o.native = ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'native' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_not_id(value) ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/inat/data/query.rb', line 264
private def parse_not_id value
case value
when Integer
@api_params[:not_id] = value
@db_where << [ "o.id != ?", [ value.to_db ] ]
when Array
raise TypeError, "Invalid 'not_id' type: #{ value.inspect }!", caller[1..] if value.any? { |v| !(Integer === v) }
@api_params[:not_id] = value
@db_where << [ "o.id NOT IN (#{ (['?'] * value.size).join(',') })", value ]
else
raise TypeError, "Invalid 'not_id' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_observed_on(value) ⇒ Object
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
# File 'lib/inat/data/query.rb', line 730
private def parse_observed_on value
case value
when Date
@api_params[:observed_on] = value
@db_where << [ "o.observed_on = ?", [ value.to_db ] ]
when Range
min = value.begin
max = value.end
parse_d1 min if min
parse_d2 max if max
else
raise TypeError, "Invalid 'observed_on/date' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_photo_license(value) ⇒ Object
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
# File 'lib/inat/data/query.rb', line 300
private def parse_photo_license value
if Array === value
value = value.map { |v| LicenseCode::parse(value) }
else
value = LicenseCode::parse(value)
end
case value
when LicenseCode
@api_params[:photo_license] = value
debug "DB selector is not implemented..."
@r_match << lambda { |o| o.photos != nil && o.photos.any? { |p| p.license_code == value } }
when Array
@api_params[:photo_license] = value
debug "DB selector is not implemented..."
@r_match << lambda { |o| o.photos != nil && o.photos.any? { |p| value.include?(p.license_code) } }
else
raise TypeError, "Invalid 'photo_license' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_photo_licensed(value) ⇒ Object
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/inat/data/query.rb', line 237
private def parse_photo_licensed value
case value
when true, false
@api_params[:photo_licensed] = value
@db_where << [ "o.photo_licensed == ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'photo_licensed' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_photos(value) ⇒ Object
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/inat/data/query.rb', line 159
private def parse_photos value
case value
when true, false
@api_params[:photos] = value
@db_where << [ "#{ value && '' || 'NOT ' }EXISTS (SELECT * FROM observation_photos WHERE observation_id = o.id)", [] ]
else
raise TypeError, "Invalid 'photos' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_place(value) ⇒ Object
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
# File 'lib/inat/data/query.rb', line 389
private def parse_place value
id_value = case value
when Place
value.id
when Array
value.map do |v|
case v
when Place
v.id
else
raise TypeError, "Invalid 'place' type: #{ value.inspect }!", caller[1..]
end
end
else
raise TypeError, "Invalid 'place' type: #{ value.inspect }!", caller[1..]
end
parse_place_id id_value
end
|
#parse_place_id(value) ⇒ Object
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# File 'lib/inat/data/query.rb', line 342
private def parse_place_id value
case value
when Integer
@api_params[:place_id] = value
@db_where << [ "EXISTS (SELECT * FROM observation_places WHERE observation_id = o.id AND place_id = ?)", [ value.to_db ] ]
when Array
@api_params[:place_id] = value
@db_where << [ "EXISTS (SELECT * FROM observation_places WHERE observation_id = o.id AND place_id IN (#{ (['?'] * value.size).join(',') }))", value ]
else
raise TypeError, "Invalid 'place_id' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_popular(value) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/inat/data/query.rb', line 181
private def parse_popular value
case value
when true, false
@api_params[:popular] = value
@db_where << [ "#{ value && '' || 'NOT ' }EXISTS (SELECT * FROM observation_faves WHERE observation_id = o.id)", [] ]
else
raise TypeError, "Invalid 'popular' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_project(value) ⇒ Object
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
# File 'lib/inat/data/query.rb', line 408
private def parse_project value
id_value = case value
when Project
value.id
when Array
value.map do |v|
case v
when Project
v.id
else
raise TypeError, "Invalid 'project' type: #{ value.inspect }!", caller[1..]
end
end
else
raise TypeError, "Invalid 'project' type: #{ value.inspect }!", caller[1..]
end
parse_project_id id_value
end
|
#parse_project_id(value) ⇒ Object
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
# File 'lib/inat/data/query.rb', line 359
private def parse_project_id value
case value
when Integer
@api_params[:project_id] = value
@db_where << [ "EXISTS(SELECT * FROM project_observations WHERE observation_id = o.id AND project_id = ?) OR EXISTS(SELECT * FROM observation_projects WHERE observation_id = o.id AND project_id = ?)", [ value, value ] ]
when Array
value = value.map do |v|
case v
when Integer, Symbol
v
when String
v.intern
else
raise TypeError, "Invalid 'project_id' type: #{ value.inspect }!", caller[1..]
end
end
@api_params[:project_id] = value
@db_where << [ "EXISTS(SELECT * FROM project_observations WHERE observation_id = o.id AND project_id IN (#{ (['?'] * value.size).join(',') })) OR EXISTS(SELECT * FROM observation_projects WHERE observation_id = o.id AND project_id IN (#{ (['?'] * value.size).join(',') }))", value + value ]
else
raise TypeError, "Invalid 'project_id' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_quality_grade(value) ⇒ Object
839
840
841
842
843
844
845
846
847
848
849
850
851
852
|
# File 'lib/inat/data/query.rb', line 839
private def parse_quality_grade value
case value
when QualityGrade
@api_params[:quality_grade] = value
@db_where << [ "o.quality_grade = ?", [ value.to_db ] ]
when Array
@api_params[:quality_grade] = value
@db_where << [ "o.quality_grade IN (#{ (['?'] * value.size).join(',') })", value.map(&:to_db) ]
else
raise TypeError, "Invalid 'quality_grade' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_query(query_string) ⇒ Object
1004
1005
1006
1007
1008
1009
1010
1011
1012
|
# File 'lib/inat/data/query.rb', line 1004
private def parse_query query_string
result = {}
params = query_string.split '&'
params.each do |param|
para = param.split '='
result[para[0].intern] = URI.decode_uri_component(para[1])
end
result
end
|
#parse_rank(value) ⇒ Object
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
# File 'lib/inat/data/query.rb', line 427
private def parse_rank value
rank = case value
when Rank
value
when Symbol
Rank[value]
when String
Rank::parse(value)
when Integer
Rank.select { |r| r.data == value }
when Array
value.map do |v|
case v
when Rank
v
when Symbol
Rank[v]
when String
Rank::parse(v)
when Integer
Rank.select { |r| r.data == value }
else
raise TypeError, "Invalid 'rank' type: #{ value.inspect }!", caller[1..]
end
end.flatten
when Range
min = value.begin
max = value.end
r_min = case min
when nil
nil
when Rank
min
when Symbol
Rank[min]
when String
Rank::parse(min)
when Integer
Rank.find { |r| r.data <= min }
else
raise TypeError, "Invalid 'rank' type: #{ value.inspect }!", caller[1..]
end
r_max = case max
when nil
nil
when Rank
max
when Symbol
Rank[max]
when String
Rank::parse(max)
when Integer
Rank.find { |r| r.data <= max }
else
raise TypeError, "Invalid 'rank' type: #{ value.inspect }!", caller[1..]
end
Range::new r_min, r_max, value.exclude_end?
else
raise TypeError, "Invalid 'rank' type: #{ value.inspect }!", caller[1..]
end
case rank
when Rank
@api_params[:rank] = rank
@db_where << [ "o.rank_name = ?", [ rank.name ] ]
when Array
@api_params[:rank] = rank
@db_where << [ "o.rank_name IN (#{(['?'] * rank.size).join(',')})", rank.map(&:name) ]
when Range
min = rank.begin
max = rank.end
if min
@api_params[:hrank] = min
@db_where << [ "o.rank_data <= ?", [ min.data ] ]
end
if max
@api_params[:lrank] = max
@db_where << [ "o.rank_data >= ?", [ max.data ] ]
end
else
raise TypeError, "Invalid rank value: #{ rank.inspect }!"
end
end
|
#parse_sound_license(value) ⇒ Object
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/inat/data/query.rb', line 321
private def parse_sound_license value
if Array === value
value = value.map { |v| LicenseCode::parse(value) }
else
value = LicenseCode::parse(value)
end
case value
when LicenseCode
@api_params[:sound_license] = value
debug "DB selector is not implemented..."
@r_match << lambda { |o| o.sounds != nil && o.sounds.any? { |s| s.license_code == value } }
when Array
@api_params[:sound_license] = value
debug "DB selector is not implemented..."
@r_match << lambda { |o| o.sounds != nil && o.sounds.any? { |s| value.include?(s.license_code) } }
else
raise TypeError, "Invalid 'sound_license' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_sounds(value) ⇒ Object
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/inat/data/query.rb', line 170
private def parse_sounds value
case value
when true, false
@api_params[:sounds] = value
@db_where << [ "#{ value && '' || 'NOT ' }EXISTS (SELECT * FROM observation_sounds WHERE observation_id = o.id)", [] ]
else
raise TypeError, "Invalid 'sounds' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_taxon(value) ⇒ Object
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
# File 'lib/inat/data/query.rb', line 539
private def parse_taxon value
case value
when Taxon
parse_taxon_id value.id
when Array
ids = value.map do |v|
case v
when Taxon
v.id
else
raise TypeError, "Invalid 'taxon' type: #{ value.inspect }!", caller[1..]
end
end
parse_taxon_id ids
else
raise TypeError, "Invalid 'taxon' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_taxon_geoprivacy(value) ⇒ Object
808
809
810
811
812
813
814
815
816
817
818
819
820
821
|
# File 'lib/inat/data/query.rb', line 808
private def parse_taxon_geoprivacy value
case value
when GeoPrivacy
@api_params[:taxon_geoprivacy] = value
@db_where << [ "o.taxon_geoprivacy = ?", [ value.to_db ] ]
when Array
@api_params[:taxon_geoprivacy] = value
@db_where << [ "o.taxon_geoprivacy IN (#{ (['?'] * value.size).join(',') })", value.map(&:to_db) ]
else
raise TypeError, "Invalid 'taxon_geoprivacy' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_taxon_id(value) ⇒ Object
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
# File 'lib/inat/data/query.rb', line 524
private def parse_taxon_id value
case value
when Integer
@api_params[:taxon_id] = value
@db_where << [ "EXISTS (SELECT * FROM observation_ident_taxa WHERE observation_id = o.id AND taxon_id = ?)", [ value ] ]
when Array
@api_params[:taxon_id] = value
@db_where << [ "EXISTS (SELECT * FROM observation_ident_taxa WHERE observation_id = o.id AND taxon_id IN (#{(['?'] * value.size).join(',')}))", value ]
else
raise TypeError, "Invalid 'taxon_id' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_taxon_is_active(value) ⇒ Object
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/inat/data/query.rb', line 193
private def parse_taxon_is_active value
case value
when true, false
@api_params[:taxon_is_active] = value
@db_where << [ "o.taxon_is_active = ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'taxon_is_active' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_taxon_name(value) ⇒ Object
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
# File 'lib/inat/data/query.rb', line 592
private def parse_taxon_name value
case value
when String
@api_params[:taxon_name] = value
debug "DB selector for 'taxon_name' is not implemented..."
small = value.downcase
@r_match << lambda { |o| o.taxon.name.downcase == small || o.taxon.preferred_common_name&.downcase == small || o.taxon.english_common_name&.downcase == small }
when Array
@api_params[:taxon_name] = value
debug "DB selector for 'taxon_name' is not implemented..."
small = value.map(&:downcase)
@r_match << lambda { |o| small.any? { |n| o.taxon.name.downcase == n || o.taxon.preferred_common_name&.downcase == n || o.taxon.english_common_name&.downcase == n } }
else
raise TypeError, "Invalid 'taxon_name' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_threatened(value) ⇒ Object
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/inat/data/query.rb', line 204
private def parse_threatened value
case value
when true, false
@api_params[:threatened] = value
@db_where << [ "o.threatened = ?", [ value.to_db ] ]
else
raise TypeError, "Invalid 'threatened' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_user(value) ⇒ Object
628
629
630
631
632
633
634
635
|
# File 'lib/inat/data/query.rb', line 628
private def parse_user value
case value
when User
parse_user_login value.id
else
raise TypeError, "Invalid 'user' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_user_login(value) ⇒ Object
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
# File 'lib/inat/data/query.rb', line 609
private def parse_user_login value
case value
when Integer
@api_params[:user_id] = value
@db_where << [ "o.user_id == ?", [ value ] ]
when String, Symbol
@api_params[:user_login] = value
@db_where << [ "EXISTS (SELECT * FROM users WHERE id = o.user_id AND login = ?)", [ value.to_db ] ]
when Array
@api_params[:user_id] = value
debug "DB selector for 'user_id/user_login' arrays is not implemented..."
@r_match << lambda { |o| value.any? { |v| o.user.id == v || o.user.login == v.to_s } }
else
raise TypeError, "Invalid 'user_id/user_login' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_verifiable(value) ⇒ Object
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/inat/data/query.rb', line 215
private def parse_verifiable value
case value
when true, false
@api_params[:verifiable] = value
@db_where << [ "o.quality_grade#{ value && '' || ' NOT' } IN ('research', 'needs_id')", [] ]
else
raise TypeError, "Invalid 'verifiable' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_without_taxon(value) ⇒ Object
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
# File 'lib/inat/data/query.rb', line 573
private def parse_without_taxon value
case value
when Taxon
parse_taxon_id value.id
when Array
ids = value.map do |v|
case v
when Taxon
v.id
else
raise TypeError, "Invalid 'without_taxon' type: #{ value.inspect }!", caller[1..]
end
end
parse_without_taxon_id ids
else
raise TypeError, "Invalid 'without_taxon' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_without_taxon_id(value) ⇒ Object
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
# File 'lib/inat/data/query.rb', line 558
private def parse_without_taxon_id value
case value
when Integer
@api_params[:taxon_id] = value
@db_where << [ "NOT EXISTS (SELECT * FROM observation_ident_taxa WHERE observation_id = o.id AND taxon_id = ?)", [ value ] ]
when Array
@api_params[:taxon_id] = value
@db_where << [ "NOT EXISTS (SELECT * FROM observation_ident_taxa WHERE observation_id = o.id AND taxon_id IN (#{(['?'] * value.size).join(',')}))", value ]
else
raise TypeError, "Invalid 'taxon_id' type: #{ value.inspect }!", caller[1..]
end
end
|
#parse_year(value) ⇒ Object
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
# File 'lib/inat/data/query.rb', line 691
private def parse_year value
case value
when Integer, String
value = value.to_i
@api_params[:year] = value
@db_where << [ "o.year = ?", [ value ] ]
when Array
value = value.map(&:to_i)
@api_params[:year] = value
@db_where << [ "o.year IN (#{(['?'] * value.size).join(',')})", value ]
else
raise TypeError, "Invalid 'year' type: #{ value.inspect }!", caller[1..]
end
end
|