Module: Attache::API::Model

Defined in:
lib/attache/api/model.rb

Instance Method Summary collapse

Instance Method Details

#attache_field_attributes(attr_value, geometry) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/attache/api/model.rb', line 12

def attache_field_attributes(attr_value, geometry)
  Utils.array(attr_value).inject([]) do |sum, obj|
    sum + Utils.array(obj && obj.tap {|attrs|
      attrs['url'] = V1.attache_url_for(attrs['path'], geometry)
      attrs.delete 'signature'
      attrs.delete 'multiple' # legacy extraneous attribute that affects signature

      # add signature
      Attache::API::V1.attache_signature_for(attrs) do |generated_signature|
        attrs['signature'] = generated_signature
      end
    })
  end
end

#attache_field_options(attr_value, geometry, options = {}) ⇒ Object



4
5
6
# File 'lib/attache/api/model.rb', line 4

def attache_field_options(attr_value, geometry, options = {})
  V1.attache_options(geometry, attache_field_attributes(attr_value, geometry), options)
end

#attache_field_set(array) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/attache/api/model.rb', line 27

def attache_field_set(array)
  new_value = Utils.array(array).inject([]) {|sum,value|
    hash = value.respond_to?(:read) && V1.attache_upload(value) || value
    hash = JSON.parse(hash.to_s) rescue Hash(error: $!) unless hash.kind_of?(Hash)
    okay = hash.respond_to?(:[]) && (hash['path'] || hash[:path])

    # check signature
    Attache::API::V1.attache_signature_for(hash) do |generated_signature|
      if generated_signature == hash['signature']
        hash.delete 'signature'
      else
        okay = nil
      end
    end
    okay ? sum + [hash] : sum
  }
  Utils.array(new_value)
end

#attache_field_urls(attr_value, geometry) ⇒ Object



8
9
10
# File 'lib/attache/api/model.rb', line 8

def attache_field_urls(attr_value, geometry)
  attache_field_attributes(attr_value, geometry).collect {|attrs| attrs['url']}
end

#attache_update_pending_diffs(old_value, new_value, pending_backup, pending_discard) ⇒ Object



46
47
48
49
50
51
# File 'lib/attache/api/model.rb', line 46

def attache_update_pending_diffs(old_value, new_value, pending_backup, pending_discard)
  old_paths = Utils.array(old_value).collect { |x| x['path'] }.reject { |path| path.nil? || path == "" }
  new_paths = Utils.array(new_value).collect { |x| x['path'] }.reject { |path| path.nil? || path == "" }
  pending_backup.push(*(new_paths - old_paths))
  pending_discard.push(*(old_paths - new_paths))
end

#attaches_backup!(files) ⇒ Object



60
61
62
63
# File 'lib/attache/api/model.rb', line 60

def attaches_backup!(files)
  files.reject! {|x| x.nil? || x == "" }
  V1.attache_backup(*files.uniq) unless files.empty?
end

#attaches_discard!(files) ⇒ Object



53
54
55
56
57
58
# File 'lib/attache/api/model.rb', line 53

def attaches_discard!(files)
  files.reject! {|x| x.nil? || x == "" }
  V1.attache_delete(*files.uniq) unless files.empty?
rescue Exception
  raise if ENV['ATTACHE_DISCARD_FAILURE_RAISE_ERROR']
end