fonts_dot_com 0.2.0
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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +1 -0
- data/bin/console +25 -0
- data/bin/setup +7 -0
- data/fonts_dot_com.gemspec +24 -0
- data/lib/fonts_dot_com.rb +16 -0
- data/lib/fonts_dot_com/api.rb +255 -0
- data/lib/fonts_dot_com/auth_param.rb +43 -0
- data/lib/fonts_dot_com/config.rb +19 -0
- data/lib/fonts_dot_com/request.rb +103 -0
- data/lib/fonts_dot_com/response.rb +52 -0
- data/lib/fonts_dot_com/version.rb +3 -0
- metadata +91 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: d17a2cf6d933dee1711c16f12293941a40171d98
|
|
4
|
+
data.tar.gz: a593374bdd3c01be400953e16c0ddd08df58d3c0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f9c687efab91dcdc3855e2073cc3410faa8bd69894553b6564edfc5e2cd49e79e95dabba42ab7f38fe9ab79714a64207a818af7edb3db61fe28789697c57d03d
|
|
7
|
+
data.tar.gz: 20d9ac8dcef38ebeaf3d93ee2fc54d29341eeb925e48793f574b0527b82c7712ba5e652f6e3d916d2f6d7cee0276e79bea6274c5e816e253286f1754ad76a498
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 John Friel
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# FontsDotCom
|
|
2
|
+
|
|
3
|
+
This is a simple wrapper for communicating with the fonts.com API. The fonts.com API requires signing calls with a hash that's not trivial to generate, which rules out e.g. playing around with curl.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'fonts_dot_com'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install fonts_dot_com
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### API key
|
|
24
|
+
|
|
25
|
+
Before you can use the fonts.com API, you need an API key. You can request a key here:
|
|
26
|
+
https://www.fonts.com/web-fonts/developers/request-api-key
|
|
27
|
+
|
|
28
|
+
### Configuration
|
|
29
|
+
|
|
30
|
+
`FontsDotCom` needs to be configured with your credentials before you can make API calls.
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
irb> require 'fonts_dot_com'
|
|
34
|
+
irb> FontsDotCom.configure do |config|
|
|
35
|
+
irb> config.api_key = 'whatever'
|
|
36
|
+
irb> config.public_key = 'whatever' # This, and the private key are generated
|
|
37
|
+
irb> config.private_key = 'whatever' # from the API key.
|
|
38
|
+
irb> end
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
In a Rails app, you'd want to put the above in an initializer.
|
|
42
|
+
|
|
43
|
+
### API calls
|
|
44
|
+
|
|
45
|
+
Until I write up some better documentation, here's the gist:
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
response = FontsDotCom::Api.list_projects
|
|
49
|
+
=> #<FontsDotCom::Response:0x007fdfdc1db2d0 @original_response_object=#<Net::HTTPOK 200 OK readbody=true>, @body={ :lots => :here }, @code="200">
|
|
50
|
+
irb> response.class
|
|
51
|
+
=> FontsDotCom::Response
|
|
52
|
+
irb> response.body.class
|
|
53
|
+
=> Hash
|
|
54
|
+
irb> response.body.keys
|
|
55
|
+
=> ["Projects"]
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
See lib/fonts_dot_com/api.rb for other calls.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
## Contributing
|
|
62
|
+
|
|
63
|
+
1. Fork it ( https://github.com/Postcontext/fonts_dot_com/fork )
|
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
67
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "fonts_dot_com"
|
|
5
|
+
|
|
6
|
+
# Feel free to change these if you'd like to store your config file(s)
|
|
7
|
+
# somewhere else.
|
|
8
|
+
config_dir = './config/'
|
|
9
|
+
config_filename = 'fonts_dot_com.yml'
|
|
10
|
+
|
|
11
|
+
config_file = config_dir + config_filename
|
|
12
|
+
|
|
13
|
+
if Dir.exists?(config_dir) && File.exists?(config_file)
|
|
14
|
+
|
|
15
|
+
CONFIG = YAML.load_file(config_file)
|
|
16
|
+
|
|
17
|
+
FontsDotCom.configure do |config|
|
|
18
|
+
config.public_key = CONFIG['public_key']
|
|
19
|
+
config.private_key = CONFIG['private_key']
|
|
20
|
+
config.api_key = CONFIG['api_key']
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
require "irb"
|
|
25
|
+
IRB.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'fonts_dot_com/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "fonts_dot_com"
|
|
8
|
+
spec.version = FontsDotCom::VERSION
|
|
9
|
+
spec.authors = ["John Friel"]
|
|
10
|
+
spec.email = ["john@johnfriel.net"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{For making requests to fonts.com API.}
|
|
13
|
+
spec.description = %q{Gem for making requests to fonts.com API.}
|
|
14
|
+
spec.homepage = "https://github.com/Postcontext/fonts_dot_com"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
18
|
+
spec.bindir = "bin"
|
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
20
|
+
spec.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
24
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require "fonts_dot_com/version"
|
|
2
|
+
require "fonts_dot_com/config"
|
|
3
|
+
require "fonts_dot_com/auth_param"
|
|
4
|
+
require "fonts_dot_com/request"
|
|
5
|
+
require "fonts_dot_com/response"
|
|
6
|
+
require "fonts_dot_com/api"
|
|
7
|
+
|
|
8
|
+
module FontsDotCom
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
def configure(&block)
|
|
12
|
+
FontsDotCom::Config.configure(&block)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
end
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
module FontsDotCom
|
|
2
|
+
class Api
|
|
3
|
+
class << self
|
|
4
|
+
|
|
5
|
+
##
|
|
6
|
+
#
|
|
7
|
+
# Projects
|
|
8
|
+
#
|
|
9
|
+
##
|
|
10
|
+
|
|
11
|
+
# http://www.fonts.com/web-fonts/developers/api/list-projects
|
|
12
|
+
def list_projects(options={})
|
|
13
|
+
base_path = '/rest/json/Projects/'
|
|
14
|
+
limit = options[:limit] || 10
|
|
15
|
+
offset = options[:offset] || 0
|
|
16
|
+
|
|
17
|
+
path = "#{base_path}?wfsplimit=#{limit}&wfspstart=#{offset}"
|
|
18
|
+
|
|
19
|
+
puts "path is: #{path}"
|
|
20
|
+
|
|
21
|
+
args = options.merge({
|
|
22
|
+
message: path,
|
|
23
|
+
method: :get
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
FontsDotCom::Request.fire(args)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# http://www.fonts.com/web-fonts/developers/api/add-project
|
|
30
|
+
def add_project(name)
|
|
31
|
+
unless ( name.is_a? String ) && ( name.length > 0 )
|
|
32
|
+
raise ArgumentError
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
data = {
|
|
36
|
+
wfsproject_name: name
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
response = FontsDotCom::Request.fire({
|
|
40
|
+
message: '/rest/json/Projects/',
|
|
41
|
+
method: :post,
|
|
42
|
+
data: data
|
|
43
|
+
})
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# http://www.fonts.com/web-fonts/developers/api/delete-project
|
|
47
|
+
def delete_project(project_id)
|
|
48
|
+
raise ArgumentError unless project_id
|
|
49
|
+
|
|
50
|
+
response = FontsDotCom::Request.fire({
|
|
51
|
+
message: "/rest/json/Projects/?wfspid=#{project_id}",
|
|
52
|
+
method: :delete
|
|
53
|
+
})
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
##
|
|
59
|
+
#
|
|
60
|
+
# Stylesheets
|
|
61
|
+
#
|
|
62
|
+
##
|
|
63
|
+
|
|
64
|
+
# https://www.fonts.com/web-fonts/developers/api/export
|
|
65
|
+
def export_stylesheet(project_id)
|
|
66
|
+
base_path = '/rest/json/ProjectStylesExport/'
|
|
67
|
+
path = "#{base_path}?wfspid=#{project_id}"
|
|
68
|
+
|
|
69
|
+
FontsDotCom::Request.fire({
|
|
70
|
+
message: path,
|
|
71
|
+
method: :get
|
|
72
|
+
})
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# https://www.fonts.com/web-fonts/developers/api/import
|
|
76
|
+
def import_stylesheet(project_id, project_token)
|
|
77
|
+
# NOTE: get `ProjectToken` from #export_stylesheet
|
|
78
|
+
# `project_id` is the ID of the recipient project
|
|
79
|
+
# `project_token` is the token from the project whose
|
|
80
|
+
# stylesheet is being imported
|
|
81
|
+
|
|
82
|
+
base_path = '/rest/json/ProjectStylesExport/'
|
|
83
|
+
path = "#{base_path}?wfspid=#{project_id}&wfsptoken=#{project_token}"
|
|
84
|
+
|
|
85
|
+
FontsDotCom::Request.fire({
|
|
86
|
+
message: path,
|
|
87
|
+
method: :get
|
|
88
|
+
})
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# https://www.fonts.com/web-fonts/developers/api/add-stylesheet
|
|
92
|
+
def add_stylesheet
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
###
|
|
99
|
+
#
|
|
100
|
+
# Project Fonts
|
|
101
|
+
#
|
|
102
|
+
###
|
|
103
|
+
|
|
104
|
+
def list_project_fonts(project_id, options={})
|
|
105
|
+
# `project_id` should be fonts.com's project ID (returned as
|
|
106
|
+
# `ProjectKey` by the API.)
|
|
107
|
+
raise ArgumentError unless project_id
|
|
108
|
+
|
|
109
|
+
offset_and_limit = ''
|
|
110
|
+
if options[:offset]
|
|
111
|
+
offset_and_limit += ( '&wfspstart=' + options[:offset].to_s )
|
|
112
|
+
end
|
|
113
|
+
if options[:limit]
|
|
114
|
+
offset_and_limit += ( '&wfsplimit=' + options[:limit].to_s )
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
response = FontsDotCom::Request.fire({
|
|
118
|
+
message: "/rest/json/Fonts/?wfspid=#{project_id}#{offset_and_limit}",
|
|
119
|
+
method: :get
|
|
120
|
+
})
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def add_font(options)
|
|
124
|
+
project_id = options[:project_id]
|
|
125
|
+
font_id = options[:font_id]
|
|
126
|
+
publish = options.has_key?(:publish) ? options[:publish] : true
|
|
127
|
+
|
|
128
|
+
raise ArgumentError unless project_id && font_id
|
|
129
|
+
|
|
130
|
+
data = {
|
|
131
|
+
wfsfid: font_id
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
response = FontsDotCom::Request.fire({
|
|
135
|
+
message: "/rest/json/Fonts/?wfspid=#{project_id}",
|
|
136
|
+
method: :post,
|
|
137
|
+
data: data
|
|
138
|
+
})
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
###
|
|
144
|
+
#
|
|
145
|
+
# Selectors
|
|
146
|
+
#
|
|
147
|
+
###
|
|
148
|
+
|
|
149
|
+
# https://www.fonts.com/web-fonts/developers/api/list-selectors
|
|
150
|
+
def list_selectors(project_id, options={})
|
|
151
|
+
# `project_id` should be fonts.com's project ID (returned as
|
|
152
|
+
# `ProjectKey` by the API.)
|
|
153
|
+
raise ArgumentError unless project_id
|
|
154
|
+
|
|
155
|
+
offset_and_limit = ''
|
|
156
|
+
if options[:offset]
|
|
157
|
+
offset_and_limit += ( '&wfspstart=' + options[:offset].to_s )
|
|
158
|
+
end
|
|
159
|
+
if options[:limit]
|
|
160
|
+
offset_and_limit += ( '&wfsplimit=' + options[:limit].to_s )
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
response = FontsDotCom::Request.fire({
|
|
164
|
+
message: "/rest/json/Selectors/?wfspid=#{project_id}#{offset_and_limit}",
|
|
165
|
+
method: :get
|
|
166
|
+
})
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
###
|
|
171
|
+
#
|
|
172
|
+
# Domains
|
|
173
|
+
#
|
|
174
|
+
###
|
|
175
|
+
|
|
176
|
+
# https://www.fonts.com/web-fonts/developers/api/list-domains
|
|
177
|
+
def list_domains(project_id, options={})
|
|
178
|
+
# `project_id` should be fonts.com's project ID (returned as
|
|
179
|
+
# `ProjectKey` by the API.)
|
|
180
|
+
raise ArgumentError unless project_id
|
|
181
|
+
|
|
182
|
+
offset_and_limit = ''
|
|
183
|
+
if options[:offset]
|
|
184
|
+
offset_and_limit += ( '&wfspstart=' + options[:offset].to_s )
|
|
185
|
+
end
|
|
186
|
+
if options[:limit]
|
|
187
|
+
offset_and_limit += ( '&wfsplimit=' + options[:limit].to_s )
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
response = FontsDotCom::Request.fire({
|
|
191
|
+
message: "/rest/json/Domains/?wfspid=#{project_id}#{offset_and_limit}",
|
|
192
|
+
method: :get
|
|
193
|
+
})
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# https://www.fonts.com/web-fonts/developers/api/add-domain
|
|
197
|
+
def add_domain(options)
|
|
198
|
+
project_id = options[:project_id]
|
|
199
|
+
domain_name = options[:domain_name]
|
|
200
|
+
publish = options.has_key?(:publish) ? options[:publish] : true
|
|
201
|
+
|
|
202
|
+
raise ArgumentError unless project_id && domain_name
|
|
203
|
+
|
|
204
|
+
path = "/rest/json/Domains/?wfspid=#{project_id}"
|
|
205
|
+
#path += '&wfsnopublish=1' unless publish
|
|
206
|
+
|
|
207
|
+
data = {
|
|
208
|
+
wfsdomain_name: domain_name
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
response = FontsDotCom::Request.fire({
|
|
212
|
+
message: path,
|
|
213
|
+
method: :post,
|
|
214
|
+
data: data
|
|
215
|
+
})
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
###
|
|
221
|
+
#
|
|
222
|
+
# Publish
|
|
223
|
+
#
|
|
224
|
+
###
|
|
225
|
+
|
|
226
|
+
# https://www.fonts.com/web-fonts/developers/api/publish
|
|
227
|
+
def publish
|
|
228
|
+
response = FontsDotCom::Request.fire({
|
|
229
|
+
message: "/rest/json/Publish/",
|
|
230
|
+
method: :get
|
|
231
|
+
})
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require 'openssl'
|
|
3
|
+
require 'base64'
|
|
4
|
+
|
|
5
|
+
module FontsDotCom
|
|
6
|
+
class AuthParam
|
|
7
|
+
# Returns properly-encoded md5 HMAC per
|
|
8
|
+
# http://www.fonts.com/web-fonts/developers/api/authorizationparameter
|
|
9
|
+
|
|
10
|
+
def self.create(message)
|
|
11
|
+
self.new(message).compute
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
attr_accessor :message, :param, :digest, :hasher, :concatenation, :hash, :hash64, :auth
|
|
15
|
+
|
|
16
|
+
def initialize(message)
|
|
17
|
+
@message = message
|
|
18
|
+
@digest = OpenSSL::Digest.new('md5')
|
|
19
|
+
@hasher = OpenSSL::HMAC.new(priv_key, @digest)
|
|
20
|
+
@concatenation = "#{pub_key}|#{message}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def compute
|
|
24
|
+
hasher.update(concatenation)
|
|
25
|
+
@hash = hasher.to_s
|
|
26
|
+
|
|
27
|
+
# Convert hash from hex to base 64
|
|
28
|
+
@hash64 = [[@hash].pack("H*")].pack("m0")
|
|
29
|
+
@auth = "#{pub_key}:#{@hash64}"
|
|
30
|
+
@param = URI.encode(@auth)
|
|
31
|
+
|
|
32
|
+
return @param
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def pub_key
|
|
36
|
+
pub_key = FontsDotCom::Config.public_key
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def priv_key
|
|
40
|
+
priv_key = FontsDotCom::Config.private_key
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
|
|
3
|
+
module FontsDotCom
|
|
4
|
+
class Request
|
|
5
|
+
|
|
6
|
+
def self.fire(options)
|
|
7
|
+
self.new(options).run
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
attr_accessor :request,
|
|
11
|
+
:message,
|
|
12
|
+
:method,
|
|
13
|
+
:uri,
|
|
14
|
+
:query_params,
|
|
15
|
+
:data_params,
|
|
16
|
+
:original_options
|
|
17
|
+
|
|
18
|
+
def initialize(options={})
|
|
19
|
+
set_up(options)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def set_up(options={})
|
|
23
|
+
@original_options = options
|
|
24
|
+
|
|
25
|
+
# Process options
|
|
26
|
+
|
|
27
|
+
if options.is_a? String
|
|
28
|
+
@message = options
|
|
29
|
+
else
|
|
30
|
+
@message = options[:message]
|
|
31
|
+
@method = ( options[:method] || :get).to_sym
|
|
32
|
+
@data_params = options[:data]
|
|
33
|
+
@query_params = options[:query]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
raise ArgumentError unless allowed_http_verbs.include? @method
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@uri = URI(base + message)
|
|
41
|
+
|
|
42
|
+
case method
|
|
43
|
+
when :get
|
|
44
|
+
@request = Net::HTTP::Get.new(uri)
|
|
45
|
+
when :post
|
|
46
|
+
@request = Net::HTTP::Post.new(uri)
|
|
47
|
+
when :put
|
|
48
|
+
@request = Net::HTTP::Put.new(uri)
|
|
49
|
+
when :delete
|
|
50
|
+
@request = Net::HTTP::Delete.new(uri)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Compute md5 HMAC for request
|
|
54
|
+
@auth_param = FontsDotCom::AuthParam.create @message
|
|
55
|
+
|
|
56
|
+
# Set request headers
|
|
57
|
+
@request['authorization'] = @auth_param
|
|
58
|
+
@request['appKey'] = FontsDotCom::Config.app_key
|
|
59
|
+
|
|
60
|
+
# Set form data
|
|
61
|
+
@request.set_form_data(data_params) if data_params
|
|
62
|
+
|
|
63
|
+
# Set query params
|
|
64
|
+
# TODO
|
|
65
|
+
|
|
66
|
+
@request
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def run(attempted_authentication=false)
|
|
70
|
+
res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(@request) }
|
|
71
|
+
|
|
72
|
+
@response = FontsDotCom::Response.new(res)
|
|
73
|
+
|
|
74
|
+
return @response
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
def base
|
|
80
|
+
"#{protocol}://api.fonts.com"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def base_uri
|
|
84
|
+
"#{protocol}://api.fonts.com/rest/#{format}/"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def config
|
|
88
|
+
@config ||= FontsDotCom.config
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def protocol
|
|
92
|
+
'http' #TODO make settable?
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def format
|
|
96
|
+
'json' #TODO make settable?
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def allowed_http_verbs
|
|
100
|
+
[:get, :post, :put, :delete]
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
module FontsDotCom
|
|
4
|
+
class Response
|
|
5
|
+
|
|
6
|
+
class ApiError < StandardError
|
|
7
|
+
attr :response
|
|
8
|
+
|
|
9
|
+
def initialize(response)
|
|
10
|
+
@response = response
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class AuthenticationFailed; end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
attr_accessor :original_response_object, :body, :code
|
|
17
|
+
|
|
18
|
+
def initialize(response, options={})
|
|
19
|
+
@original_response_object = response
|
|
20
|
+
|
|
21
|
+
json = JSON.parse(response.body)
|
|
22
|
+
|
|
23
|
+
@body = json
|
|
24
|
+
@code = response.code
|
|
25
|
+
|
|
26
|
+
raise error.new(self) if returned_error
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def status
|
|
30
|
+
raise 'TODO'
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def session_key
|
|
34
|
+
records[0].fetch(:sessionKey)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def returned_error
|
|
38
|
+
code[0].to_i > 2
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def error
|
|
42
|
+
# TODO
|
|
43
|
+
ApiError
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def authentication_error_code
|
|
49
|
+
raise 'todo'
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fonts_dot_com
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- John Friel
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-03-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.9'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.9'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
description: Gem for making requests to fonts.com API.
|
|
42
|
+
email:
|
|
43
|
+
- john@johnfriel.net
|
|
44
|
+
executables:
|
|
45
|
+
- console
|
|
46
|
+
- setup
|
|
47
|
+
extensions: []
|
|
48
|
+
extra_rdoc_files: []
|
|
49
|
+
files:
|
|
50
|
+
- ".gitignore"
|
|
51
|
+
- ".rspec"
|
|
52
|
+
- ".travis.yml"
|
|
53
|
+
- Gemfile
|
|
54
|
+
- LICENSE.txt
|
|
55
|
+
- README.md
|
|
56
|
+
- Rakefile
|
|
57
|
+
- bin/console
|
|
58
|
+
- bin/setup
|
|
59
|
+
- fonts_dot_com.gemspec
|
|
60
|
+
- lib/fonts_dot_com.rb
|
|
61
|
+
- lib/fonts_dot_com/api.rb
|
|
62
|
+
- lib/fonts_dot_com/auth_param.rb
|
|
63
|
+
- lib/fonts_dot_com/config.rb
|
|
64
|
+
- lib/fonts_dot_com/request.rb
|
|
65
|
+
- lib/fonts_dot_com/response.rb
|
|
66
|
+
- lib/fonts_dot_com/version.rb
|
|
67
|
+
homepage: https://github.com/Postcontext/fonts_dot_com
|
|
68
|
+
licenses:
|
|
69
|
+
- MIT
|
|
70
|
+
metadata: {}
|
|
71
|
+
post_install_message:
|
|
72
|
+
rdoc_options: []
|
|
73
|
+
require_paths:
|
|
74
|
+
- lib
|
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - ">="
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: '0'
|
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - ">="
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: '0'
|
|
85
|
+
requirements: []
|
|
86
|
+
rubyforge_project:
|
|
87
|
+
rubygems_version: 2.5.1
|
|
88
|
+
signing_key:
|
|
89
|
+
specification_version: 4
|
|
90
|
+
summary: For making requests to fonts.com API.
|
|
91
|
+
test_files: []
|