getty_connect_authentication 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3399b5809d6053c1a4724921690498b30e70600
4
+ data.tar.gz: a3a0e582230a2f9e1bde90e78c041e7d2b4dbc0c
5
+ SHA512:
6
+ metadata.gz: e48f7c7d37efe503f486bb603053d83b182dc2a4094874fe191a0c8c21dc70e4d6e396d664fdd3abe2e009fc9700cd4ba805bcd114cc3336a31948aba2101036
7
+ data.tar.gz: 89c9f1a6886d0374150f44cedb198addd035786844bf0dc958d94664ccdefb21848c34406263268b1558df69968e9823fa5fe62a15cb2337251096587eba7770
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in getty_connect_authentication.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Cymen Vig
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # GettyConnectAuthentication
2
+
3
+ Access the Getty Connect API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'getty_connect_authentication'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install getty_connect_authentication
18
+
19
+ ## Completion
20
+
21
+ Only a subset of the API is implemented so far:
22
+
23
+ * getting a token
24
+ * searching for images
25
+
26
+ ## Usage
27
+
28
+ ### Configuration
29
+
30
+ `config/initializers/getty_connect_authentication.rb`:
31
+
32
+ require 'getty_connect_authentication'
33
+ GettyConnectAuthentication::Configuration.load(File.join(Rails.root, 'config', 'getty_config.yml'))
34
+
35
+ `getty_config.yml`:
36
+
37
+ id: provided by Getty
38
+ secret: provided by Getty
39
+
40
+ ### Examples
41
+
42
+ See the example directory.
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( https://github.com/cymen/getty_connect_authentication/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,2 @@
1
+ id: the id from Getty Connect
2
+ secret: the secret from Getty Connect
@@ -0,0 +1,5 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'getty_connect_authentication'
3
+
4
+ GettyConnectAuthentication::Configuration.load(File.expand_path('../../config.yml', __FILE__))
5
+ puts GettyConnectAuthentication.token
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'getty_connect_authentication/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "getty_connect_authentication"
8
+ spec.version = GettyConnectAuthentication::VERSION
9
+ spec.authors = ["Cymen Vig"]
10
+ spec.email = ["cymenvig@gmail.com"]
11
+ spec.summary = "Authentication for the Getty Images Connect API"
12
+ spec.description = "Fetch authentication tokens from the Getty Images Connect API v2"
13
+ spec.homepage = "https://github.com/cymen/getty_connect_authentication"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0") - %w(config.yml .ruby-gemset .ruby-version .gitignore)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rspec", "~> 2.14"
23
+ spec.add_development_dependency "rake", "~> 10"
24
+ spec.add_development_dependency "webmock", "~> 1.17"
25
+ end
@@ -0,0 +1,42 @@
1
+ require 'getty_connect_authentication/version'
2
+ require 'getty_connect_authentication/configuration'
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'uri'
6
+
7
+ module GettyConnectAuthentication
8
+ BASE_URI = 'https://connect.gettyimages.com'
9
+ PATH = 'oauth2/token'
10
+
11
+ def self.token
12
+ uri = self.uri
13
+ http = Net::HTTP.new(uri.host, uri.port)
14
+ http.use_ssl = uri.is_a?(URI::HTTPS)
15
+
16
+ body = {
17
+ client_id: Configuration.id,
18
+ client_secret: Configuration.secret,
19
+ grant_type: 'client_credentials'
20
+ }
21
+
22
+ request = Net::HTTP::Post.new(uri.request_uri)
23
+ request.set_form_data(body)
24
+ response = http.request(request)
25
+
26
+ case response.code.to_i
27
+ when 200
28
+ JSON.parse(response.body)
29
+ else
30
+ raise "#{response.code} response code with body: #{response.body}"
31
+ end
32
+ end
33
+
34
+ def self.uri
35
+ URI.parse "#{BASE_URI}/#{PATH}"
36
+ end
37
+
38
+ def self.reset
39
+ @token = nil
40
+ @expires = nil
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ module GettyConnectAuthentication
4
+ module Configuration
5
+
6
+ def self.load(path_to_yaml_file)
7
+ begin
8
+ configuration = YAML::load(IO.read(path_to_yaml_file))
9
+ @id = configuration['id']
10
+ @secret = configuration['secret']
11
+ @username = configuration['username']
12
+ @password = configuration['password']
13
+ return
14
+ rescue Errno::ENOENT
15
+ raise "YAML configuration file \"#{path_to_yaml_file}\" couldn't be found. Using defaults."
16
+ rescue Psych::SyntaxError
17
+ raise "YAML configuration file contains invalid syntax. Using defaults."
18
+ end
19
+ end
20
+
21
+ def self.id
22
+ @id
23
+ end
24
+
25
+ def self.secret
26
+ @secret
27
+ end
28
+
29
+ def self.username
30
+ @username
31
+ end
32
+
33
+ def self.password
34
+ @password
35
+ end
36
+
37
+ def self.reset
38
+ @id = nil
39
+ @secret = nil
40
+ @username = nil
41
+ @password = nil
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module GettyConnectAuthentication
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe GettyConnectAuthentication::Configuration do
4
+
5
+ it 'returns nil by default for id' do
6
+ GettyConnectAuthentication::Configuration.id.should be_nil
7
+ end
8
+
9
+ it 'returns nil by default for secret' do
10
+ GettyConnectAuthentication::Configuration.secret.should be_nil
11
+ end
12
+
13
+ it 'can be configured with a YAML file' do
14
+ GettyConnectAuthentication::Configuration.load(File.join(File.dirname(__FILE__), 'fixtures', 'a.yml'))
15
+
16
+ GettyConnectAuthentication::Configuration.id.should == 'abc123'
17
+ GettyConnectAuthentication::Configuration.secret.should == 'big_secret'
18
+ end
19
+
20
+ it 'raises an error if the file is not present' do
21
+ expect { GettyConnectAuthentication::Configuration.load('does_not_exist.yml') }.to raise_error
22
+ end
23
+
24
+ it 'raises an error if the file is not valid YAML' do
25
+ expect {
26
+ GettyConnectAuthentication::Configuration.load(File.join(File.dirname(__FILE__), 'fixtures', 'b.yml'))
27
+ }.to raise_error
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ id: abc123
2
+ secret: big_secret
@@ -0,0 +1 @@
1
+ id: %xyz
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe GettyConnectAuthentication do
4
+
5
+ specify 'uri returns the URI' do
6
+ GettyConnectAuthentication.uri.to_s.should == 'https://connect.gettyimages.com/oauth2/token'
7
+ end
8
+
9
+ describe 'token' do
10
+ url = nil
11
+
12
+ before(:each) do
13
+ GettyConnectAuthentication.reset
14
+ GettyConnectAuthentication::Configuration.load(File.join(File.dirname(__FILE__), 'fixtures', 'getty.yml'))
15
+ url = "#{GettyConnectAuthentication::BASE_URI}/oauth2/token"
16
+ end
17
+
18
+ it 'sends a request to BASE_URI/oauth2/token' do
19
+ stub_request(:any, url).to_return(
20
+ status: 200,
21
+ headers: {'Content-Type' => 'application/json'},
22
+ body: {access_token: 'a', expires_in: 10}.to_json
23
+ )
24
+
25
+ GettyConnectAuthentication.token
26
+
27
+ a_request(:post, url).
28
+ with(body: "client_id=my_id&client_secret=my_secret&grant_type=client_credentials").should have_been_made.once
29
+ end
30
+
31
+ it 'sets expires to current timestamp plus response expires_in value' do
32
+ stub_request(:any, url).to_return(
33
+ status: 200,
34
+ headers: {'Content-Type' => 'application/json'},
35
+ body: {access_token: 'a', expires_in: 10}.to_json
36
+ )
37
+
38
+ response = GettyConnectAuthentication.token
39
+
40
+ response['expires_in'].should be_within(1).of(10)
41
+ end
42
+
43
+ it 'returns the token in the response' do
44
+ stub_request(:any, url).to_return(
45
+ status: 200,
46
+ headers: {'Content-Type' => 'application/json'},
47
+ body: {access_token: 'the_token', expires_in: 10}.to_json
48
+ )
49
+
50
+ response = GettyConnectAuthentication.token
51
+
52
+ response['access_token'].should == 'the_token'
53
+ end
54
+
55
+ it 'raises an error if the response code is not 200' do
56
+ stub_request(:any, url).to_return(status: 403)
57
+
58
+ expect { GettyConnectAuthentication.token }.to raise_error
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,2 @@
1
+ id: my_id
2
+ secret: my_secret
@@ -0,0 +1,3 @@
1
+ require 'webmock/rspec'
2
+ require 'getty_connect_authentication'
3
+ require 'getty_connect_authentication/configuration'
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: getty_connect_authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cymen Vig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ description: Fetch authentication tokens from the Getty Images Connect API v2
70
+ email:
71
+ - cymenvig@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - config.yml.sample
81
+ - example/token.rb
82
+ - getty_connect_authentication.gemspec
83
+ - lib/getty_connect_authentication.rb
84
+ - lib/getty_connect_authentication/configuration.rb
85
+ - lib/getty_connect_authentication/version.rb
86
+ - spec/getty/connect/configuration_spec.rb
87
+ - spec/getty/connect/fixtures/a.yml
88
+ - spec/getty/connect/fixtures/b.yml
89
+ - spec/getty/connect_spec.rb
90
+ - spec/getty/fixtures/getty.yml
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/cymen/getty_connect_authentication
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Authentication for the Getty Images Connect API
116
+ test_files:
117
+ - spec/getty/connect/configuration_spec.rb
118
+ - spec/getty/connect/fixtures/a.yml
119
+ - spec/getty/connect/fixtures/b.yml
120
+ - spec/getty/connect_spec.rb
121
+ - spec/getty/fixtures/getty.yml
122
+ - spec/spec_helper.rb