handwritingio 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/handwritingio.rb +242 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd5daba1585a1d808c0c60859464bb84e1461d8d
4
+ data.tar.gz: af2a0d8711db4c132a687f9654d0fc286a012414
5
+ SHA512:
6
+ metadata.gz: bc248b4f87151418c48072c24db6e9fd3a6d7fca4a5353da282c721ca7fc1830df8bdc48124e5d9cb311c5f9af61cbce05bdbf1ae7eab22dc43aeaaee94fcd74
7
+ data.tar.gz: ef18f4161aaf6b7581952f05b953daf0c1a6b38bec70c8bb406d5ee9721b8160cb5b87ffd6906019db722f71b2e9f070a2875cbeb9ae31beb1249f1dec0ddd83
@@ -0,0 +1,242 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'time'
5
+ require 'forwardable'
6
+
7
+ ##
8
+ # Contains Client and supporting classes for interacting with Handwriting.io API
9
+ module Handwritingio
10
+
11
+ ##
12
+ # The Official Handwriting.io API Client
13
+ class Client
14
+ attr_reader :uri
15
+
16
+ ##
17
+ # Creates a new +Client+ given a complete uri.
18
+ #
19
+ # Prefer Client.with_credentials unless you to control hostname.
20
+ def initialize(uri)
21
+ @uri = uri.is_a?(URI) ? uri : URI.parse(uri)
22
+ end
23
+
24
+ ##
25
+ # Get a handwriting
26
+ #
27
+ # Parameters
28
+ # [id]
29
+ # Handwriting ID
30
+ # required
31
+ def handwriting(id)
32
+ Handwriting.new(JSON.parse(get("/handwritings/#{id}")))
33
+ end
34
+
35
+ ##
36
+ # Lists handwritings
37
+ #
38
+ # Parameters
39
+ # [limit]
40
+ # number of items to fetch
41
+ # defaults to 200, minimum is 1, maximum is 1000
42
+ # [offset]
43
+ # starting point in data set
44
+ # defaults to 0
45
+ # [order_dir]
46
+ # order direction
47
+ # defaults to asc, value must be one of: asc, desc
48
+ # [order_by]
49
+ # order field
50
+ # defaults to id, value must be one of: id, title, date_created, date_modified, rating_neatness, rating_cursivity, rating_embellishment, rating_character_width
51
+ def handwritings(params = {})
52
+ Handwriting.initialize_many(JSON.parse(get("/handwritings", params)))
53
+ end
54
+
55
+ ##
56
+ # Render text in the specified handwriting, as a PNG image.
57
+ #
58
+ # Parameters
59
+ #
60
+ # [handwriting_id]
61
+ # +required+
62
+ # The ID of the handwriting to use.
63
+ # [text]
64
+ # +required+
65
+ # maximum length is 9000 characters
66
+ # [handwriting_size]
67
+ # The size of the handwriting, from baseline to cap height.
68
+ # defaults to 20px, minimum is 0px, maximum is 9000px
69
+ # [handwriting_color]
70
+ # The color of the handwriting expressed as #RRGGBB.
71
+ # defaults to #000000
72
+ # [width]
73
+ # Width of the image.
74
+ # defaults to 504px, minimum is 0px, maximum is 9000px
75
+ # [height]
76
+ # Height of the image. May be set to `auto` to determine the height automatically based on the text.
77
+ # defaults to 360px, minimum is 0px, maximum is 9000px
78
+ # [min_padding]
79
+ # +experimental+
80
+ # Centers the block of text within the image, preserving at least min_padding around all four edges of the image.
81
+ # [line_spacing]
82
+ # Amount of vertical space for each line, provided as a multiplier of handwriting_size.
83
+ # defaults to 1.5, minimum is 0.0, maximum is 5.0
84
+ # [line_spacing_variance]
85
+ # Amount to randomize spacing between lines, provided as a multiplier. Example: 0.1 means the space between lines will vary by +/- 10%.
86
+ # defaults to 0.0, minimum is 0.0, maximum is 1.0
87
+ # [word_spacing_variance]
88
+ # Amount to randomize spacing between words, provided as a multiplier. Example: 0.1 means the space between words will vary by +/- 10%.
89
+ # defaults to 0.0, minimum is 0.0, maximum is 1.0
90
+ # [random_seed]
91
+ # Set this to a positive number to get a repeatable image. If this parameter is included and positive, the returned image should always be the same for the given set of parameters.
92
+ # defaults to -1
93
+ def render_png(params)
94
+ get("/render/png", params)
95
+ end
96
+
97
+ # Render text in the specified handwriting as a PDF file.
98
+ #
99
+ # Parameters
100
+ #
101
+ # [handwriting_id]
102
+ # +required+
103
+ # The ID of the handwriting to use.
104
+ # [text]
105
+ # +required+
106
+ # maximum length is 9000 characters
107
+ # [handwriting_size]
108
+ # The size of the handwriting, from baseline to cap height.
109
+ # defaults to 20pt, minimum is 0in, maximum is 100in
110
+ # [handwriting_color]
111
+ # The color of the handwriting expressed as (C,M,Y,K).
112
+ # defaults to (0, 0, 0, 1)
113
+ # [width]
114
+ # Width of the image.
115
+ # defaults to 7in, minimum is 0in, maximum is 100in
116
+ # [height]
117
+ # Height of the image. May be set to auto to determine the height automatically based on the text.
118
+ # defaults to 5in, minimum is 0in, maximum is 100in
119
+ # [min_padding]
120
+ # +experimental+
121
+ # Centers the block of text within the image, preserving at least min_padding around all four edges of the image.
122
+ # [line_spacing]
123
+ # Amount of vertical space for each line, provided as a multiplier of handwriting_size.
124
+ # defaults to 1.5, minimum is 0.0, maximum is 5.0
125
+ # [line_spacing_variance]
126
+ # Amount to randomize spacing between lines, provided as a multiplier. Example: 0.1 means the space between lines will vary by +/- 10%.
127
+ # defaults to 0.0, minimum is 0.0, maximum is 1.0
128
+ # [word_spacing_variance]
129
+ # Amount to randomize spacing between words, provided as a multiplier. Example: 0.1 means the space between words will vary by +/- 10%.
130
+ # defaults to 0.0, minimum is 0.0, maximum is 1.0
131
+ # [random_seed]
132
+ # Set this to a positive number to get a repeatable image. If this parameter is included and positive, the returned image should always be the same for the given set of parameters.
133
+ # defaults to -1
134
+ def render_pdf(params)
135
+ get("/render/pdf", params)
136
+ end
137
+
138
+ def get(path, params = {})
139
+ uri = @uri
140
+ uri.path = path
141
+
142
+ unless params.empty?
143
+ uri.query = URI.encode_www_form(params)
144
+ end
145
+
146
+ req = Net::HTTP::Get.new(uri)
147
+ req.basic_auth(uri.user, uri.password)
148
+
149
+ use_ssl = uri.scheme == 'https'
150
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) {|http|
151
+ http.request(req)
152
+ }
153
+
154
+ if res.is_a?(Net::HTTPSuccess)
155
+ return res.body
156
+ else
157
+ raise Errors.new(res)
158
+ end
159
+ end
160
+ private :get
161
+
162
+ ##
163
+ # Production API URI
164
+ DEFAULT_URI = URI.parse('https://api.handwriting.io')
165
+
166
+ ##
167
+ # Initializes a new Client with just your key and secret.
168
+ #
169
+ # This will be the most common way to get a Client instance.
170
+ # If you need more control use Client.new.
171
+ def self.with_credentials(key, secret)
172
+ uri = DEFAULT_URI
173
+ uri.user = key
174
+ uri.password = secret
175
+ new(uri)
176
+ end
177
+
178
+ end
179
+
180
+ ##
181
+ # Handwriting class returned from Client#handwritings and Client#handwriting
182
+ class Handwriting
183
+ attr_reader :id, :title, :date_created, :date_modified, :rating_neatness, :rating_cursivity, :rating_embellishment, :rating_character_width
184
+ def initialize(hash) #:nodoc:
185
+ @id = hash['id']
186
+ @title = hash['title']
187
+ @date_created = DateTime.parse(hash['date_created'])
188
+ @date_modified = DateTime.parse(hash['date_modified'])
189
+ @rating_neatness = hash['rating_neatness']
190
+ @rating_cursivity = hash['rating_cursivity']
191
+ @rating_embellishment = hash['rating_embellishment']
192
+ @rating_character_width = hash['rating_character_width']
193
+ end
194
+
195
+ def inspect #:nodoc:
196
+ "#<#{self.class.name} id=#{@id.inspect} title=#{@title.inspect}>"
197
+ end
198
+
199
+ def self.initialize_many(hashes) #:nodoc:
200
+ hashes.map{ |hash| new(hash) }
201
+ end
202
+
203
+ end
204
+
205
+ ##
206
+ # +Errors+ class for errors raised by the API
207
+ #
208
+ # Contains one or more +Error+
209
+ class Errors < RuntimeError
210
+ extend Forwardable
211
+ def_delegators :@parsed, :[], :count, :map, :each, :first, :last
212
+
213
+ attr_reader :response
214
+
215
+ def initialize(response) #:nodoc:
216
+ @response = response
217
+ @parsed = JSON.parse(response.body)['errors'].map{ |e| Error.new(e) }
218
+ end
219
+
220
+ def inspect #:nodoc:
221
+ "#<#{self.class.name} (#{self.count})>"
222
+ end
223
+
224
+ end
225
+
226
+ ##
227
+ # Error class for individual error descriptions
228
+ class Error < RuntimeError
229
+ attr_reader :error, :field
230
+
231
+ def initialize(hash) #:nodoc:
232
+ @error = hash['error']
233
+ @field = hash['field']
234
+ end
235
+
236
+ def inspect #:nodoc:
237
+ "#<#{self.class.name} #{self.error.inspect}>"
238
+ end
239
+
240
+ end
241
+
242
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handwritingio
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Ehlers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email: support@handwriting.io
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/handwritingio.rb
34
+ homepage: http://www.handwriting.io
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.6.4
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Handwriting.io API Client
58
+ test_files: []