Class: SFCcustom
- Inherits:
-
Object
- Object
- SFCcustom
- Defined in:
- lib/sfc-custom.rb
Defined Under Namespace
Classes: Block, Font, Template
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Instance Method Summary collapse
- #build_request(type, params = nil) ⇒ Object
-
#delete(name) ⇒ Object
Delete an existing template.
-
#fonts ⇒ Object
List the fonts available.
-
#generate(name, params, resize = nil, cache = true, copy = nil, thumbnail = true) ⇒ Object
Generate custom output based on
name
template andparams
. -
#initialize(api_key = '464809cd2debb66da895ce171c95c70c') ⇒ SFCcustom
constructor
A new instance of SFCcustom.
- #request(type, params = nil) ⇒ Object
- #template(name) ⇒ Object
- #templates ⇒ Object
-
#upload(name, url, digest = nil) ⇒ Object
Upload a new template to SFCcustom, by passing a
URL
of a PDF file with PDFlib blocks. -
#upload_asset(filename, data) ⇒ Object
Upload a new asset to SFCcustom,
data
must be a base64-encoded PDF file with PDFlib blocks.
Constructor Details
#initialize(api_key = '464809cd2debb66da895ce171c95c70c') ⇒ SFCcustom
Returns a new instance of SFCcustom.
31 32 33 34 35 |
# File 'lib/sfc-custom.rb', line 31 def initialize(api_key = '464809cd2debb66da895ce171c95c70c') @api_key = api_key @host = 'custom.sfcgraphics.com' @api = '/batch.php' end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
29 30 31 |
# File 'lib/sfc-custom.rb', line 29 def api @api end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
29 30 31 |
# File 'lib/sfc-custom.rb', line 29 def api_key @api_key end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
29 30 31 |
# File 'lib/sfc-custom.rb', line 29 def host @host end |
Instance Method Details
#build_request(type, params = nil) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 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 203 204 205 206 207 208 209 210 211 212 213 214 215 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 241 242 243 |
# File 'lib/sfc-custom.rb', line 127 def build_request(type, params = nil) builder = Builder::XmlMarkup.new(:indent => 2) builder.instruct!(:xml, :version => "1.0", :encoding => "UTF-8") xml = builder.tag!(type) do |b| b.authentication do |a| a.key(api_key) end case type when 'UploadTemplate' b.template do |t| t.name(params[:name]) t.url(params[:url]) t.digest(params[:digest]) end when 'UploadAsset' b.asset do |t| t.filename(params[:filename]) t.data(params[:data]) end when 'DeleteTemplate' b.template do |t| t.name(params[:name]) end when 'ListTemplates' when 'ListFonts' when 'GenerateCustom' b.template do |t| t.name(params[:name]) end if params[:cache] b.cache do |t| t.cache(params[:cache]) end end b.output do |t| t.resize(params[:resize]) if params[:resize] t.copy(params[:copy]) if params[:copy] t.thumbnail(params[:thumbnail].to_s) end b.blocks do |bl| params[:data].each do |k, v| block_type = case k.to_s when /image|photo|picture/i "image" when /pdf/i "pdf" else "text" end bl.tag!(block_type) do |blo| blo.name(k.to_s) if (k.to_s =~ /image|photo|picture|pdf/i) != nil if v.is_a?(String) if (v.to_s =~ /^http/i) != nil blo.url(v) else blo.asset(v) end elsif v.is_a?(Hash) blo.template do blo.name v['template'] end blo.blocks do v.each do |kk, vv| if kk == 'template' else if kk =~ /^PDF/ blo.pdf do blo.name kk if vv.match(/http/i) blo.url vv else blo.asset vv end end else blo.text do blo.name kk blo.value {|x| x.cdata!(vv) } end end end end end else blo.url(v['url']) end blo.fitmethod(v['fitmethod']) if v['fitmethod'] blo.rotate(v['rotate']) if v['rotate'] blo.position(v['position']) if v['position'] else if v.is_a?(String) || v.nil? blo.value {|x| x.cdata!(v.to_s) } else blo.value(v['value']) blo.font(v['font']) if v['font'] end end end end end else # Not much else to do, eh? end end return xml end |
#delete(name) ⇒ Object
Delete an existing template.
Return true
or false
on success or failure, respectively
67 68 69 70 |
# File 'lib/sfc-custom.rb', line 67 def delete(name) r = request('DeleteTemplate', { :name => name }) return r['status'] == ' ok ' end |
#fonts ⇒ Object
List the fonts available
78 79 80 |
# File 'lib/sfc-custom.rb', line 78 def fonts [] end |
#generate(name, params, resize = nil, cache = true, copy = nil, thumbnail = true) ⇒ Object
Generate custom output based on name
template and params
73 74 75 |
# File 'lib/sfc-custom.rb', line 73 def generate(name, params, resize = nil, cache = true, copy = nil, thumbnail = true) request('GenerateCustom', { :name => name, :data => params, :resize => resize, :cache => cache, :copy => copy, :thumbnail => thumbnail}) end |
#request(type, params = nil) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/sfc-custom.rb', line 245 def request(type, params = nil) xml = build_request(type, params) puts xml http = Net::HTTP.new(host, 80) res = http.post("http://#{host}#{api}", "xml=#{xml}", {'Accept' => 'application/xml'}) puts res.body begin result = XmlSimple.xml_in(res.body, { 'ForceArray' => false }) if result['error'] case result['error'].to_s.strip when "Template does not exist" raise SFCcustomTemplateDoesNotExist else raise SFCcustomResultException end end rescue raise SFCcustomResultException end puts result.inspect return result end |
#template(name) ⇒ Object
82 83 84 |
# File 'lib/sfc-custom.rb', line 82 def template(name) Template.new(name) end |
#templates ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sfc-custom.rb', line 86 def templates templates = request('ListTemplates') r = [] if templates['templates'] templates['templates'].each do |template| r.push(Template.new(template['template']['name'].strip)) end return r else return nil end end |
#upload(name, url, digest = nil) ⇒ Object
Upload a new template to SFCcustom, by passing a URL
of a PDF file with PDFlib blocks. digest
(optional) is the MD5 digest of the file, compared after the file has been processed to ensure it has been transferred properly.
Returns a hash with :status and :blocks
47 48 49 50 51 52 |
# File 'lib/sfc-custom.rb', line 47 def upload(name, url, digest = nil) r = request('UploadTemplate', { :name => name, :url => url, :digest => digest}) { :status => r['status'] == ' ok ', :blocks => r['blocks'] } end |
#upload_asset(filename, data) ⇒ Object
Upload a new asset to SFCcustom, data
must be a base64-encoded PDF file with PDFlib blocks. Returns a hash with :status and :blocks
56 57 58 59 60 61 62 |
# File 'lib/sfc-custom.rb', line 56 def upload_asset(filename, data) r = request('UploadAsset', { :filename => filename, :data => data }) result = { :status => r['status'] == ' ok ', :key => r['key'] } return result end |