Skip to content

Commit

Permalink
Make @inat_import an ivar
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Aug 14, 2024
1 parent f392f20 commit 8a233d3
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions app/controllers/observations/inat_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def create
return designation_required unless imports_designated?
return consent_required if params[:consent] == "0"

inat_import = InatImport.find_or_create_by(user: User.current)
inat_import.update(state: "Authorizing",
import_all: params[:all],
inat_ids: params[:inat_ids],
inat_username: params[:inat_username])
@inat_import = InatImport.find_or_create_by(user: User.current)
@inat_import.update(state: "Authorizing",
import_all: params[:all],
inat_ids: params[:inat_ids],
inat_username: params[:inat_username])

request_inat_user_authorization
end
Expand Down Expand Up @@ -107,8 +107,8 @@ def authenticate
auth_code = params[:code]
return not_authorized if auth_code.blank?

inat_import = InatImport.find_or_create_by(user: User.current)
inat_import.update(state: "Authenticating")
@inat_import = InatImport.find_or_create_by(user: User.current)
@inat_import.update(state: "Authenticating")

# exchange code received from iNat for an oAuth `access_token`
payload = {
Expand All @@ -131,11 +131,11 @@ def authenticate

# Now that we've got the right token, we can make authenticated requests
# to iNat that get the real real private data.
inat_import.update(token: api_token, state: "Importing")
@inat_import.update(token: api_token, state: "Importing")

import_requested_observations(inat_import)
import_requested_observations

inat_import.update(state: "Done")
@inat_import.update(state: "Done")
redirect_to(observations_path)
end

Expand All @@ -148,16 +148,16 @@ def not_authorized
redirect_to(observations_path)
end

def import_requested_observations(inat_import)
inat_ids = inat_id_list(inat_import)
return if inat_import[:import_all].blank? && inat_ids.blank?
def import_requested_observations
inat_ids = inat_id_list
return if @inat_import[:import_all].blank? && inat_ids.blank?

last_import_id = 0
loop do
page =
inat_search_observations(
ids: inat_ids, id_above: last_import_id,
user_login: inat_import.inat_username
user_login: @inat_import.inat_username
)
break if page_empty?(page)

Expand All @@ -179,8 +179,8 @@ def done_with_page?(parsed_page)
parsed_page["page"] * parsed_page["per_page"]
end

def inat_id_list(inat_import)
inat_import.inat_ids.delete(" ")
def inat_id_list
@inat_import.inat_ids.delete(" ")
end

# https://api.inaturalist.org/v1/docs/#!/Observations/get_observations
Expand All @@ -198,9 +198,18 @@ def inat_search_observations(**args)
}.merge(args)

query = URI.encode_www_form(query_args)
::Inat.new(operation: query, token: inat_import.token).body
# ::Inat.new(operation: query, token: @inat_import.token).body

# Nimmo 2024-06-19 jdc. Moving the request from the inat class to here.
# RestClient::Request.execute wasn't available in the class
headers = { authorization: "Bearer #{@inat_import.token}", accept: :json }
@inat = RestClient::Request.execute(
method: :get, url: "#{API_BASE}/observations?#{query}", headers: headers
)
end

API_BASE = "https://api.inaturalist.org/v1"

def import_page(page)
JSON.parse(page)["results"].each do |result|
import_one_result(JSON.generate(result))
Expand Down Expand Up @@ -240,9 +249,9 @@ def import_one_result(result)
# update_field_slip(@observation, params[:field_code])
end

def inat_import
InatImport.find_by(user: User.current)
end
# def find_inat_import
# InatImport.find_by(user: User.current)
# end

def not_importable(inat_obs)
return if inat_obs.taxon_importable?
Expand Down

0 comments on commit 8a233d3

Please sign in to comment.