Class: Adyen::Admin::Skin

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

Constant Summary collapse

UPLOAD_SELECT =
"https://ca-test.adyen.com/ca/ca/skin/skins.shtml"
UPLOAD =
"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.



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

def initialize(attributes = {})
  if attributes[:path] && attributes[:code]
    raise ArgumentError, "Either :path or :code has to be provided"
  end

  if new_path = attributes[:path]
    raise ArgumentError, ":path '#{new_path}' is not a valid skin" unless Skin.is_skin_path?(new_path)
    @path = new_path
    @code = skin_data[:code] || code_from_path(path)
    @name = skin_data[:name] || name_from_path(path)
  else
    @code = attributes[:code]
    raise ArgumentError, ":code is not provided" unless code
    @name = attributes[:name]
    @path = Skin.path_from_code(code) || Skin.generate_path(code, name)
  end
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject (readonly)

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



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

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

.all_localObject

fetch all local skins



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

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

.all_remoteObject

fetch all remote skins



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

def 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



38
39
40
# File 'lib/adyen-admin/skin.rb', line 38

def default_path
  @default_path || "."
end

.default_path=(default_path) ⇒ Object



42
43
44
# File 'lib/adyen-admin/skin.rb', line 42

def default_path=(default_path)
  @default_path = default_path
end

.find(skin_code) ⇒ Object

find a skin within remote + local ones



80
81
82
83
84
# File 'lib/adyen-admin/skin.rb', line 80

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

.generate_path(name, code) ⇒ Object



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

def generate_path(name, code)
  dir_name = name.gsub(' ', '-').gsub(/[^a-z0-9-]/i, '')
  File.expand_path File.join(Skin.default_path, [dir_name, code].compact.join("-"))
end

.is_skin_path?(skin_path) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/adyen-admin/skin.rb', line 90

def is_skin_path?(skin_path)
  %w(skin.yml inc css js).any? do |sub_path|
    File.exists?(File.join(skin_path.to_s, sub_path))
  end
end

.path_from_code(code) ⇒ Object



96
97
98
# File 'lib/adyen-admin/skin.rb', line 96

def path_from_code(code)
  Dir[File.join(Skin.default_path, "**/*#{code}*")].first
end

.purge_cacheObject



86
87
88
# File 'lib/adyen-admin/skin.rb', line 86

def purge_cache
  @all = nil
end

Instance Method Details

#==(skin) ⇒ Object



279
280
281
# File 'lib/adyen-admin/skin.rb', line 279

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

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

Raises:

  • (ArgumentError)


204
205
206
207
208
209
210
211
212
213
214
# File 'lib/adyen-admin/skin.rb', line 204

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)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/adyen-admin/skin.rb', line 216

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

  outfile.tap do |filename|
    `rm -f #{filename}`
    Zip::File.open(filename, Zip::File::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?(dir, file)
          zip_file.add(file.sub(dir, code), file)
        rescue Zip::ZipEntryExistsError
          # NOOP
        end
      end
    end
  end
end

#decompile(filename, backup = true) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/adyen-admin/skin.rb', line 173

def decompile(filename, backup = true)
  # create backup of current, include any files
  if 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::File.open(filename) do |zip_file|
    zip_file.each do |file|
      f_path = File.join(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(path, ".backup.zip")}`
  end
end

#default_dataObject



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

def default_data
  skin_data[:default_data] || {}
end

#downloadObject



167
168
169
170
171
# File 'lib/adyen-admin/skin.rb', line 167

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

#get_file(filename) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/adyen-admin/skin.rb', line 108

def get_file(filename)
  if path
    File.join(path, filename).tap do |file|
      if !File.exists?(file)
        return File.join(File.dirname(path), parent_skin, filename)
      end
    end
  end
end

#parent_skinObject



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

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

#publishObject



255
256
257
258
259
# File 'lib/adyen-admin/skin.rb', line 255

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



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

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

#test_url(options = {}) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/adyen-admin/skin.rb', line 263

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



275
276
277
# File 'lib/adyen-admin/skin.rb', line 275

def to_s
  code
end

#updateObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/adyen-admin/skin.rb', line 149

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

#uploadObject



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/adyen-admin/skin.rb', line 243

def upload
  file = compress

  page = Adyen::Admin.get(UPLOAD_SELECT)
  page = page.link_with(:href => Regexp.new(Regexp.escape(UPLOAD % code))).click
  page = Adyen::Admin.client.submit(page.form.tap do |form|
    form.file_uploads.first.file_name = file
  end)
  page = page.form.submit(page.form.button_with(:name => 'submit'))
  update
end

#uploaded_atObject



123
124
125
# File 'lib/adyen-admin/skin.rb', line 123

def uploaded_at
  skin_data[:uploaded_at]
end

#versionObject



127
128
129
# File 'lib/adyen-admin/skin.rb', line 127

def version
  skin_data[:version]
end

#version_liveObject



131
132
133
# File 'lib/adyen-admin/skin.rb', line 131

def version_live
  skin_data[:version_live]
end

#version_testObject



135
136
137
# File 'lib/adyen-admin/skin.rb', line 135

def version_test
  skin_data[:version_test]
end