6px 1.0.0 → 1.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.
- checksums.yaml +4 -4
- data/lib/6px/client.rb +59 -28
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39535251eeafaa74bbdee0614a85c2f58dc7311c
|
4
|
+
data.tar.gz: cba260fbc32f9bc9fa2b57447f73455338dfde3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37e56948033e7b5d88e3860eaaf0eaefc4c77c3521091bf370b1e513405ed7dd5399c0845c79ee952ed3696d4de5bf167c4d61179a1ef61e0218fc3cda84dfe0
|
7
|
+
data.tar.gz: 7dc75ac6f1aa1e8f35039eba210aab888df3c61a3c99746bc03aed9b1bc2f412171c5b113da7fe61a52357d794b82453e43e16d8a3285d5dd60c63392fd9b4eb
|
data/lib/6px/client.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'base64'
|
2
3
|
|
3
|
-
class
|
4
|
+
class PX
|
4
5
|
include HTTParty
|
5
6
|
|
6
7
|
METHOD_NAMES = [:rotate, :resize, :crop, :filter, :layer, :analyze]
|
@@ -17,10 +18,9 @@ class SixPX
|
|
17
18
|
self.class.default_params key: api_key, secret: api_secret
|
18
19
|
@payload = {}
|
19
20
|
@inputs = {}
|
21
|
+
@outputs = []
|
22
|
+
@output = {}
|
20
23
|
@methods = []
|
21
|
-
@refs = {}
|
22
|
-
@type = 'jpeg'
|
23
|
-
@url = ''
|
24
24
|
@data = {}
|
25
25
|
@callback = ''
|
26
26
|
end
|
@@ -35,27 +35,38 @@ class SixPX
|
|
35
35
|
JSON.parse(response)
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
def output(images)
|
39
|
+
@outputs << @output unless @output.empty?
|
40
|
+
|
41
|
+
@output = {
|
42
|
+
ref: images,
|
43
|
+
type: 'images/jpeg',
|
44
|
+
url: '',
|
45
|
+
methods: []
|
46
|
+
}
|
47
|
+
|
41
48
|
self
|
42
49
|
end
|
43
50
|
|
44
|
-
#
|
45
|
-
def
|
46
|
-
|
51
|
+
# Sets images to that need to be processed
|
52
|
+
def inputs(inputs)
|
53
|
+
inputs.each do |k,v|
|
54
|
+
puts k, v
|
55
|
+
@inputs[k] = encode_image(v)
|
56
|
+
end
|
57
|
+
|
47
58
|
self
|
48
59
|
end
|
49
60
|
|
50
61
|
# Sets what type the output should be in. Default is jpeg.
|
51
62
|
def type(type)
|
52
|
-
@type = type
|
63
|
+
@output[:type] = type
|
53
64
|
self
|
54
65
|
end
|
55
66
|
|
56
67
|
# Sets output url
|
57
68
|
def url(url)
|
58
|
-
@url = url
|
69
|
+
@output[:url] = url
|
59
70
|
self
|
60
71
|
end
|
61
72
|
|
@@ -80,46 +91,67 @@ class SixPX
|
|
80
91
|
end
|
81
92
|
|
82
93
|
# Builds and sends the payload to 6px
|
83
|
-
def
|
94
|
+
def save
|
84
95
|
payload = build_payload.to_json
|
85
96
|
empty_variables
|
97
|
+
puts payload
|
86
98
|
response = self.class.post("jobs", body: payload).body
|
87
99
|
JSON.parse(response)
|
88
100
|
end
|
89
101
|
|
90
102
|
private
|
91
103
|
|
104
|
+
# Replaces underscores with periods in hash
|
92
105
|
def clean_up_search_params(params)
|
93
106
|
parsed_hash = {}
|
94
107
|
params.each {|i,k| parsed_hash[i.to_s.gsub('_', '.')] = k }
|
95
108
|
parsed_hash
|
96
109
|
end
|
97
110
|
|
111
|
+
# Encodes the images in Base64 if they are not hosted
|
112
|
+
def encode_image(url)
|
113
|
+
return url if URI.parse(url).scheme
|
114
|
+
|
115
|
+
raw_image = open(url, 'r').read
|
116
|
+
image_type = image_type(url)
|
117
|
+
encoded_image = Base64.encode64(raw_image)
|
118
|
+
|
119
|
+
"#{image_type};base64,#{encoded_image}"
|
120
|
+
end
|
121
|
+
|
122
|
+
def image_type(path)
|
123
|
+
png = Regexp.new("\x89PNG".force_encoding("binary"))
|
124
|
+
jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
|
125
|
+
jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
|
126
|
+
case IO.read(path, 10)
|
127
|
+
when /^GIF8/ then 'images/gif'
|
128
|
+
when /^#{png}/ then 'images/png'
|
129
|
+
when /^#{jpg}/, /^#{jpg2}/ then 'images/jpg'
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
98
133
|
# Add methods to method hash
|
99
134
|
def add_method(method, options)
|
100
|
-
@methods << {
|
135
|
+
@output[:methods] << {
|
101
136
|
'method' => method,
|
102
137
|
'options' => options
|
103
138
|
}
|
104
139
|
end
|
105
140
|
|
106
|
-
# Builds the final payload to
|
141
|
+
# Builds the final payload to save
|
107
142
|
def build_payload
|
108
|
-
|
143
|
+
@outputs << @output
|
144
|
+
|
145
|
+
payload = {
|
109
146
|
input: @inputs,
|
110
|
-
output: [
|
111
|
-
{
|
112
|
-
methods: @methods,
|
113
|
-
type: "image/#{@type}",
|
114
|
-
ref: @refs,
|
115
|
-
url: @url
|
116
|
-
}
|
117
|
-
],
|
118
147
|
data: @data,
|
119
148
|
callback: {
|
120
149
|
url: @callback
|
121
|
-
}
|
150
|
+
},
|
151
|
+
output: @outputs
|
122
152
|
}
|
153
|
+
|
154
|
+
payload
|
123
155
|
end
|
124
156
|
|
125
157
|
# Resets instance variables after request is sent
|
@@ -127,10 +159,9 @@ class SixPX
|
|
127
159
|
@payload = {}
|
128
160
|
@inputs = {}
|
129
161
|
@methods = []
|
130
|
-
@refs = {}
|
131
|
-
@type = 'jpeg'
|
132
|
-
@url = ''
|
133
162
|
@data = {}
|
134
163
|
@callback = ''
|
164
|
+
@outputs = []
|
165
|
+
@output = {}
|
135
166
|
end
|
136
167
|
end
|