Sha256: 3017002acc59e20387661a96c43e4aeb02b1a0128e070d842ccfbd941c2a74af

Contents?: true

Size: 1.89 KB

Versions: 12

Compression:

Stored size: 1.89 KB

Contents

require 'uri'
require 'net/http'

module Apidae
  class Export < ActiveRecord::Base

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

    validates_presence_of :file_url, :project_id

    before_save :normalize_url

    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

    # Note : handle reset case
    def self.pending
      where(remote_status: 'SUCCESS', status: PENDING).order(:id)
    end

    def import_data
      success = true
      begin
        open(file_url) do |f|
          begin
            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
            end
            update(status: Export::COMPLETE)
            if Rails.application.config.respond_to?(:apidae_import_callback)
              Rails.application.config.apidae_import_callback.call(self)
            end
          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
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
apidae-1.3.11 app/models/apidae/export.rb
apidae-1.3.10 app/models/apidae/export.rb
apidae-1.3.9 app/models/apidae/export.rb
apidae-1.3.8 app/models/apidae/export.rb
apidae-1.3.7 app/models/apidae/export.rb
apidae-1.3.6 app/models/apidae/export.rb
apidae-1.3.5 app/models/apidae/export.rb
apidae-1.3.4 app/models/apidae/export.rb
apidae-1.3.3 app/models/apidae/export.rb
apidae-1.3.2 app/models/apidae/export.rb
apidae-1.3.1 app/models/apidae/export.rb
apidae-1.3.0 app/models/apidae/export.rb