Class: Insup::Uploader::InsalesUploader

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

Constant Summary collapse

InsalesUploaderError =
Class.new(Insup::Exceptions::UploaderError)

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.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/insup/uploader/insales_uploader.rb', line 10

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

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

  assets_list(true)
end

Instance Method Details

#remove_file(file) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/insup/uploader/insales_uploader.rb', line 96

def remove_file(file)
  asset = find_asset(file)

  if !asset
    raise InsalesUploaderError, "Cannot find remote counterpart for file #{file.path}"
  end

  changed
  notify_observers(DELETING_FILE, file)

  if pd=asset.destroy
    assets_list.delete(asset)
  end

  changed
  notify_observers(DELETED_FILE, file)
end

#upload_file(file) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/insup/uploader/insales_uploader.rb', line 22

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



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

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/'
    res = asset.update_attribute(:content, file_contents)
    if !res
      process_error(asset, file)
    end
  else
    remove_file(file)
    upload_new_file(file)
  end

  changed
  notify_observers(MODIFIED_FILE, file)
end

#upload_new_file(file) ⇒ Object



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

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 = get_asset_type(file.path)

  if !asset_type
    raise InsalesUploaderError, "Cannot determine asset type for file #{file.path}"
  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

  asset = ::Insup::Insales::Asset.new(hash)
  assets_list << asset
  changed
  notify_observers(CREATED_FILE, file)
end