Class: Adyen::Admin::Skin

Inherits:
Object
  • Object
show all
Defined in:
lib/adyen-admin/skin.rb

Constant Summary collapse

UPLOAD =
"https://ca-test.adyen.com/ca/ca/skin/uploadskin.shtml?skinCode=%s"
DOWNLOAD =
"https://ca-test.adyen.com/ca/ca/skin/downloadskinsubmit.shtml?downloadSkin=Download&skinCode=%s"
TEST =
"https://ca-test.adyen.com/ca/ca/skin/testpayment.shtml?skinCode=%s"
VERSION_TEST =
"https://test.adyen.com/hpp/version.shtml?skinCode=%s"
VERSION_LIVE =
"https://live.adyen.com/hpp/version.shtml?skinCode=%s"
PUBLISH =
"https://ca-test.adyen.com/ca/ca/skin/publishskin.shtml?skinCode=%s"
SKINS =
"https://ca-test.adyen.com/ca/ca/skin/skins.shtml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Skin

Returns a new instance of Skin.

Raises:

  • (ArgumentError)

19
20
21
22
23
24
25
26
27
28
29
# File 'lib/adyen-admin/skin.rb', line 19

def initialize(attributes = {})
  attributes.each do |key, value|
    send("#{key}=", value)
  end

  self.path ||= File.expand_path File.join(Skin.default_path, [name,code].compact.join("-"))
  self.path ||= File.expand_path File.join(Skin.default_path, name.to_s)
  self.path ||= File.expand_path File.join(Skin.default_path, code.to_s)

  raise ArgumentError, "No Code given" unless code
end

Instance Attribute Details

#codeObject

Returns the value of attribute code


17
18
19
# File 'lib/adyen-admin/skin.rb', line 17

def code
  @code
end

#nameObject

Returns the value of attribute name


17
18
19
# File 'lib/adyen-admin/skin.rb', line 17

def name
  @name
end

#pathObject

Returns the value of attribute path


17
18
19
# File 'lib/adyen-admin/skin.rb', line 17

def path
  @path
end

Class Method Details

.allObject

union remote and local skins. Local skins are frozen to indicate no availble remote counter part which avoid update


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/adyen-admin/skin.rb', line 41

def self.all
  @all ||= {}.tap do |hash|
    if Adyen::Admin.authenticated?
      self.all_remote.each do |skin|
        hash[skin.code] = skin unless hash[skin.code]
      end
    end
    self.all_local.each do |skin|
      hash[skin.code] = skin unless hash[skin.code]
    end
  end.values
end

.all_localObject

fetch all local skins


66
67
68
69
70
# File 'lib/adyen-admin/skin.rb', line 66

def self.all_local
  Dir[File.join(default_path.to_s, "*")].map do |path|
    Skin.new(:path => path).freeze rescue nil
  end.compact
end

.all_remoteObject

fetch all remote skins


55
56
57
58
59
60
61
62
63
# File 'lib/adyen-admin/skin.rb', line 55

def self.all_remote
  page = Adyen::Admin.get(SKINS)
  page.search(".data tbody tr").map do |node|
    Skin.new({
      :code => node.search("a")[0].content.strip,
      :name => node.search("td")[1].content.strip,
    })
  end
end

.default_pathObject


31
32
33
# File 'lib/adyen-admin/skin.rb', line 31

def self.default_path
  @default_path || "."
end

.default_path=(path) ⇒ Object


35
36
37
# File 'lib/adyen-admin/skin.rb', line 35

def self.default_path=(path)
  @default_path = path
end

.find(skin_code) ⇒ Object

find a skin within remote + local ones


73
74
75
76
77
# File 'lib/adyen-admin/skin.rb', line 73

def self.find(skin_code)
  self.all.select do |skin|
    skin.code == skin_code
  end.first
end

.purge_cacheObject


79
80
81
# File 'lib/adyen-admin/skin.rb', line 79

def self.purge_cache
  @all = nil
end

Instance Method Details

#==(skin) ⇒ Object


254
255
256
# File 'lib/adyen-admin/skin.rb', line 254

def ==(skin)
  self.code == skin.code
end

#compile(output, pattern = /<!-- ### inc\/([a-z]+) -->(.+?)<!-- ### -->/m) ⇒ Object

Raises:

  • (ArgumentError)

180
181
182
183
184
185
186
187
188
189
190
# File 'lib/adyen-admin/skin.rb', line 180

def compile(output, pattern = /<!-- ### inc\/([a-z]+) -->(.+?)<!-- ### -->/m)
  raise ArgumentError, "No Path given" unless path

  output.scan(pattern) do |name, content|
    file = File.join(path, "inc/#{name}.txt")
    `mkdir -p #{File.dirname(file)}`
    File.open(file, "w") do |f|
      f.write content.strip
    end
  end
end

#compress(exclude = /(yml|zip|erb)$/, outfile = "#{code}.zip") ⇒ Object

Raises:

  • (ArgumentError)

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/adyen-admin/skin.rb', line 192

def compress(exclude = /(yml|zip|erb)$/, outfile = "#{code}.zip")
  raise ArgumentError, "No Path given" unless path

  outfile.tap do |filename|
    `rm -f #{filename}`
    Zip::ZipFile.open(filename, Zip::ZipFile::CREATE) do |zip_file|
      Dir["#{path}/**/**"].each do |file|
        next if file =~ exclude
        raise if nested_subdirectory?(path, file)
        zip_file.add(file.sub(path, code), file)
      end

      dir = File.join(File.dirname(path), parent_skin)
      Dir["#{dir}/**/**"].each do |file|
        begin
          next if file =~ exclude
          raise if nested_subdirectory?(path, file)
          zip_file.add(file.sub(dir, code), file)
        rescue Zip::ZipEntryExistsError
          # NOOP
        end
      end
    end
  end
end

#decompile(filename, backup = true) ⇒ Object


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/adyen-admin/skin.rb', line 149

def decompile(filename, backup = true)
  # create backup of current, include any files
  if self.path
    if backup
      compress(/(zip|lock)$/, ".backup.zip")
    end
  else
    backup = false
    decompile_path = File.join(Skin.default_path, [name,code].compact.join("-"))
    `mkdir -p #{decompile_path}`
  end

  Zip::ZipFile.open(filename) do |zip_file|
    zip_file.each do |file|
      f_path = File.join(self.path || decompile_path, file.name.gsub("#{code}/", ""))
      FileUtils.mkdir_p(File.dirname(f_path))
      if File.directory?(f_path)
        `mkdir -p #{f_path}`
      else
        `rm -f #{f_path}`
        zip_file.extract(file, f_path)
      end
    end
  end
  self.path ||= decompile_path

  if backup
    `mv .backup.zip #{File.join(self.path, ".backup.zip")}`
  end
end

#downloadObject


143
144
145
146
147
# File 'lib/adyen-admin/skin.rb', line 143

def download
  "#{code}.zip".tap do |filename|
    Adyen::Admin.client.download(DOWNLOAD % code, filename)
  end
end

#parent_skinObject


121
122
123
# File 'lib/adyen-admin/skin.rb', line 121

def parent_skin
  skin_data[:parent_skin] || "base"
end

#publishObject


230
231
232
233
234
# File 'lib/adyen-admin/skin.rb', line 230

def publish
  page = Adyen::Admin.get(PUBLISH % code)
  page = Adyen::Admin.client.submit(page.form.tap do |form|
  end)
end

#skin_data(force_update = false) ⇒ Object


100
101
102
103
# File 'lib/adyen-admin/skin.rb', line 100

def skin_data(force_update = false)
  update if force_update
  @skin_data ||= YAML.load_file(skin_data_file) rescue {}
end

#test_url(options = {}) ⇒ Object


238
239
240
241
242
243
244
245
246
247
248
# File 'lib/adyen-admin/skin.rb', line 238

def test_url(options = {})
  page = Adyen::Admin.get(TEST % code)
  page = Adyen::Admin.client.submit(page.form.tap do |form|
    #:amount => 199, :currency => :shopper_locale, :country_code, :merchant_reference, :merchant_account, :system, :skip, :one_page
  end)
  Addressable::URI.parse(page.form.action).tap do |uri|
    uri.query_values = page.form.fields.inject({}) { |hash, node|
      hash[node.name] = node.value; hash
    }
  end
end

#to_sObject


250
251
252
# File 'lib/adyen-admin/skin.rb', line 250

def to_s
  self.code
end

#updateObject


127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/adyen-admin/skin.rb', line 127

def update
  @skin_data = {
    :name => name,
    :code => code,
    :uploaded_at => Time.now,
    :version => remote_version,
    :version_live => remote_version(:live),
    :version_test => remote_version(:test),
  }
  File.open(skin_data_file, "w") do |file|
    file.write @skin_data.to_yaml
  end
end

#uploadObject


219
220
221
222
223
224
225
226
227
228
# File 'lib/adyen-admin/skin.rb', line 219

def upload
  file = self.compress
  page = Adyen::Admin.get(UPLOAD % code)
  page = Adyen::Admin.client.submit(page.form.tap do |form|
    form.file_uploads.first.file_name = file
  end)
  form = page.form
  page = form.submit(page.form.button_with(:name => 'submit'))
  update
end

#uploaded_atObject


105
106
107
# File 'lib/adyen-admin/skin.rb', line 105

def uploaded_at
  skin_data[:uploaded_at]
end

#versionObject


109
110
111
# File 'lib/adyen-admin/skin.rb', line 109

def version
  skin_data[:version]
end

#version_liveObject


113
114
115
# File 'lib/adyen-admin/skin.rb', line 113

def version_live
  skin_data[:version_live]
end

#version_testObject


117
118
119
# File 'lib/adyen-admin/skin.rb', line 117

def version_test
  skin_data[:version_test]
end