aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/import.rb
blob: 93388a6f54fe6780f6eeb060f64c008ac2518d39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class Import
  extend Enumerize
  extend ActiveModel::Naming
  include ActiveModel::Model  
  
  # enumerize :status, in: %w{created scheduled terminated canceled aborted}, default: "created", predicates: true
  # enumerize :format, in: %w{neptune netex gtfs}, default: "neptune", predicates: true

  attr_reader :datas, :links, :headers, :errors

  def initialize( response  )
    @datas = response[:datas]
    @headers = response[:headers]
    @links = response[:links]
    # @status = @datas.status.downcase if @datas.status?
    # @format = @datas.type.downcase if @datas.type?
  end  
  
  def report
    report_path = links[:report]
    if report_path      
      response = IevApi.request(:get, compliance_check_path, params)
      ImportReport.new(response)
    else
      raise IevApi::IevError("Impossible to access report path link for import")
    end
  end 

  def compliance_check
    compliance_check_path = links[:validation]
    if compliance_check_path
      response = IevApi.request(:get, compliance_check_path, params)
      ComplianceCheck.new(response)
    else
      raise IevApi::IevError("Impossible to access compliance check path link for import")
    end
  end

  def delete
    delete_path =  links[:delete]
    if delete_path
      IevApi.request(:delete, delete_path, params)
    else
      raise IevApi::IevError("Impossible to access delete path link for import")
    end
  end

  def cancel
    cancel_path =  links[:cancel]
    if cancel_path
      IevApi.request(:delete, cancel_path, params)
    else
      raise IevApi::IevError("Impossible to access cancel path link for import")
    end
  end

  def id
    @datas.id
  end

  def status
    datas.status
  end

  def format
    datas.format
  end

  def filename
    @datas.filename
  end

  def filename_extension
    File.extname(filename) if filename
  end

  def percentage_progress
    if %w{created}.include? status
      0
    elsif %w{ terminated canceled aborted }.include? status
      100
    else
      20
    end
  end
  
  def referential_name
    @datas.referential
  end
  
  def name
    @datas.parameters.name
  end

  def user_name?
    @datas.parameters? && @datas.parameters.user_name?
  end
  
  def user_name
    @datas.parameters.user_name if user_name?
  end

  def no_save
    @datas.parameters.no_save
  end

  def filename
    @datas.filename
  end

  def created_at?
    @datas.created?
  end
  
  def created_at
    Time.at(@datas.created.to_i / 1000) if created_at?
  end

  def updated_at?
    @datas.updated?
  end

  def updated_at
    Time.at(@datas.updated.to_i / 1000) if updated_at?
  end

end