Class: Adyen::Admin::Skin
- Inherits:
-
Object
- Object
- Adyen::Admin::Skin
- 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
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.all(path = Skin.default_path) ⇒ Object
union remote and local skins.
-
.all_local(path = Skin.default_path) ⇒ Object
fetch all local skins.
-
.all_remote ⇒ Object
fetch all remote skins.
- .default_path ⇒ Object
- .default_path=(path) ⇒ Object
-
.find(skin_code) ⇒ Object
find a skin within remote + local ones.
- .purge_cache ⇒ Object
Instance Method Summary collapse
- #==(skin) ⇒ Object
- #compile(output, pattern = /<!-- ### inc\/([a-z]+) -->(.+?)<!-- ### -->/m) ⇒ Object
- #compress(exclude = /(yml|zip|erb)$/, outfile = "#{code}.zip") ⇒ Object
- #decompile(filename, backup = true) ⇒ Object
-
#download ⇒ Object
stackoverflow.com/questions/4360043/using-wwwmechanize-to-download-a-file-to-disk-without-loading-it-all-in-memory Adyen::Admin.client.pluggable_parser.default = Mechanize::FileSaver.
-
#initialize(attributes = {}) ⇒ Skin
constructor
A new instance of Skin.
- #parent_skin_code ⇒ Object
- #publish ⇒ Object
- #skin_data(force_update = false) ⇒ Object
- #test_url(options = {}) ⇒ Object
- #to_s ⇒ Object
- #update ⇒ Object
- #upload ⇒ Object
- #uploaded_at ⇒ Object
- #version ⇒ Object
- #version_live ⇒ Object
- #version_test ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Skin
Returns a new instance of Skin.
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
#code ⇒ Object
Returns the value of attribute code.
17 18 19 |
# File 'lib/adyen-admin/skin.rb', line 17 def code @code end |
#name ⇒ Object
Returns the value of attribute name.
17 18 19 |
# File 'lib/adyen-admin/skin.rb', line 17 def name @name end |
#path ⇒ Object
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_remote ⇒ Object
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_path ⇒ Object
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_cache ⇒ Object
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
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
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 |
#download ⇒ Object
stackoverflow.com/questions/4360043/using-wwwmechanize-to-download-a-file-to-disk-without-loading-it-all-in-memory Adyen::Admin.client.pluggable_parser.default = Mechanize::FileSaver
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_code ⇒ Object
210 211 212 |
# File 'lib/adyen-admin/skin.rb', line 210 def parent_skin_code "base" end |
#publish ⇒ Object
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( = {}) 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_s ⇒ Object
246 247 248 |
# File 'lib/adyen-admin/skin.rb', line 246 def to_s self.code end |
#update ⇒ Object
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 |
#upload ⇒ Object
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.(:name => 'submit')) update end |
#uploaded_at ⇒ Object
103 104 105 |
# File 'lib/adyen-admin/skin.rb', line 103 def uploaded_at skin_data[:uploaded_at] end |
#version ⇒ Object
107 108 109 |
# File 'lib/adyen-admin/skin.rb', line 107 def version skin_data[:version] end |
#version_live ⇒ Object
111 112 113 |
# File 'lib/adyen-admin/skin.rb', line 111 def version_live skin_data[:version_live] end |
#version_test ⇒ Object
115 116 117 |
# File 'lib/adyen-admin/skin.rb', line 115 def version_test skin_data[:version_test] end |