giddy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ giddy (0.0.1)
5
+ httparty (>= 0.11.0)
6
+ json (>= 1.8.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ httparty (0.11.0)
12
+ multi_json (~> 1.0)
13
+ multi_xml (>= 0.5.2)
14
+ json (1.8.0)
15
+ multi_json (1.8.0)
16
+ multi_xml (0.5.5)
17
+ rake (10.1.0)
18
+ rdoc (4.0.1)
19
+ json (~> 1.4)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ giddy!
26
+ rake
27
+ rdoc
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Brian Muller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Giddy
2
+ Giddy is a ruby gem for interacting with the Getty API.
3
+
4
+ # Installation
5
+
6
+ ```
7
+ gem install giddy
8
+ ```
9
+
10
+ ## Configuration
11
+ To set up the authorization configuration, use:
12
+
13
+ ```ruby
14
+ Giddy.setup do |config|
15
+ config.system_id = "12345"
16
+ config.system_password = "alongpassword"
17
+ config.username = "user"
18
+ config.password = "password"
19
+ end
20
+ ```
21
+
22
+ ## Usage
23
+ Searching:
24
+ ```ruby
25
+ images = Giddy::Image.find(:query => "puppy")
26
+ # or, with pagination
27
+ images = Giddy::Image.find(:query => "kitty", :start => 21, :limit => 20)
28
+ ```
29
+
30
+ Get an images details:
31
+ ```ruby
32
+ image = Giddy::Image(:image_id => "182405488")
33
+ puts image
34
+ puts image.artist
35
+ ```
36
+
37
+ Download request:
38
+ ```ruby
39
+ puts image.download_largest
40
+ ```
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rdoc/task'
3
+
4
+ desc "Create documentation"
5
+ Rake::RDocTask.new("doc") { |rdoc|
6
+ rdoc.title = "Giddy - Getty Connect API library for Ruby"
7
+ rdoc.rdoc_dir = 'docs'
8
+ rdoc.rdoc_files.include('README.md')
9
+ rdoc.rdoc_files.include('lib/**/*.rb')
10
+ }
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "giddy/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "giddy"
6
+ s.version = Giddy::VERSION
7
+ s.authors = ["Brian Muller"]
8
+ s.email = ["bamuller@gmail.com"]
9
+ s.homepage = "https://github.com/opbandit/giddy"
10
+ s.summary = "Ruby client for Getty API"
11
+ s.description = "Ruby client for Getty API"
12
+
13
+ s.rubyforge_project = "giddy"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ s.add_dependency("httparty", ">= 0.11.0")
20
+ s.add_dependency("json", ">= 1.8.0")
21
+ s.add_development_dependency('rdoc')
22
+ s.add_development_dependency('rake')
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'json'
2
+ require 'httparty'
3
+
4
+ require "giddy/version"
5
+ require "giddy/config"
6
+ require "giddy/mediator"
7
+ require "giddy/session"
8
+ require "giddy/search"
9
+ require "giddy/download"
10
+ require "giddy/image"
11
+ require "giddy/utils"
12
+
13
+ module Giddy
14
+ def self.config
15
+ @config ||= Config.new
16
+ end
17
+
18
+ def self.setup(&block)
19
+ config.clear_session
20
+ yield config
21
+ config.check!
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ module Giddy
2
+
3
+ class Config
4
+ def self.required_fields
5
+ [ :system_id, :system_password, :username, :password ]
6
+ end
7
+
8
+ # system specific information
9
+ attr_accessor :system_id, :system_password
10
+
11
+ # user specific information
12
+ attr_accessor :username, :password
13
+
14
+ # session information
15
+ attr_accessor :token, :secure_token
16
+
17
+ def check!
18
+ self.class.required_fields.each do |required_field|
19
+ unless send(required_field)
20
+ raise MissingConfigurationError, "#{required_field} must be set"
21
+ end
22
+ end
23
+ end
24
+
25
+ def clear_session
26
+ @token = nil
27
+ @secure_token = nil
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,28 @@
1
+ module Giddy
2
+ class Download
3
+ extend Mediator
4
+
5
+ def self.path
6
+ "download"
7
+ end
8
+
9
+ def self.search_for_images(attrs)
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)
17
+ ids = [ids].flatten.map { |id| { :ImageId => id } }
18
+ result = gettyup :GetLargestImageDownloadAuthorizations, { :Images => ids }
19
+ result["Images"].inject({}) { |h,i| h[i["ImageId"]] = Utils.rubified_hash(i); h }
20
+ end
21
+
22
+ def self.create_download_request(tokens)
23
+ tokens = [tokens].flatten.map { |token| { :DownloadToken => token } }
24
+ result = gettyup :CreateDownloadRequest, { :DownloadItems => tokens }, "CreateDownload", true
25
+ result["DownloadUrls"].inject({}) { |h,i| h[i["ImageId"]] = Utils.rubified_hash(i); h }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,50 @@
1
+ module Giddy
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
7
+ @attrs = Utils.rubified_hash(attrs)
8
+ end
9
+
10
+ def method_missing(method, *args, &block)
11
+ @attrs.fetch(method, nil)
12
+ end
13
+
14
+ def largest_available
15
+ result = Download.get_largest_image_download_authorizations(@attrs[:image_id])
16
+ result[@attrs[:image_id]]
17
+ end
18
+
19
+ def download_request(token)
20
+ result = Download.create_download_request(token)
21
+ result[@attrs[:image_id]]
22
+ end
23
+
24
+ def download_largest
25
+ authorizations = largest_available[:authorizations]
26
+ return nil if authorizations.length == 0
27
+ download_request authorizations.first[:download_token]
28
+ end
29
+
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
+ def to_s
46
+ as = @attrs.map { |k,v| "#{k}=#{v}" }.join(", ")
47
+ "<Image #{as}>"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,41 @@
1
+ module Giddy
2
+ module Mediator
3
+ ROOTPATH = "https://connect.gettyimages.com/v1"
4
+
5
+ def gettyup(name, data, bodyname=nil, secure=false, check_token=true)
6
+ token = secure ? Giddy.config.secure_token : Giddy.config.token
7
+ if token.nil? and check_token
8
+ Session.create_session
9
+ token = secure ? Giddy.config.secure_token : Giddy.config.token
10
+ end
11
+
12
+ result = fetch(token, bodyname, name, data)
13
+
14
+ if reauth_needed?(result)
15
+ Session.create_session
16
+ gettyup name, data, bodyname
17
+ elsif result["ResponseHeader"]["Status"] == "success"
18
+ result["#{name}Result"]
19
+ else
20
+ raise "Error fetching #{name}: #{result["ResponseHeader"]}"
21
+ end
22
+ end
23
+
24
+ def fetch(token, bodyname, name, data)
25
+ body = { :RequestHeader => { :Token => token }, "#{bodyname || name}RequestBody" => data }
26
+ headers = { 'Content-Type' => 'application/json' }
27
+ url = "#{ROOTPATH}/#{path}/#{name}"
28
+ HTTParty.post(url, :body => body.to_json, :headers => headers)
29
+ end
30
+
31
+ def reauth_needed?(result)
32
+ return true if result.body == "<h1>Developer Inactive</h1>"
33
+ reauth_codes = [ "AUTH-012", "AUTH-010" ]
34
+ if result["ResponseHeader"]["StatusList"].length > 0
35
+ reauth_codes.include? result["ResponseHeader"]["StatusList"].first["Code"]
36
+ else
37
+ false
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ module Giddy
2
+ class Search
3
+ extend Mediator
4
+
5
+ def self.path
6
+ "search"
7
+ end
8
+
9
+ def self.search_for_images(attrs)
10
+ result = gettyup :SearchForImages, attrs, :SearchForImages2
11
+ result["Images"].map { |attrs|
12
+ Image.new(attrs)
13
+ }
14
+ end
15
+
16
+ def self.get_image_details(ids)
17
+ attrs = { :ImageIds => [ids].flatten, :Language => 'en-us' }
18
+ result = gettyup :GetImageDetails, attrs
19
+ result["Images"]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
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
@@ -0,0 +1,23 @@
1
+ module Giddy
2
+ class Utils
3
+ # covert camel case keys to underscore
4
+ def self.rubified_hash(h)
5
+ if h.is_a?(Array)
6
+ return h.map { |v| rubified_hash(v) }
7
+ elsif not h.is_a?(Hash)
8
+ return h
9
+ end
10
+
11
+ fixed = {}
12
+ h.each { |key, value|
13
+ key = key.to_s.gsub(/::/, '/').
14
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
15
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
16
+ tr("-", "_").
17
+ downcase
18
+ fixed[key.intern] = rubified_hash(value)
19
+ }
20
+ fixed
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Giddy
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: giddy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Brian Muller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2013-10-02 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.11.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rdoc
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ description: Ruby client for Getty API
60
+ email:
61
+ - bamuller@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - giddy.gemspec
76
+ - lib/giddy.rb
77
+ - lib/giddy/config.rb
78
+ - lib/giddy/download.rb
79
+ - lib/giddy/image.rb
80
+ - lib/giddy/mediator.rb
81
+ - lib/giddy/search.rb
82
+ - lib/giddy/session.rb
83
+ - lib/giddy/utils.rb
84
+ - lib/giddy/version.rb
85
+ homepage: https://github.com/opbandit/giddy
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 1099659910985099941
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 1099659910985099941
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project: giddy
114
+ rubygems_version: 1.8.24
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Ruby client for Getty API
118
+ test_files: []
119
+