obf 0.2.3 → 0.2.4
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 +4 -4
- data/README.md +7 -159
- data/lib/obf/external.rb +1 -1
- data/lib/obf/pdf.rb +17 -7
- data/lib/obf/utils.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3dd93aa5841bcdf4538a71b0338ab2b13f3ea674
|
4
|
+
data.tar.gz: c3b6ecf6cdeba8605bd34ac2a890b1e9f8345b4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac95324522ab772b27ded6a24215d86128b809c0b3f71cc4b08c27b15e815f948de8cb0148e67c2ab06f198e6156930b607d6c7412b7232a7d3ceb1b948d2895
|
7
|
+
data.tar.gz: 483a7f515b1fc3b96bb87cf335dc5d9efeb044da802d117e94450b61d49ba2addc9b91eda38b2cc7921eea8e08cdb1fb1903ba3c9965ceddaaf5d5a5fd8658c3
|
data/README.md
CHANGED
@@ -1,174 +1,22 @@
|
|
1
|
-
#
|
1
|
+
# OBF Parser
|
2
2
|
|
3
|
-
This ruby library is to make it easier to
|
4
|
-
[Canvas API](http://api.instructure.com).
|
3
|
+
This ruby library is to make it easier to handle .obf and .obz files
|
5
4
|
|
6
5
|
## Installation
|
7
|
-
This is packaged as the `
|
6
|
+
This is packaged as the `obf` rubygem, so you can just add the dependency to
|
8
7
|
your Gemfile or install the gem on your system:
|
9
8
|
|
10
|
-
gem install
|
9
|
+
gem install obf
|
11
10
|
|
12
11
|
To require the library in your project:
|
13
12
|
|
14
|
-
require '
|
13
|
+
require 'obf'
|
15
14
|
|
16
15
|
## Usage
|
17
16
|
|
18
|
-
### OAuth Dance
|
19
|
-
|
20
|
-
Before you can make API calls you need an access token on behalf of the current user.
|
21
|
-
In order to get an access token you'll need to do the OAuth dance (and for that you'll
|
22
|
-
need a client_id and secret. Talk to the Canvas admin about getting these values):
|
23
|
-
|
24
|
-
```ruby
|
25
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :client_id => 123, :secret => "abcdef")
|
26
|
-
url = canvas.oauth_url("https://my.site/oauth_success")
|
27
|
-
# => "https://canvas.example.com/login/oauth2/auth?client_id=123&response_type=code&redirect_uri=http%3A%2F%2Fmy.site%2Foauth_success
|
28
|
-
redirect to(url)
|
29
|
-
```
|
30
|
-
|
31
|
-
And then when the browser redirects to oauth_success:
|
32
|
-
|
33
|
-
```ruby
|
34
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :client_id => 123, :secret => "abcdef")
|
35
|
-
code = params['code']
|
36
|
-
canvas.retrieve_access_token(code, 'https://my.site/oauth_success') # this callback_url must match the one provided in the first step
|
37
|
-
# => {access_token: "qwert"}
|
38
|
-
```
|
39
|
-
### General API Calls
|
40
|
-
|
41
|
-
Once you've got an access token for a user you should save it (securely!) for future use. To use the API call:
|
42
|
-
|
43
|
-
```ruby
|
44
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :token => "qwert")
|
45
|
-
canvas.get("/api/v1/users/self/profile")
|
46
|
-
# => {id: 90210, name: "Annie Wilson", ... }
|
47
|
-
```
|
48
|
-
|
49
|
-
For POST and PUT requests the second parameter is the form parameters to append, either as a hash or
|
50
|
-
an array of arrays:
|
51
|
-
|
52
17
|
```ruby
|
53
|
-
|
54
|
-
canvas.put("/api/v1/users/self", {'user[name]' => 'Dixon Wilson', 'user[short_name]' => 'Dixon'})
|
55
|
-
# => {id: 90210, name: "Dixon Wilson", ... }
|
56
|
-
canvas.put("/api/v1/users/self", {'user' => {'name' => 'Dixon Wilson', 'short_name' => 'Dixon'}}) # this is synonymous with the previous call
|
57
|
-
# => {id: 90210, name: "Dixon Wilson", ... }
|
58
|
-
canvas.put("/api/v1/users/self", [['user[name]', 'Dixon Wilson'],['user[short_name]', 'Dixon']]) # this is synonymous with the previous call
|
59
|
-
# => {id: 90210, name: "Dixon Wilson", ... }
|
18
|
+
OBF::PDF.from_external(data, 'my.pdf')
|
60
19
|
```
|
61
20
|
|
62
|
-
|
63
|
-
|
64
|
-
```ruby
|
65
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :token => "qwert")
|
66
|
-
canvas.get("/api/v1/users/self/enrollments?type[]=TeacherEnrollment&type[]=TaEnrollment")
|
67
|
-
# => [{id: 1234, course_id: 5678, ... }, {id: 2345, course_id: 6789, ...}]
|
68
|
-
canvas.get("/api/v1/users/self/enrollments", {'type' => ['TeacherEnrollment', 'TaEnrollment']}) # this is synonymous with the previous call
|
69
|
-
# => [{id: 1234, course_id: 5678, ... }, {id: 2345, course_id: 6789, ...}]
|
70
|
-
```
|
71
|
-
|
72
|
-
### Pagination
|
73
|
-
|
74
|
-
API endpoints that return lists are often paginated, meaning they will only return the first X results
|
75
|
-
(where X depends on the endpoint and, possibly, the per_page parameter you optionally set). To get more
|
76
|
-
results you'll need to make additional API calls:
|
77
|
-
|
78
|
-
```ruby
|
79
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :token => "qwert")
|
80
|
-
list = canvas.get("/api/v1/calendar_events?all_events=true")
|
81
|
-
list.length
|
82
|
-
# => 50
|
83
|
-
list.more?
|
84
|
-
# => true (if there's another page of results)
|
85
|
-
list.next_page!
|
86
|
-
# => [...] (returns the next page of results)
|
87
|
-
list.length
|
88
|
-
# => 100 (also concatenates the results on to the previous list, if that's more convenient)
|
89
|
-
list.next_page!
|
90
|
-
# => [...]
|
91
|
-
list.length
|
92
|
-
# => 150
|
93
|
-
```
|
94
|
-
|
95
|
-
### Additional Utilities
|
96
|
-
|
97
|
-
There are also some helper methods that can make some of the other tricky parts of the Canvas API a little more approachable.
|
98
|
-
|
99
|
-
#### File Uploads
|
100
|
-
|
101
|
-
Uploading files ia typically a multi-step process. There are three different ways to upload
|
102
|
-
files.
|
103
|
-
|
104
|
-
Upload a file from the local file system:
|
105
|
-
|
106
|
-
|
107
|
-
```ruby
|
108
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :token => "qwert")
|
109
|
-
canvas.upload_file_from_local("/api/v1/users/self/files", File.open("/path/to/file.jpg"), :content_type => "image/jpeg")
|
110
|
-
# => {id: 1, display_name: "file.jpg", ... }
|
111
|
-
```
|
112
|
-
|
113
|
-
Upload a file synchronously from a remote URL:
|
114
|
-
|
115
|
-
```ruby
|
116
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :token => "qwert")
|
117
|
-
canvas.upload_file_from_url("/api/v1/users/self/files", :name => "image.jpg", :size => 12345, :url => "http://www.example.com/image.jpg")
|
118
|
-
# => {id: 1, display_name: "image.jpg", ... }
|
119
|
-
```
|
120
|
-
|
121
|
-
Upload a file asynchronouysly from a remote URL:
|
122
|
-
|
123
|
-
```ruby
|
124
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :token => "qwert")
|
125
|
-
status_url = canvas.upload_file_from_url("/api/v1/users/self/files", :asynch => true, :name => "image.jpg", :size => 12345, :url => "http://www.example.com/image.jpg")
|
126
|
-
# => "/api/v1/file_status/url"
|
127
|
-
canvas.get(status_url)
|
128
|
-
# => {upload_status: "pending"}
|
129
|
-
canvas.get(status_url)
|
130
|
-
# => {upload_status: "ready", attachment: {id: 1, display_name: "image.jpg", ... } }
|
131
|
-
```
|
132
|
-
|
133
|
-
```ruby
|
134
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :token => "qwert")
|
135
|
-
status_url = canvas.upload_file_from_url("/api/v1/users/self/files", :asynch => true, :name => "image.jpg", :size => 12345, :url => "http://www.example.com/image.jpg")
|
136
|
-
# => "/api/v1/file_status/url"
|
137
|
-
canvas.get(status_url)
|
138
|
-
# => {upload_status: "errored", message: "Invalid response code, expected 200 got 404"}
|
139
|
-
```
|
140
|
-
|
141
|
-
For any of these upload types you can optionally provide additional configuration parameters if
|
142
|
-
the upload endpoint is to an area of Canvas that supports folders (user files, course files, etc.)
|
143
|
-
|
144
|
-
```ruby
|
145
|
-
canvas = Canvas::API.new(:host => "https://canvas.example.com", :token => "qwert")
|
146
|
-
#
|
147
|
-
# upload the file to a known folder with id 1234
|
148
|
-
canvas.upload_file_from_url("/api/v1/users/self/files", :parent_folder_id => 1234, :name => "image.jpg", :size => 12345, :url => "http://www.example.com/image.jpg")
|
149
|
-
# => {id: 1, display_name: "image.jpg", ... }
|
150
|
-
#
|
151
|
-
# upload the file to a folder with the path "/friends"
|
152
|
-
canvas.upload_file_from_url("/api/v1/users/self/files", :parent_folder_path => "/friends", :name => "image.jpg", :size => 12345, :url => "http://www.example.com/image.jpg")
|
153
|
-
# => {id: 1, display_name: "image.jpg", ... }
|
154
|
-
#
|
155
|
-
# rename this file instead of overwriting a file with the same name (overwrite is the default)
|
156
|
-
canvas.upload_file_from_url("/api/v1/users/self/files", :on_duplicate => "rename", :name => "image.jpg", :size => 12345, :url => "http://www.example.com/image.jpg")
|
157
|
-
# => {id: 1, display_name: "image.jpg", ... }
|
158
|
-
```
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
#### SIS ID Encoding
|
163
|
-
|
164
|
-
In addition to regular IDs, Canvas supports [SIS IDs](https://canvas.instructure.com/doc/api/file.object_ids.html) defined
|
165
|
-
by other systems. Sometimes these IDs contain non-standard characters, which can cause problems when
|
166
|
-
trying to use them via the API. In those cases you can do the following:
|
167
|
-
|
168
|
-
```ruby
|
169
|
-
sis_course_id = canvas.encode_id("sis_course_id", "r#-789")
|
170
|
-
# => "hex:sis_course_id:72232d373839"
|
171
|
-
canvas.get("/api/v1/courses/#{sis_course_id}/enrollments")
|
172
|
-
# => [...]
|
173
|
-
```
|
21
|
+
TODO...
|
174
22
|
|
data/lib/obf/external.rb
CHANGED
@@ -156,7 +156,7 @@ module OBF::External
|
|
156
156
|
end
|
157
157
|
|
158
158
|
['images', 'sounds'].each do |type|
|
159
|
-
obj[type].each do |item|
|
159
|
+
(obj[type] || []).each do |item|
|
160
160
|
item['data_or_url'] = item['data']
|
161
161
|
if !item['data_or_url'] && item['path'] && opts['zipper']
|
162
162
|
content_type = item['content_type']
|
data/lib/obf/pdf.rb
CHANGED
@@ -48,6 +48,14 @@ module OBF::PDF
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
+
def self.fix_color(str)
|
52
|
+
@@colors ||= {}
|
53
|
+
return @@colors[str] if @@colors[str]
|
54
|
+
color = `node lib/tinycolor_convert.js "#{str}"`.strip
|
55
|
+
@@colors[str] = color
|
56
|
+
color
|
57
|
+
end
|
58
|
+
|
51
59
|
def self.build_page(pdf, obj, options)
|
52
60
|
OBF::Utils.as_progress_percent(0, 1.0) do
|
53
61
|
doc_width = 11*72 - 72
|
@@ -104,20 +112,22 @@ module OBF::PDF
|
|
104
112
|
fill = "ffffff"
|
105
113
|
border = "eeeeee"
|
106
114
|
if button['background_color']
|
107
|
-
fill =
|
115
|
+
fill = fix_color(button['background_color'])
|
108
116
|
end
|
109
117
|
if button['border_color']
|
110
|
-
border =
|
118
|
+
border = fix_color(button['border_color'])
|
111
119
|
end
|
112
120
|
pdf.fill_color fill
|
113
121
|
pdf.stroke_color border
|
114
122
|
pdf.fill_and_stroke_rounded_rectangle [0, button_height], button_width, button_height, default_radius
|
115
123
|
pdf.bounding_box([5, button_height - 5], :width => button_width - 10, :height => button_height - text_height - 5) do
|
116
|
-
image = obj['images_hash'][button['image_id']]
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
124
|
+
image = (obj['images_hash'] || {})[button['image_id']]
|
125
|
+
if image
|
126
|
+
image_local_path = image && OBF::Utils.save_image(image, options['zipper'])
|
127
|
+
if image_local_path && File.exist?(image_local_path)
|
128
|
+
pdf.image image_local_path, :fit => [button_width - 10, button_height - text_height - 5], :position => :center, :vposition => :center
|
129
|
+
File.unlink image_local_path
|
130
|
+
end
|
121
131
|
end
|
122
132
|
end
|
123
133
|
if options['pages'] && button['load_board']
|
data/lib/obf/utils.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: obf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Whitmer
|
@@ -127,7 +127,7 @@ files:
|
|
127
127
|
- lib/obf/png.rb
|
128
128
|
- lib/obf/utils.rb
|
129
129
|
- lib/tinycolor_convert.js
|
130
|
-
homepage:
|
130
|
+
homepage: https://github.com/CoughDrop/obf
|
131
131
|
licenses:
|
132
132
|
- MIT
|
133
133
|
metadata: {}
|