Class: INat::API

Inherits:
Object
  • Object
show all
Includes:
App, INat::App::Logger::DSL
Defined in:
lib/inat/data/api.rb

Constant Summary collapse

RECORDS_LIMIT =
200
FREQUENCY_LIMIT =
1.0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from INat::App::Logger::DSL

#debug, debug, echo, #echo, error, #error, #info, info, log, #log, #warning, warning

Constructor Details

#initializeAPI

Returns a new instance of API.



20
21
22
# File 'lib/inat/data/api.rb', line 20

def initialize
  @mutex = Mutex::new
end

Class Method Details

.get(path, part, limit, *ids) ⇒ Array<Hash>

Get one or more objects by id

Returns:

  • (Array<Hash>)


231
232
233
# File 'lib/inat/data/api.rb', line 231

def get path, part, limit, *ids
  instance.get path, part, limit, *ids
end

.instanceAPI

Singleton instance

Returns:



223
224
225
226
# File 'lib/inat/data/api.rb', line 223

def instance
  @instance ||= new
  @instance
end

.load_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



241
242
243
# File 'lib/inat/data/api.rb', line 241

def load_file filename
  instance.load_file filename
end

.query(path, first_only: false, **params, &block) ⇒ Object



236
237
238
# File 'lib/inat/data/api.rb', line 236

def query path, first_only: false, **params, &block
  instance.query path, first_only: first_only, **params, &block
end

Instance Method Details

#get(path, part, limit, *ids) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/inat/data/api.rb', line 24

def get path, part, limit, *ids
  return [] if ids.empty?
  if ids.size > limit
    rest = ids.dup
    head = rest.shift limit
    return get(path, *head) + get(path, *rest)
  end
  result = []
  Status::status "[api]", "#{path} ..."
  @mutex.synchronize do
    now = Time::new
    if @last_call && now - @last_call < FREQUENCY_LIMIT
      sleep FREQUENCY_LIMIT - (now - @last_call)
    end
    case part
    when :query
      url = G.config[:api][:root] + path.to_s + "?id=#{ids.join(",")}"
      url += "&per_page=#{limit}"
      locale = G.config[:api][:locale]
      url += "&locale=#{locale}" if locale
      preferred_place_id = G.config[:api][:preferred_place_id]
      url += "&preferred_place_id=#{preferred_place_id}" if preferred_place_id
    when :path
      url = G.config[:api][:root] + path.to_s + "/#{ids.join(",")}"
    else
      raise ArgumentError, "Invalid 'part' argument: #{part.inspect}!", caller
    end
    uri = URI(url)
    info "GET: URI = #{uri.inspect}"
    # Status::status 'GET', uri.to_s
    https = uri.scheme == "https"
    open_timeout = G.config[:api][:open_timeout]
    read_timeout = G.config[:api][:read_timeout]
    http_options = {
      use_ssl: https,
    }
    http_options[:open_timeout] = open_timeout if open_timeout
    http_options[:read_timeout] = read_timeout if read_timeout
    answered = false
    answer_count = 50
    last_time = Time::new
    until answered
      begin
        Net::HTTP::start uri.host, uri.port, **http_options do |http|
          request = Net::HTTP::Get::new uri
          request["User-Agent"] = G.config[:api][:user_agent] || "INat::Get // Unknown Instance"
          response = http.request request
          if Net::HTTPSuccess === response
            data = JSON.parse(response.body)
            result = data["results"]
            total = data["total_results"]
            paged = data["per_page"]
            time_diff = Time::new - last_time
            debug "GET OK: total = #{total} paged = #{paged} time = #{time_diff} "
            # Status::status 'GET', uri.to_s + ' DONE'
          else
            error "Bad response om #{uri.path}#{uri.query && !uri.query.empty? && "?" + uri.query || ""}: #{response.inspect}!"
            result = [{ "id" => ids.first }]
            # Status::status 'GET', uri.to_s + ' ERROR'
          end
        end
        answered = true
      rescue Exception
        if answer_count > 0
          answer_count -= 1
          answered = false
          error "Error in HTTP request: #{$!.inspect}, retry: #{answer_count}."
          # Status::status "Error in HTTP request: #{ $!.inspect }, retry: #{ answer_count }."
          sleep 2.0
        else
          answered = true
          error "Error in HTTP request: #{$!.inspect}!"
          # Status::status "Error in HTTP request: #{ $!.inspect }!"
        end
      end
    end
    @last_call = Time::new
  end
  Status::status "[api]", "#{path} DONE"
  result
end

#load_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



211
212
213
214
# File 'lib/inat/data/api.rb', line 211

def load_file filename
  data = JSON.parse File.read(filename)
  data["results"]
end

#make_url(path, **params) ⇒ Object (private)



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/inat/data/api.rb', line 106

private def make_url path, **params
  url = G.config[:api][:root] + path.to_s
  query = []
  params.each do |key, value|
    query_param = "#{key}="
    if Array === value
      query_param += value.map(&:to_query).join(",")
    else
      query_param += value.to_query
    end
    query << query_param
  end
  locale = G.config[:api][:locale]
  query << "locale=#{locale}" if locale
  preferred_place_id = G.config[:api][:preferred_place_id]
  query << "preferred_place_id=#{preferred_place_id}" if preferred_place_id
  if !query.empty?
    url += "?" + query.join("&")
  end
  url
end

#query(path, first_only: false, **params, &block) ⇒ Object



128
129
130
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/inat/data/api.rb', line 128

def query path, first_only: false, **params, &block
  Status::status "[api]", "#{path} ..."
  para = params.dup
  para.delete_if { |key, _| key.intern == :page }
  para[:per_page] = RECORDS_LIMIT
  para[:order_by] = :id
  para[:order] = :asc
  result = []
  rest = nil
  total = 0
  @mutex.synchronize do
    now = Time::new
    if @last_call && now - @last_call < FREQUENCY_LIMIT
      sleep FREQUENCY_LIMIT - (now - @last_call)
    end
    url = make_url path, **para
    uri = URI(url)
    info "QUERY: URI = #{uri.inspect}"
    # Status::status 'QUERY', uri.to_s
    https = uri.scheme == "https"
    open_timeout = G.config[:api][:open_timeout]
    read_timeout = G.config[:api][:read_timeout]
    http_options = {
      use_ssl: https,
    }
    http_options[:open_timeout] = open_timeout if open_timeout
    http_options[:read_timeout] = read_timeout if read_timeout
    answered = false
    answer_count = 50
    last_time = Time::new
    until answered
      begin
        Net::HTTP::start uri.host, uri.port, **http_options do |http|
          request = Net::HTTP::Get::new uri
          request["User-Agent"] = G.config[:api][:user_agent] || "INat::Get // Unknown Instance"
          response = http.request request
          if Net::HTTPSuccess === response
            data = JSON.parse(response.body)
            result = data["results"]
            total = data["total_results"]
            paged = data["per_page"]
            time_diff = Time::new - last_time
            debug "QUERY OK: total = #{total} paged = #{paged} time = #{time_diff} "
            if total > paged && !first_only
              max = result.map { |o| o["id"] }.max
              rest = para
              rest[:id_above] = max
            end
          else
            raise RuntimeError, "Invalid response: #{response.inspect}"
          end
        end
        answered = true
      rescue Exception
        if answer_count > 0
          answer_count -= 1
          answered = false
          error "Error in HTTP request: #{$!.inspect}, retry: #{answer_count}."
          # Status::status "Error in HTTP request: #{ $!.inspect }, retry: #{ answer_count }."
          sleep 2.0
        else
          raise
        end
      end
    end
    @last_call = Time::new
  end
  Status::status "[api]", "#{path} DONE"
  # TODO: переделать рекурсию в итерации
  if block_given?
    rr = []
    result.each do |js_object|
      rr << yield(js_object, total)
    end
    rr += query(path, **rest, &block) if rest
    rr
  else
    result += query(path, **rest) if rest
    result
  end
end