Class: Insup::Uploader::InsalesUploader

Inherits:
Insup::Uploader show all
Defined in:
lib/insup/uploader/insales_uploader.rb

Constant Summary

Constants inherited from Insup::Uploader

BATCH_UPLOADED_FILES, BATCH_UPLOADING_FILES, CREATED_FILE, CREATING_FILE, DELETED_FILE, DELETING_FILE, ERROR, MODIFIED_FILE, MODIFYING_FILE

Instance Method Summary collapse

Methods inherited from Insup::Uploader

#batch_upload, #delete_file, find_uploader, #process_file, uploader

Constructor Details

#initialize(settings) ⇒ InsalesUploader

Returns a new instance of InsalesUploader.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/insup/uploader/insales_uploader.rb', line 7

def initialize(settings)
  super
  @insales = Insup::Insales.new(settings)
  @insales.configure_api

  if !theme
    fail Insup::Exceptions::FatalUploaderError, "Theme #{theme_id} is not found in the Insales shop"
  end

  assets_list(true)
end

Instance Method Details

#remove_file(file) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/insup/uploader/insales_uploader.rb', line 109

def remove_file(file)
  asset = find_asset(file)
  
  if !asset
    msg = "Cannot find remote counterpart for file #{file.path}"
    changed
    notify_observers(ERROR, file, msg)
    raise Insup::Exceptions::RecoverableUploaderError, "Cannot find remote counterpart for file #{file.path}"
  end

  changed
  notify_observers(DELETING_FILE, file)

  begin
    if asset.destroy
      assets_list.delete(asset)
      changed
      notify_observers(DELETED_FILE, file)
    end
  rescue ActiveResource::ServerError => ex
    changed
    notify_observers(ERROR, file, ex.message)
    raise Insup::Exceptions::RecoverableUploaderError, "Server error occured when deleting file #{file.path}"
  end
end

#upload_file(file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/insup/uploader/insales_uploader.rb', line 19

def upload_file(file)
  case file.state
  when Insup::TrackedFile::NEW
    upload_new_file(file)
  when Insup::TrackedFile::MODIFIED
    upload_modified_file(file)
  when Insup::TrackedFile::UNSURE
    upload_modified_file(file)
  end
end

#upload_modified_file(file) ⇒ Object



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
# File 'lib/insup/uploader/insales_uploader.rb', line 75

def upload_modified_file(file)
  asset = find_asset(file)

  if !asset
    upload_new_file(file)
    return
  end

  changed
  notify_observers(MODIFYING_FILE, file)
  file_contents = File.read(file.path)

  if asset.content_type.start_with? 'text/'
    begin
      res = asset.update_attribute(:content, file_contents)

      if !res
        process_error(asset, file)
        return
      end

      changed
      notify_observers(MODIFIED_FILE, file)
    rescue ActiveResource::ServerError => ex
      changed
      notify_observers(ERROR, file, ex.message)
      raise Insup::Exceptions::RecoverableUploaderError, "Server error occured when updating file #{file.path}"
    end
  else
    remove_file(file)
    upload_new_file(file)
  end
end

#upload_new_file(file) ⇒ Object



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
# File 'lib/insup/uploader/insales_uploader.rb', line 30

def upload_new_file(file)
  asset = find_asset(file)

  if !asset.nil?
    upload_modified_file(file)
    return
  end

  changed
  notify_observers(CREATING_FILE, file)
  asset_type = ::Insup::Insales::Asset.get_type(file.path)

  if !asset_type
    msg = "Cannot determine asset type for file #{file.path}"
    changed
    notify_observers(ERROR, file, msg)
    raise Insup::Exceptions::RecoverableUploaderError, msg
  end

  file_contents = File.read(file.path)

  hash = {
    name: file.file_name,
    theme_id: @config.uploader['theme_id'],
    type: asset_type
  }

  if ['Asset::Snippet', 'Asset::Template'].include?(asset_type)
    hash[:content] = file_contents
  else
    hash[:attachment] = Base64.encode64(file_contents)
  end

  begin
    asset = ::Insup::Insales::Asset.create(hash)
    assets_list << asset
    changed
    notify_observers(CREATED_FILE, file)
  rescue ActiveResource::ServerError => ex
    changed
    notify_observers(ERROR, file, ex.message)
    raise Insup::Exceptions::RecoverableUploaderError, "Server error occured when creating file #{file.path}"
  end
end