giddy 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +30 -6
- data/lib/giddy.rb +2 -2
- data/lib/giddy/client.rb +40 -0
- data/lib/giddy/config.rb +2 -13
- data/lib/giddy/download.rb +6 -15
- data/lib/giddy/endpoint.rb +13 -0
- data/lib/giddy/image.rb +8 -21
- data/lib/giddy/mediator.rb +33 -10
- data/lib/giddy/search.rb +9 -9
- data/lib/giddy/version.rb +1 -1
- metadata +5 -4
- data/lib/giddy/session.rb +0 -22
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -8,28 +8,35 @@ gem install giddy
|
|
8
8
|
```
|
9
9
|
|
10
10
|
## Configuration
|
11
|
-
To set up the authorization configuration, use:
|
11
|
+
To set up the authorization configuration for the system (different than your per-user credentials), use:
|
12
12
|
|
13
13
|
```ruby
|
14
14
|
Giddy.setup do |config|
|
15
15
|
config.system_id = "12345"
|
16
16
|
config.system_password = "alongpassword"
|
17
|
-
config.username = "user"
|
18
|
-
config.password = "password"
|
19
17
|
end
|
20
18
|
```
|
21
19
|
|
22
20
|
## Usage
|
21
|
+
First, create a client object with a username and password
|
22
|
+
```ruby
|
23
|
+
client = Giddy::Client.new("ausername", "apassword")
|
24
|
+
```
|
25
|
+
|
23
26
|
Searching:
|
24
27
|
```ruby
|
25
|
-
images =
|
28
|
+
images = client.search(:query => "puppy")
|
29
|
+
|
26
30
|
# or, with pagination
|
27
|
-
images =
|
31
|
+
images = client.search(:query => "kitty", :start => 21, :limit => 20)
|
32
|
+
|
33
|
+
# or, by image id (returns only one):
|
34
|
+
image = client.search(:image_id => "110740425")
|
28
35
|
```
|
29
36
|
|
30
37
|
Get an images details:
|
31
38
|
```ruby
|
32
|
-
image =
|
39
|
+
image = client.search(:image_id => "110740425")
|
33
40
|
puts image
|
34
41
|
puts image.artist
|
35
42
|
```
|
@@ -38,3 +45,20 @@ Download request:
|
|
38
45
|
```ruby
|
39
46
|
puts image.download_largest
|
40
47
|
```
|
48
|
+
|
49
|
+
It's also possible to cache session information so that you don't have to reauthenticate on each client creation. For instance:
|
50
|
+
```ruby
|
51
|
+
client = Giddy::Client.new("username", "password")
|
52
|
+
# next line happens automatically if a session doesn't exist or if a session goes stale
|
53
|
+
# let's force it, just to get some tokens
|
54
|
+
client.create_session
|
55
|
+
|
56
|
+
# these can be stored somewhere (memcache, etc)
|
57
|
+
token = client.token
|
58
|
+
secure_token = client.secure_token
|
59
|
+
|
60
|
+
# create a new client with old tokens so a new session doesn't have to be initialized
|
61
|
+
# if the tokens have gone stale, a new session will be created
|
62
|
+
otherclient = Giddy::Client.new("username", "password", token, secure_token)
|
63
|
+
puts otherclient.search(:image_id => "110740425")
|
64
|
+
```
|
data/lib/giddy.rb
CHANGED
@@ -2,9 +2,10 @@ require 'json'
|
|
2
2
|
require 'httparty'
|
3
3
|
|
4
4
|
require "giddy/version"
|
5
|
+
require "giddy/endpoint"
|
5
6
|
require "giddy/config"
|
6
7
|
require "giddy/mediator"
|
7
|
-
require "giddy/
|
8
|
+
require "giddy/client"
|
8
9
|
require "giddy/search"
|
9
10
|
require "giddy/download"
|
10
11
|
require "giddy/image"
|
@@ -16,7 +17,6 @@ module Giddy
|
|
16
17
|
end
|
17
18
|
|
18
19
|
def self.setup(&block)
|
19
|
-
config.clear_session
|
20
20
|
yield config
|
21
21
|
config.check!
|
22
22
|
end
|
data/lib/giddy/client.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module Giddy
|
2
|
+
class Client
|
3
|
+
def initialize(username, password, token=nil, secure_token=nil)
|
4
|
+
@mediator = Mediator.new(username, password, token, secure_token)
|
5
|
+
end
|
6
|
+
|
7
|
+
def search(attrs)
|
8
|
+
if attrs.has_key?(:image_id) and attrs.keys.length == 1
|
9
|
+
return Search.new(@mediator).get_image_details(attrs[:image_id]).first
|
10
|
+
end
|
11
|
+
|
12
|
+
attrs = {
|
13
|
+
:limit => 25,
|
14
|
+
:start => 1,
|
15
|
+
:query => "",
|
16
|
+
:additional => {}
|
17
|
+
}.merge(attrs)
|
18
|
+
|
19
|
+
converted = {
|
20
|
+
:Query => { :SearchPhrase => attrs[:query] },
|
21
|
+
:ResultOptions => { :ItemCount => attrs[:limit], :ItemStartNumber => attrs[:start] }
|
22
|
+
}
|
23
|
+
attrs = attrs[:additional].merge(converted)
|
24
|
+
Search.new(@mediator).search_for_images(attrs)
|
25
|
+
end
|
26
|
+
|
27
|
+
def token
|
28
|
+
@mediator.token
|
29
|
+
end
|
30
|
+
|
31
|
+
def secure_token
|
32
|
+
@mediator.secure_token
|
33
|
+
end
|
34
|
+
|
35
|
+
# not necessary typically, sessions will be autocreated
|
36
|
+
def create_session
|
37
|
+
@mediator.create_session
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/giddy/config.rb
CHANGED
@@ -2,30 +2,19 @@ module Giddy
|
|
2
2
|
|
3
3
|
class Config
|
4
4
|
def self.required_fields
|
5
|
-
[ :system_id, :system_password
|
5
|
+
[ :system_id, :system_password ]
|
6
6
|
end
|
7
7
|
|
8
8
|
# system specific information
|
9
9
|
attr_accessor :system_id, :system_password
|
10
10
|
|
11
|
-
# user specific information
|
12
|
-
attr_accessor :username, :password
|
13
|
-
|
14
|
-
# session information
|
15
|
-
attr_accessor :token, :secure_token
|
16
|
-
|
17
11
|
def check!
|
18
12
|
self.class.required_fields.each do |required_field|
|
19
13
|
unless send(required_field)
|
20
|
-
raise
|
14
|
+
raise "#{required_field} must be set in config"
|
21
15
|
end
|
22
16
|
end
|
23
17
|
end
|
24
|
-
|
25
|
-
def clear_session
|
26
|
-
@token = nil
|
27
|
-
@secure_token = nil
|
28
|
-
end
|
29
18
|
end
|
30
19
|
|
31
20
|
end
|
data/lib/giddy/download.rb
CHANGED
@@ -1,27 +1,18 @@
|
|
1
1
|
module Giddy
|
2
|
-
class Download
|
3
|
-
|
4
|
-
|
5
|
-
def self.path
|
6
|
-
"download"
|
2
|
+
class Download < Endpoint
|
3
|
+
def initialize(mediator)
|
4
|
+
super mediator, "download", true
|
7
5
|
end
|
8
6
|
|
9
|
-
def
|
10
|
-
result = gettyup :SearchForImages, attrs, :SearchForImages2
|
11
|
-
result["Images"].map { |attrs|
|
12
|
-
Image.new(attrs)
|
13
|
-
}
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.get_largest_image_download_authorizations(ids)
|
7
|
+
def get_largest_image_download_authorizations(ids)
|
17
8
|
ids = [ids].flatten.map { |id| { :ImageId => id } }
|
18
9
|
result = gettyup :GetLargestImageDownloadAuthorizations, { :Images => ids }
|
19
10
|
result["Images"].inject({}) { |h,i| h[i["ImageId"]] = Utils.rubified_hash(i); h }
|
20
11
|
end
|
21
12
|
|
22
|
-
def
|
13
|
+
def create_download_request(tokens)
|
23
14
|
tokens = [tokens].flatten.map { |token| { :DownloadToken => token } }
|
24
|
-
result = gettyup :CreateDownloadRequest, { :DownloadItems => tokens }, "CreateDownload"
|
15
|
+
result = gettyup :CreateDownloadRequest, { :DownloadItems => tokens }, "CreateDownload"
|
25
16
|
result["DownloadUrls"].inject({}) { |h,i| h[i["ImageId"]] = Utils.rubified_hash(i); h }
|
26
17
|
end
|
27
18
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Giddy
|
2
|
+
class Endpoint
|
3
|
+
def initialize(mediator, path, secure)
|
4
|
+
@mediator = mediator
|
5
|
+
@path = path
|
6
|
+
@secure = secure
|
7
|
+
end
|
8
|
+
|
9
|
+
def gettyup(name, data, bodyname=nil, secure=false)
|
10
|
+
@mediator.gettyup @path, name, data, bodyname, @secure
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/giddy/image.rb
CHANGED
@@ -1,23 +1,25 @@
|
|
1
1
|
module Giddy
|
2
2
|
class Image
|
3
|
-
def initialize(attrs)
|
4
|
-
if attrs.has_key?(:image_id) and attrs.keys.length == 1
|
5
|
-
attrs = Search.get_image_details(attrs[:image_id]).first
|
6
|
-
end
|
3
|
+
def initialize(attrs, mediator)
|
7
4
|
@attrs = Utils.rubified_hash(attrs)
|
5
|
+
@mediator = mediator
|
8
6
|
end
|
9
7
|
|
10
8
|
def method_missing(method, *args, &block)
|
11
9
|
@attrs.fetch(method, nil)
|
12
10
|
end
|
13
11
|
|
12
|
+
def downloader
|
13
|
+
@downloader ||= Download.new(@mediator)
|
14
|
+
end
|
15
|
+
|
14
16
|
def largest_available
|
15
|
-
result =
|
17
|
+
result = downloader.get_largest_image_download_authorizations(@attrs[:image_id])
|
16
18
|
result[@attrs[:image_id]]
|
17
19
|
end
|
18
20
|
|
19
21
|
def download_request(token)
|
20
|
-
result =
|
22
|
+
result = downloader.create_download_request(token)
|
21
23
|
result[@attrs[:image_id]]
|
22
24
|
end
|
23
25
|
|
@@ -27,21 +29,6 @@ module Giddy
|
|
27
29
|
download_request authorizations.first[:download_token]
|
28
30
|
end
|
29
31
|
|
30
|
-
def self.find(attrs)
|
31
|
-
attrs = {
|
32
|
-
:limit => 25,
|
33
|
-
:start => 1,
|
34
|
-
:query => "",
|
35
|
-
:additional => {}
|
36
|
-
}.merge(attrs)
|
37
|
-
converted = {
|
38
|
-
:Query => { :SearchPhrase => attrs[:query] },
|
39
|
-
:ResultOptions => { :ItemCount => attrs[:limit], :ItemStartNumber => attrs[:start] }
|
40
|
-
}
|
41
|
-
attrs = attrs[:additional].merge(converted)
|
42
|
-
Search.search_for_images(attrs)
|
43
|
-
end
|
44
|
-
|
45
32
|
def to_s
|
46
33
|
as = @attrs.map { |k,v| "#{k}=#{v}" }.join(", ")
|
47
34
|
"<Image #{as}>"
|
data/lib/giddy/mediator.rb
CHANGED
@@ -1,19 +1,42 @@
|
|
1
1
|
module Giddy
|
2
|
-
|
2
|
+
class Mediator
|
3
3
|
ROOTPATH = "https://connect.gettyimages.com/v1"
|
4
|
+
attr_reader :token, :secure_token
|
4
5
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def initialize(username, password, token=nil, secure_token=nil)
|
7
|
+
@username = username
|
8
|
+
@password = password
|
9
|
+
@token = token
|
10
|
+
@secure_token = secure_token
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_session
|
14
|
+
data = {
|
15
|
+
:SystemId => Giddy.config.system_id,
|
16
|
+
:SystemPassword => Giddy.config.system_password,
|
17
|
+
:UserName => @username,
|
18
|
+
:UserPassword => @password
|
19
|
+
}
|
20
|
+
result = fetch "session", nil, nil, :CreateSession, data
|
21
|
+
unless result["ResponseHeader"]["Status"] == "success"
|
22
|
+
raise "Error authenticating: #{result["ResponseHeader"]}"
|
23
|
+
end
|
24
|
+
@token = result["CreateSessionResult"]["Token"]
|
25
|
+
@secure_token = result["CreateSessionResult"]["SecureToken"]
|
26
|
+
end
|
27
|
+
|
28
|
+
def gettyup(path, name, data, bodyname, secure)
|
29
|
+
token = secure ? @secure_token : @token
|
30
|
+
if token.nil?
|
31
|
+
create_session
|
32
|
+
token = secure ? @secure_token : @token
|
10
33
|
end
|
11
34
|
|
12
|
-
result = fetch(token, bodyname, name, data)
|
35
|
+
result = fetch(path, token, bodyname, name, data)
|
13
36
|
|
14
37
|
if reauth_needed?(result)
|
15
|
-
|
16
|
-
gettyup name, data, bodyname
|
38
|
+
create_session
|
39
|
+
gettyup path, name, data, bodyname, secure
|
17
40
|
elsif result["ResponseHeader"]["Status"] == "success"
|
18
41
|
result["#{name}Result"]
|
19
42
|
else
|
@@ -21,7 +44,7 @@ module Giddy
|
|
21
44
|
end
|
22
45
|
end
|
23
46
|
|
24
|
-
def fetch(token, bodyname, name, data)
|
47
|
+
def fetch(path, token, bodyname, name, data)
|
25
48
|
body = { :RequestHeader => { :Token => token }, "#{bodyname || name}RequestBody" => data }
|
26
49
|
headers = { 'Content-Type' => 'application/json' }
|
27
50
|
url = "#{ROOTPATH}/#{path}/#{name}"
|
data/lib/giddy/search.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
module Giddy
|
2
|
-
class Search
|
3
|
-
|
4
|
-
|
5
|
-
def self.path
|
6
|
-
"search"
|
2
|
+
class Search < Endpoint
|
3
|
+
def initialize(mediator)
|
4
|
+
super mediator, "search", false
|
7
5
|
end
|
8
6
|
|
9
|
-
def
|
7
|
+
def search_for_images(attrs)
|
10
8
|
result = gettyup :SearchForImages, attrs, :SearchForImages2
|
11
9
|
result["Images"].map { |attrs|
|
12
|
-
Image.new(attrs)
|
10
|
+
Image.new(attrs, @mediator)
|
13
11
|
}
|
14
12
|
end
|
15
13
|
|
16
|
-
def
|
14
|
+
def get_image_details(ids)
|
17
15
|
attrs = { :ImageIds => [ids].flatten, :Language => 'en-us' }
|
18
16
|
result = gettyup :GetImageDetails, attrs
|
19
|
-
result["Images"]
|
17
|
+
result["Images"].map { |attrs|
|
18
|
+
Image.new(attrs, @mediator)
|
19
|
+
}
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
data/lib/giddy/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: giddy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brian Muller
|
@@ -74,12 +74,13 @@ files:
|
|
74
74
|
- Rakefile
|
75
75
|
- giddy.gemspec
|
76
76
|
- lib/giddy.rb
|
77
|
+
- lib/giddy/client.rb
|
77
78
|
- lib/giddy/config.rb
|
78
79
|
- lib/giddy/download.rb
|
80
|
+
- lib/giddy/endpoint.rb
|
79
81
|
- lib/giddy/image.rb
|
80
82
|
- lib/giddy/mediator.rb
|
81
83
|
- lib/giddy/search.rb
|
82
|
-
- lib/giddy/session.rb
|
83
84
|
- lib/giddy/utils.rb
|
84
85
|
- lib/giddy/version.rb
|
85
86
|
homepage: https://github.com/opbandit/giddy
|
@@ -95,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
96
|
requirements:
|
96
97
|
- - ">="
|
97
98
|
- !ruby/object:Gem::Version
|
98
|
-
hash:
|
99
|
+
hash: 2644968470803126356
|
99
100
|
segments:
|
100
101
|
- 0
|
101
102
|
version: "0"
|
@@ -104,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
105
|
requirements:
|
105
106
|
- - ">="
|
106
107
|
- !ruby/object:Gem::Version
|
107
|
-
hash:
|
108
|
+
hash: 2644968470803126356
|
108
109
|
segments:
|
109
110
|
- 0
|
110
111
|
version: "0"
|
data/lib/giddy/session.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
module Giddy
|
2
|
-
class Session
|
3
|
-
extend Mediator
|
4
|
-
|
5
|
-
def self.path
|
6
|
-
"session"
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.create_session
|
10
|
-
Giddy.config.clear_session
|
11
|
-
data = {
|
12
|
-
:SystemId => Giddy.config.system_id,
|
13
|
-
:SystemPassword => Giddy.config.system_password,
|
14
|
-
:UserName => Giddy.config.username,
|
15
|
-
:UserPassword => Giddy.config.password
|
16
|
-
}
|
17
|
-
result = gettyup :CreateSession, data, nil, false, false
|
18
|
-
Giddy.config.token = result["Token"]
|
19
|
-
Giddy.config.secure_token = result["SecureToken"]
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|