sfc-custom 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (7) hide show
  1. data/CHANGELOG +1 -0
  2. data/LICENSE +21 -0
  3. data/Manifest +5 -0
  4. data/README +0 -0
  5. data/lib/custom.rb +231 -0
  6. data/sfc-custom.gemspec +32 -0
  7. metadata +60 -0
@@ -0,0 +1 @@
1
+ v0.1. Initial release on RubyForge
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2006-7 SFC Limited, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,5 @@
1
+ README
2
+ Manifest
3
+ LICENSE
4
+ lib/custom.rb
5
+ CHANGELOG
data/README ADDED
File without changes
@@ -0,0 +1,231 @@
1
+ # = SFCcustom
2
+ # Ruby interface for SFC's Custom templating engine
3
+ #
4
+ # == Requirements
5
+ # * xmlsimple (gem install xml-simple)
6
+ # * an SFC API key
7
+ #
8
+ # == Usage
9
+ # custom = SFCcustom.new # => #<SFCcustom:0x605c70 @api_key="464809cd2debb66da895ce171c95c70c", @api="/custom.php", @host="http://custom.sfcgraphics.com">
10
+ # custom.fonts # => []
11
+ # custom.templates # => [#<SFCcustom::Template:0x604a50 @name="Sign">, #<SFCcustom::Template:0x6049b0 @name="Banner">]
12
+ # sign = custom.template('Sign') # => #<SFCcustom::Template:0x604654 @name="Sign">
13
+ # sign.blocks # => #<SFCcustom::Template:0x604654 @name="Sign">
14
+ # sign.generate! # => #<SFCcustom::Template:0x604654 @name="Sign">
15
+
16
+ require 'rubygems'
17
+ require 'cgi'
18
+ require 'net/http'
19
+ require 'xmlsimple' unless defined?(XmlSimple)
20
+ require 'digest/md5'
21
+ require 'base64'
22
+ require 'builder'
23
+
24
+ class SFCcustomException < Exception; end
25
+ class SFCcustomResultException < SFCcustomException; end
26
+ class SFCcustomTemplateDoesNotExist < SFCcustomResultException; end
27
+
28
+ class SFCcustom
29
+ attr_reader :api_key, :host, :api
30
+
31
+ def initialize(api_key = '464809cd2debb66da895ce171c95c70c')
32
+ @api_key = api_key
33
+ @host = 'custom.sfcgraphics.com'
34
+ @api = '/batch.php'
35
+ end
36
+
37
+ def fonts
38
+ []
39
+ end
40
+
41
+ # Upload a new template to SFCcustom, +data+ can either be a Base64-encoded
42
+ # string or a Tempfile or File object. In either case it must be a PDF file
43
+ # with PDFlib blocks. Returns a hash with :status and :blocks
44
+ def upload(name, url, digest = nil)
45
+
46
+ r = request('UploadTemplate', { :name => name, :url => url, :digest => digest})
47
+
48
+ { :status => r['status'] == ' ok ', :blocks => r['blocks'] }
49
+ end
50
+
51
+ # Upload a new asset to SFCcustom, +data+ must be a base64-encoded PDF
52
+ # file with PDFlib blocks. Returns a hash with :status and :blocks
53
+ def upload_asset(filename, data)
54
+ r = request('UploadAsset', { :filename => filename, :data => data })
55
+
56
+ result = { :status => r['status'] == ' ok ', :key => r['key'] }
57
+
58
+ return result
59
+ end
60
+
61
+ # Delete an existing template, return true or false on success or failure
62
+ def delete(name)
63
+ r = request('DeleteTemplate', { :name => name })
64
+ return r['status'] == ' ok '
65
+ end
66
+
67
+ def generate(name, params, resize = nil, cache = true, copy = nil)
68
+ request('GenerateCustom', { :name => name, :data => params, :resize => resize, :cache => cache, :copy => copy})
69
+ end
70
+
71
+ def fonts
72
+ request('ListFonts')
73
+ end
74
+
75
+ def template(name)
76
+ Template.new(name)
77
+ end
78
+
79
+ def templates
80
+ templates = request('ListTemplates')
81
+ r = []
82
+ if templates['templates']
83
+ templates['templates'].each do |template|
84
+ r.push(Template.new(template['template']['name'].strip))
85
+ end
86
+ return r
87
+ else
88
+ return nil
89
+ end
90
+ end
91
+
92
+ class Font
93
+ attr_accessor :name, :family
94
+ end
95
+
96
+ class Template
97
+ attr_accessor :name, :customization_signature
98
+
99
+ def initialize(name)
100
+ @name = name
101
+ end
102
+
103
+ def blocks
104
+ [Block.new, Block.new]
105
+ end
106
+
107
+ def generate!
108
+ @customization_signature = Digest::MD5.hexdigest(Time.now.to_s)
109
+ end
110
+
111
+ def preview_url
112
+ "http://custom.sfcgraphics.com/#{@customization_signature}.jpg" if @customization_signature
113
+ end
114
+ end
115
+
116
+ class Block
117
+ attr_accessor :name, :type
118
+ end
119
+
120
+ def request(type, params = nil)
121
+ builder = Builder::XmlMarkup.new(:indent => 2)
122
+ builder.instruct!(:xml, :version => "1.0", :encoding => "UTF-8")
123
+
124
+ xml = builder.tag!(type) do |b|
125
+ b.authentication do |a|
126
+ a.key(api_key)
127
+ end
128
+ case type
129
+ when 'UploadTemplate':
130
+ b.template do |t|
131
+ t.name(params[:name])
132
+ t.url(params[:url])
133
+ t.digest(params[:digest])
134
+ end
135
+
136
+ when 'UploadAsset':
137
+ b.asset do |t|
138
+ t.filename(params[:filename])
139
+ t.data(params[:data])
140
+ end
141
+
142
+ when 'DeleteTemplate':
143
+ b.template do |t|
144
+ t.name(params[:name])
145
+ end
146
+
147
+ when 'ListTemplates':
148
+
149
+ when 'ListFonts':
150
+
151
+ when 'GenerateCustom':
152
+ b.template do |t|
153
+ t.name(params[:name])
154
+ end
155
+ if params[:cache]
156
+ b.cache do |t|
157
+ t.cache(params[:cache])
158
+ end
159
+ end
160
+ b.output do |t|
161
+ t.resize(params[:resize]) if params[:resize]
162
+ t.copy(params[:copy]) if params[:copy]
163
+ end
164
+ b.blocks do |bl|
165
+ params[:data].each do |k, v|
166
+
167
+ block_type = case k.to_s
168
+ when /image|photo|picture/i
169
+ "image"
170
+ when /pdf/i
171
+ "pdf"
172
+ else
173
+ "text"
174
+ end
175
+
176
+ bl.tag!(block_type) do |blo|
177
+ blo.name(k.to_s)
178
+ if (k.to_s =~ /image|photo|picture|pdf/i) != nil
179
+ if v.is_a?(String)
180
+ if (v.to_s =~ /^http/i) != nil
181
+ blo.url(v)
182
+ else
183
+ blo.asset(v)
184
+ end
185
+ else
186
+ blo.url(v['url'])
187
+ end
188
+ blo.fitmethod(v['fitmethod']) if v['fitmethod']
189
+ blo.rotate(v['rotate']) if v['rotate']
190
+ blo.position(v['position']) if v['position']
191
+ else
192
+ if v.is_a?(String)
193
+ blo.value(v)
194
+ else
195
+ blo.value(v['value'])
196
+ blo.font(v['font']) if v['font']
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
202
+ else
203
+ # Not much else to do, eh?
204
+ end
205
+ end
206
+
207
+ puts xml
208
+
209
+ http = Net::HTTP.new(host, 80)
210
+ res = http.post("http://#{host}#{api}", "xml=#{xml}", {'Accept' => 'application/xml'})
211
+
212
+ puts res.body
213
+
214
+ begin
215
+ result = XmlSimple.xml_in(res.body, { 'ForceArray' => false })
216
+
217
+ if result['error']
218
+ case result['error'].to_s.strip
219
+ when "Template does not exist"
220
+ raise SFCcustomTemplateDoesNotExist
221
+ else
222
+ raise SFCcustomResultException
223
+ end
224
+ end
225
+ rescue
226
+ raise SFCcustomResultException
227
+ end
228
+
229
+ return result
230
+ end
231
+ end
@@ -0,0 +1,32 @@
1
+
2
+ # Gem::Specification for Sfc-custom-0.1
3
+ # Originally generated by Echoe
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{sfc-custom}
7
+ s.version = "0.1"
8
+ s.date = %q{2007-09-21}
9
+ s.summary = %q{Library for accessing SFCcustom, a web service for generating dynamic content for print}
10
+ s.require_paths = ["lib", "ext"]
11
+ s.email = %q{}
12
+ s.homepage = %q{}
13
+ s.rubyforge_project = %q{sfc}
14
+ s.description = %q{Library for accessing SFCcustom, a web service for generating dynamic content for print}
15
+ s.has_rdoc = true
16
+ s.authors = ["SFC Limited, Inc."]
17
+ s.files = ["README", "Manifest", "LICENSE", "lib/custom.rb", "CHANGELOG", "sfc-custom.gemspec"]
18
+ s.add_dependency(%q<xml-simple>, ["> 0.0.0"])
19
+ end
20
+
21
+
22
+ # # Original Rakefile source (requires the Echoe gem):
23
+ #
24
+ # require 'rubygems'
25
+ # require 'echoe'
26
+ #
27
+ # Echoe.new('sfc-custom') do |p|
28
+ # p.author = "SFC Limited, Inc."
29
+ # p.summary = "Library for accessing SFCcustom, a web service for generating dynamic content for print"
30
+ # p.dependencies = ["xml-simple"]
31
+ # p.project = "sfc"
32
+ # end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: sfc-custom
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2007-09-21 00:00:00 -04:00
8
+ summary: Library for accessing SFCcustom, a web service for generating dynamic content for print
9
+ require_paths:
10
+ - lib
11
+ - ext
12
+ email: ""
13
+ homepage: ""
14
+ rubyforge_project: sfc
15
+ description: Library for accessing SFCcustom, a web service for generating dynamic content for print
16
+ autorequire:
17
+ default_executable:
18
+ bindir: bin
19
+ has_rdoc: true
20
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
21
+ requirements:
22
+ - - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ signing_key:
28
+ cert_chain:
29
+ post_install_message:
30
+ authors:
31
+ - SFC Limited, Inc.
32
+ files:
33
+ - README
34
+ - Manifest
35
+ - LICENSE
36
+ - lib/custom.rb
37
+ - CHANGELOG
38
+ - sfc-custom.gemspec
39
+ test_files: []
40
+
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ requirements: []
50
+
51
+ dependencies:
52
+ - !ruby/object:Gem::Dependency
53
+ name: xml-simple
54
+ version_requirement:
55
+ version_requirements: !ruby/object:Gem::Version::Requirement
56
+ requirements:
57
+ - - ">"
58
+ - !ruby/object:Gem::Version
59
+ version: 0.0.0
60
+ version: