preferredpictures 0.1.1 → 0.1.2
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/Gemfile +2 -0
- data/README.md +22 -17
- data/lib/preferredpictures.rb +121 -31
- data/lib/preferredpictures/version.rb +1 -1
- data/preferredpictures.gemspec +4 -3
- metadata +20 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7519f3ed012ccd405a556693e5a5f3d23cd7852b00d688616224907cfc7a0a0d
|
|
4
|
+
data.tar.gz: 81316ab2943a9f39223bbdfa8f6c6f82d7b01313e60d71c2e2af798ee06b8484
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9aa226f4d0645c8fea3fbdd9d698d05594dd0f38fc39af75992b7ffaeb2b5dd25c85581eebf493cf443895178617bb8ab83967a3f4838fd7cf3b279b2c9bef0e
|
|
7
|
+
data.tar.gz: 5d6eff8a9e950763012e822b789b9aaf9b9183ad4d8d764a38cdbb74bdfb3f163374d243c25df055363a5183ce2790dceb0ca07af902e49d127890e32103825e
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PreferredPictures Ruby Client Library
|
|
2
2
|
|
|
3
|
-
The [
|
|
4
|
-
[
|
|
3
|
+
The [PreferredPictures](https://preferred.pictures) PHP library provides a convenient way to call the
|
|
4
|
+
[PreferredPictures](https://preferred.pictures) API for applications written in Ruby
|
|
5
|
+
|
|
6
|
+
[View the full documentation about the PreferredPicture's API](https://docs.preferred.pictures/api-sdks/api)
|
|
7
|
+
|
|
8
|
+
[Learn more about what PreferredPictures can do.](https://docs.preferred.pictures/)
|
|
5
9
|
|
|
6
10
|
## Installation
|
|
7
11
|
|
|
@@ -20,31 +24,32 @@ Or install it yourself as:
|
|
|
20
24
|
## Usage
|
|
21
25
|
|
|
22
26
|
```ruby
|
|
27
|
+
require "preferredpictures"
|
|
23
28
|
|
|
24
29
|
# Create a new client using an issued identity
|
|
25
30
|
# and a secret key
|
|
26
|
-
client = PreferredPictures::Client.new(
|
|
31
|
+
client = PreferredPictures::Client.new(
|
|
32
|
+
identity: "testidentity",
|
|
33
|
+
secret_key: "secret123456")
|
|
27
34
|
|
|
28
35
|
# A simple example with just choices and the a tournament.
|
|
29
36
|
url = client.createChooseUrl(
|
|
30
|
-
['https://example.com/image-red.jpg',
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
'test-tournament')
|
|
37
|
+
choices: ['https://example.com/image-red.jpg',
|
|
38
|
+
'https://example.com/image-green.jpg',
|
|
39
|
+
'https://example.com/image-blue.jpg'],
|
|
40
|
+
tournament: 'test-tournament')
|
|
34
41
|
|
|
35
|
-
# A more involved example setting
|
|
36
|
-
#
|
|
42
|
+
# A more involved example setting a prefix and suffix
|
|
43
|
+
# applied to the choices for brevity
|
|
37
44
|
url = client.createChooseUrl(
|
|
38
|
-
['red', 'green', 'blue'],
|
|
39
|
-
'test-tournament',
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"https://example.com/image-",
|
|
43
|
-
".jpg")
|
|
45
|
+
choices: ['red', 'green', 'blue'],
|
|
46
|
+
tournament: 'test-tournament',
|
|
47
|
+
choices_prefix: "https://example.com/image-",
|
|
48
|
+
choices_suffix: ".jpg")
|
|
44
49
|
|
|
45
50
|
# The url returned will appear to be something like:
|
|
46
51
|
#
|
|
47
|
-
# https://api.preferred.
|
|
52
|
+
# https://api.preferred-pictures.com/choose-url?choices=red%2Cgreen%2Cblue&tournament=testing&expiration=[EXPIRATION]&uid=[UNIQUEID]&ttl=600&prefix=https%3A%2F%2Fexample.com%2Fjacket-&suffix=.jpg&identity=test-identity&signature=[SIGNATURE]
|
|
48
53
|
#
|
|
49
54
|
# which should be placed where it is needed in your application or templates.
|
|
50
55
|
```
|
data/lib/preferredpictures.rb
CHANGED
|
@@ -2,68 +2,157 @@ require "preferredpictures/version"
|
|
|
2
2
|
require 'securerandom'
|
|
3
3
|
require 'openssl'
|
|
4
4
|
|
|
5
|
+
##
|
|
6
|
+
# The encompassing class for the PreferredPictures API
|
|
7
|
+
#
|
|
5
8
|
module PreferredPictures
|
|
9
|
+
|
|
10
|
+
##
|
|
11
|
+
# A class representing an error from a PreferredPictures
|
|
12
|
+
# call
|
|
6
13
|
class Error < StandardError; end
|
|
7
14
|
|
|
15
|
+
##
|
|
16
|
+
# The client class of PreferredPictures
|
|
8
17
|
class Client
|
|
9
18
|
|
|
10
|
-
|
|
19
|
+
##
|
|
20
|
+
# Initialize a new PreferredPictures client which will be used to create
|
|
11
21
|
# and sign API requests
|
|
12
22
|
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# -
|
|
16
|
-
# -
|
|
23
|
+
# ==== Options
|
|
24
|
+
#
|
|
25
|
+
# * +:identity+ - (String) The PreferredPictures identity that will be used.
|
|
26
|
+
# * +:secret_key+ - (String) The secret key associated with the identity that is used to sign requests
|
|
27
|
+
# * +:max_choices+ - (Number) The maximum number of choices to allow
|
|
28
|
+
# * +:endpoint+ - (String) The PreferredPictures API endpoint to use for requests.
|
|
29
|
+
#
|
|
30
|
+
# ==== Examples
|
|
31
|
+
#
|
|
32
|
+
# This will create an example PreferredPictures client.
|
|
17
33
|
#
|
|
18
|
-
|
|
34
|
+
# client = PreferredPictures::Client.new(identity: "testidentity",
|
|
35
|
+
# secret_key: "secret123456")
|
|
36
|
+
#
|
|
37
|
+
def initialize(identity:, secret_key:, max_choices: 35, endpoint: "https://api.preferred-pictures.com/")
|
|
19
38
|
@identity = identity
|
|
20
39
|
@secret_key = secret_key
|
|
21
40
|
@max_choices = max_choices
|
|
22
41
|
@endpoint = endpoint
|
|
23
42
|
end
|
|
24
43
|
|
|
25
|
-
|
|
26
|
-
#
|
|
44
|
+
##
|
|
45
|
+
# Create a URL that calls the PreferredPictures API to
|
|
46
|
+
# choose one option among the supplied chocies.
|
|
47
|
+
#
|
|
48
|
+
# ==== Options
|
|
49
|
+
#
|
|
50
|
+
# * +:choices+ (Array<String>) A list of choices of which the API should choose
|
|
51
|
+
# * +:tournament+ (String) The tournament where this request will participate
|
|
52
|
+
# * +:ttl+ (Number) The time to live for an action to be taken from the choice, specified in seconds
|
|
53
|
+
# * +:expiration_ttl+ (Number) The time to live for the URL's signature, after this time the request
|
|
54
|
+
# * will no longer be valid.
|
|
55
|
+
# * +:uid+ (String) An optional unique identifier that will be used to correlate choices and
|
|
56
|
+
# * actions
|
|
57
|
+
# * +:choices_prefix+ (String) An optional prefix to prepend to all of the choices.
|
|
58
|
+
# * +:choices_suffix+ (String) An optional suffix to append to all of the choices.
|
|
59
|
+
# * +:destinations+ (Array<String) An array of destination URLs that are paired with each of the choices
|
|
60
|
+
# * +:destinations_prefix+ (String) An optional prefix to prepend to all of the destination URLs
|
|
61
|
+
# * +:destinations_suffix+ (String) An optional suffix to append to all of the destination URLs
|
|
62
|
+
# * +:go+ (Boolean) Indicate that the result should be a redirect to a destination URL from
|
|
63
|
+
# * a previously made choice
|
|
64
|
+
# * +:json+ (Boolean) Return the choice using JSON rather than a HTTP 302 redirect.
|
|
27
65
|
#
|
|
28
|
-
#
|
|
29
|
-
# - tournament: The tournament where this request will participate
|
|
30
|
-
# - ttl: The time to live for an action to be taken from the choice, specified in seconds
|
|
31
|
-
# - expiration_ttl: The time to live for the URL's signature, after this time the request
|
|
32
|
-
# will no longer be valid.
|
|
33
|
-
# - prefix: An optional prefix to prepend to all of the choices.
|
|
34
|
-
# - suffix: An optional suffix to append to all of the choices.
|
|
66
|
+
# ==== Examples
|
|
35
67
|
#
|
|
36
|
-
|
|
68
|
+
# This will create an PreferredPictures URL that will result in a choice:
|
|
69
|
+
#
|
|
70
|
+
# client = PreferredPictures::Client.new(
|
|
71
|
+
# identity: "testidentity",
|
|
72
|
+
# secret_key: "secret123456")
|
|
73
|
+
#
|
|
74
|
+
# url = client.createChooseUrl(
|
|
75
|
+
# choices: ['a', 'b', 'c'],
|
|
76
|
+
# tournament: 'test-ruby-tournment',
|
|
77
|
+
# choices_prefix: "https://example.com/image-",
|
|
78
|
+
# choices_suffix: ".jpg")
|
|
79
|
+
#
|
|
80
|
+
#
|
|
81
|
+
def createChooseUrl(choices:,
|
|
82
|
+
tournament:,
|
|
83
|
+
ttl: 600,
|
|
84
|
+
expiration_ttl: 3600,
|
|
85
|
+
uid: "",
|
|
86
|
+
choices_prefix: "",
|
|
87
|
+
choices_suffix: "",
|
|
88
|
+
destinations: [],
|
|
89
|
+
destinations_prefix: "",
|
|
90
|
+
destinations_suffix: "",
|
|
91
|
+
go: false,
|
|
92
|
+
json: false)
|
|
37
93
|
|
|
38
94
|
if choices.length() > @max_choices
|
|
39
95
|
raise Error.new "Too many choices were supplied"
|
|
40
96
|
end
|
|
41
97
|
|
|
98
|
+
if choices.length() === 0
|
|
99
|
+
raise Error.new "No choices were supplied"
|
|
100
|
+
end
|
|
101
|
+
|
|
42
102
|
params = {
|
|
43
|
-
"choices" => choices
|
|
103
|
+
"choices[]" => choices,
|
|
44
104
|
# Set the expiration of the request to be an hour
|
|
45
105
|
# from when it is generated.
|
|
46
106
|
"expiration" => Time.now.to_i + expiration_ttl,
|
|
47
|
-
# The identifier of the
|
|
107
|
+
# The identifier of the tournament
|
|
48
108
|
"tournament" => tournament,
|
|
49
|
-
# The unique id of this request
|
|
50
|
-
"uid" => SecureRandom.uuid,
|
|
51
109
|
"ttl" => ttl,
|
|
52
110
|
};
|
|
53
111
|
|
|
54
|
-
if
|
|
55
|
-
params['
|
|
112
|
+
if uid == ""
|
|
113
|
+
params['uid'] = SecureRandom.uuid
|
|
114
|
+
else
|
|
115
|
+
params['uid'] = uid
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
if choices_prefix != ""
|
|
119
|
+
params['choices_prefix'] = choices_prefix
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
if choices_suffix != ""
|
|
123
|
+
params['choices_suffix'] = choices_suffix
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
if destinations_prefix != ""
|
|
127
|
+
params['destinations_prefix'] = destinations_prefix
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
if destinations_suffix != ""
|
|
131
|
+
params['destinations_suffix'] = destinations_suffix
|
|
56
132
|
end
|
|
57
133
|
|
|
58
|
-
if
|
|
59
|
-
params['
|
|
134
|
+
if destinations.length > 0
|
|
135
|
+
params['destinations[]'] = destinations
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
if go
|
|
139
|
+
params['go'] = 'true'
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
if json
|
|
143
|
+
params['json'] = 'true'
|
|
60
144
|
end
|
|
61
145
|
|
|
62
146
|
signing_field_order = [
|
|
63
|
-
"
|
|
147
|
+
"choices_prefix",
|
|
148
|
+
"choices_suffix",
|
|
149
|
+
"choices[]",
|
|
150
|
+
"destinations_prefix",
|
|
151
|
+
"destinations_suffix",
|
|
152
|
+
"destinations[]",
|
|
64
153
|
"expiration",
|
|
65
|
-
"
|
|
66
|
-
"
|
|
154
|
+
"go",
|
|
155
|
+
"json",
|
|
67
156
|
"tournament",
|
|
68
157
|
"ttl",
|
|
69
158
|
"uid",
|
|
@@ -71,15 +160,16 @@ module PreferredPictures
|
|
|
71
160
|
|
|
72
161
|
signing_string = signing_field_order
|
|
73
162
|
.select { |field_name| params.has_key?(field_name) }
|
|
74
|
-
.map{|field_name| params[field_name]}
|
|
163
|
+
.map{ |field_name| params[field_name].kind_of?(Array) ? params[field_name].join(",") : params[field_name] }
|
|
75
164
|
.join("/")
|
|
76
165
|
|
|
77
|
-
|
|
78
166
|
sha256 = OpenSSL::Digest.new('sha256')
|
|
79
167
|
signature = OpenSSL::HMAC.hexdigest(sha256, @secret_key, signing_string)
|
|
80
168
|
|
|
81
|
-
|
|
82
|
-
|
|
169
|
+
params['signature'] = signature
|
|
170
|
+
params['identity'] = @identity
|
|
83
171
|
|
|
172
|
+
return @endpoint + "choose?" + URI.encode_www_form(params)
|
|
173
|
+
end
|
|
84
174
|
end
|
|
85
175
|
end
|
data/preferredpictures.gemspec
CHANGED
|
@@ -6,13 +6,12 @@ Gem::Specification.new do |spec|
|
|
|
6
6
|
spec.authors = ["Preferred Pictures"]
|
|
7
7
|
spec.email = ["contact@preferred.pictures"]
|
|
8
8
|
|
|
9
|
-
spec.summary = %q{
|
|
10
|
-
spec.description = %q{The
|
|
9
|
+
spec.summary = %q{PreferredPictures Ruby client library}
|
|
10
|
+
spec.description = %q{The PreferredPictures Ruby client library provides a convenient way to call the PreferredPictures API for applications written in Ruby.}
|
|
11
11
|
spec.homepage = "https://github.com/preferred-pictures/ruby"
|
|
12
12
|
spec.license = "MIT"
|
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
14
14
|
|
|
15
|
-
|
|
16
15
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
17
16
|
spec.metadata["source_code_uri"] = "https://github.com/preferred-pictures/ruby.git"
|
|
18
17
|
|
|
@@ -24,4 +23,6 @@ Gem::Specification.new do |spec|
|
|
|
24
23
|
spec.bindir = "exe"
|
|
25
24
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
26
25
|
spec.require_paths = ["lib"]
|
|
26
|
+
|
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
|
27
28
|
end
|
metadata
CHANGED
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: preferredpictures
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Preferred Pictures
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-
|
|
12
|
-
dependencies:
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
date: 2020-08-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rspec
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.2'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3.2'
|
|
27
|
+
description: The PreferredPictures Ruby client library provides a convenient way to
|
|
28
|
+
call the PreferredPictures API for applications written in Ruby.
|
|
15
29
|
email:
|
|
16
30
|
- contact@preferred.pictures
|
|
17
31
|
executables: []
|
|
@@ -55,5 +69,5 @@ requirements: []
|
|
|
55
69
|
rubygems_version: 3.0.3
|
|
56
70
|
signing_key:
|
|
57
71
|
specification_version: 4
|
|
58
|
-
summary:
|
|
72
|
+
summary: PreferredPictures Ruby client library
|
|
59
73
|
test_files: []
|