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
# File 'lib/adyen-admin/skin.rb', line 19

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

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

  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

.all(path = Skin.default_path) ⇒ Object

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/adyen-admin/skin.rb', line 39

def self.all(path = Skin.default_path)
  {}.tap do |hash|
    self.all_remote.each do |skin|
      hash[skin.code] = skin unless hash[skin.code]
    end
    self.all_local(path).each do |skin|
      hash[skin.code] = skin unless hash[skin.code]
    end
  end.values
end

.all_local(path = Skin.default_path) ⇒ Object

fetch all local skins



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

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

.all_remoteObject

fetch all remote skins



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/adyen-admin/skin.rb', line 51

def self.all_remote
  @skins_remote ||= begin
    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
end

.default_pathObject



29
30
31
# File 'lib/adyen-admin/skin.rb', line 29

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

.default_path=(path) ⇒ Object



33
34
35
# File 'lib/adyen-admin/skin.rb', line 33

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

.find(skin_code) ⇒ Object

find a skin within remote + local ones



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

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

.purge_cacheObject



77
78
79
# File 'lib/adyen-admin/skin.rb', line 77

def self.purge_cache
  @skins_remote = nil
end

Instance Method Details

#==(skin) ⇒ Object



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

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

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

Raises:

  • (ArgumentError)


174
175
176
177
178
179
180
181
182
183
184
# File 'lib/adyen-admin/skin.rb', line 174

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)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/adyen-admin/skin.rb', line 186

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
        zip_file.add(file.sub(path, code), file)
      end

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

#decompile(filename, backup = true) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/adyen-admin/skin.rb', line 143

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



137
138
139
140
141
# File 'lib/adyen-admin/skin.rb', line 137

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

#parent_skin_codeObject



210
211
212
# File 'lib/adyen-admin/skin.rb', line 210

def parent_skin_code
  "base"
end

#publishObject



226
227
228
229
230
# File 'lib/adyen-admin/skin.rb', line 226

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



98
99
100
101
# File 'lib/adyen-admin/skin.rb', line 98

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

#test_url(options = {}) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/adyen-admin/skin.rb', line 234

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



246
247
248
# File 'lib/adyen-admin/skin.rb', line 246

def to_s
  self.code
end

#updateObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/adyen-admin/skin.rb', line 121

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



215
216
217
218
219
220
221
222
223
224
# File 'lib/adyen-admin/skin.rb', line 215

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



103
104
105
# File 'lib/adyen-admin/skin.rb', line 103

def uploaded_at
  skin_data[:uploaded_at]
end

#versionObject



107
108
109
# File 'lib/adyen-admin/skin.rb', line 107

def version
  skin_data[:version]
end

#version_liveObject



111
112
113
# File 'lib/adyen-admin/skin.rb', line 111

def version_live
  skin_data[:version_live]
end

#version_testObject



115
116
117
# File 'lib/adyen-admin/skin.rb', line 115

def version_test
  skin_data[:version_test]
end