Class: Apidae::Export

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/apidae/export.rb

Constant Summary collapse

PENDING =
'pending'
IN_PROGRESS =
'in_progress'
COMPLETE =
'complete'
CANCELLED =
'cancelled'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.in_progressObject



28
29
30
# File 'app/models/apidae/export.rb', line 28

def self.in_progress
  where(remote_status: 'SUCCESS', status: IN_PROGRESS)
end

.pendingObject

Note : handle reset case



24
25
26
# File 'app/models/apidae/export.rb', line 24

def self.pending
  where(remote_status: 'SUCCESS', status: PENDING).order(:id)
end

Instance Method Details

#import_dataObject



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
# File 'app/models/apidae/export.rb', line 32

def import_data
  success = true
  begin
    open(file_url) do |f|
      begin
        logger.info "Starting file import for export #{id} - project #{project_id}"
        FileImport.import(f, project_id)
        unless confirm_url.blank?
          uri = URI(confirm_url)
          req = Net::HTTP::Post.new(uri)
          Net::HTTP.start(uri.hostname, uri.port) do |http|
            http.request(req)
          end
          logger.info "Posted file import callback for export #{id} - project #{project_id}"
        end
        update(status: Export::COMPLETE)
        if Rails.application.config.respond_to?(:apidae_import_callback)
          Rails.application.config.apidae_import_callback.call(self)
        end
        logger.info "Completed file import for export #{id} - project #{project_id}"
      rescue Exception => ex
        logger.error("Failed to import export file : #{file_url}")
        logger.error("Error is : #{ex} \n#{ex.backtrace.join("\n") unless ex.backtrace.blank?}")
        success = false
        update(status: Export::CANCELLED)
      end
    end
  rescue OpenURI::HTTPError => err
    logger.error("Failed to download export file : #{file_url}")
    logger.error("Error is : #{err}")
    success = false
    update(status: Export::CANCELLED)
  rescue Exception => e
    logger.error "Failed to import file : #{e.file_url}"
    logger.error("Error is : #{err}")
    success = false
    e.update(status: Export::CANCELLED)
  end
  success
end

#normalize_urlObject



16
17
18
19
20
21
# File 'app/models/apidae/export.rb', line 16

def normalize_url
  self.file_url = file_url.strip
  unless file_url.include?('/')
    self.file_url = "http://export.apidae-tourisme.com/exports/#{file_url}"
  end
end