shutterstock-ruby 0.3.0 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a16a3aea51b0797b15acdf0b796a4b1f318e5333
4
- data.tar.gz: 6c60678b9f218c9f4c0129d8db4846b9ee238e9d
3
+ metadata.gz: ee208d1ecc3c46db667ef7bc689ab76952ccb557
4
+ data.tar.gz: 5b1aaec166ec632891e7715186576717b0862314
5
5
  SHA512:
6
- metadata.gz: 9a868e0f76dcacb6b56b30858b588924596077ab071dd4be3e36851b55d86c220e97d3758cb650305df640174158473a2365736799e9d93d7c9734ddae67fdcd
7
- data.tar.gz: 7c867d2f9b4f0c32ef84e723150edf76ba037bcf26988fba2ba03b198e506ab312488fcd037ecc324731a13986dfca4a54ebc94679a7746c692be2d790af20cd
6
+ metadata.gz: b99eaded4fc7b274a2867a2bbb502a2486ab478e926a2c4877e343197602d0cb957714096f54c47bef15d386ccd55abd77e12987a2c026f9f8c8a2ec4c50e57d
7
+ data.tar.gz: 6ef64b682f9596a7d169d2e9eee3758c822ebb1f5b41521275a2de3fbe67bdb7d22c942cd490a9182b7d98c7aabf4165c70f550a1ec440a3dfadfd0a00b5b5fb
@@ -1,5 +1,6 @@
1
1
  require 'json'
2
2
  require 'rest_client'
3
+ require 'shutterstock-ruby/configuration'
3
4
  require 'shutterstock-ruby/connections'
4
5
  require 'shutterstock-ruby/images'
5
6
  require 'shutterstock-ruby/videos'
@@ -9,24 +10,22 @@ module ShutterstockRuby
9
10
  API_BASE = 'api.shutterstock.com/v2'
10
11
 
11
12
  def self.configuration
12
- @configuration ||= Configuration.new
13
+ @configuration ||= Configuration.new({})
13
14
  end
14
15
 
15
16
  def self.configure
16
- self.configuration ||= Configuration.new
17
+ self.configuration ||= Configuration.new({})
17
18
  yield(configuration) if block_given?
18
19
  end
19
20
 
20
- # Main configuration class.
21
- class Configuration
22
- attr_accessor :access_token
23
- attr_accessor :api_client
24
- attr_accessor :api_secret
21
+ class Client
22
+ attr_reader :configuration, :videos, :images
25
23
 
26
- def initialize
27
- @access_token = nil
28
- @api_client = nil
29
- @api_secret = nil
24
+ def initialize(args = {})
25
+ @configuration = Configuration.new(args)
26
+ @videos = Videos.new(configuration)
27
+ @images = Images.new(configuration)
30
28
  end
29
+
31
30
  end
32
31
  end
@@ -0,0 +1,13 @@
1
+ module ShutterstockRuby
2
+ class Configuration
3
+ attr_accessor :access_token
4
+ attr_accessor :api_client
5
+ attr_accessor :api_secret
6
+
7
+ def initialize(args)
8
+ @access_token = args[:access_token]
9
+ @api_client = args[:api_client]
10
+ @api_secret = args[:api_secret]
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,11 @@
1
1
  module ShutterstockRuby
2
- module Connections
2
+ class Connections
3
+ attr_reader :configuration
4
+
5
+ def initialize(configuration)
6
+ @configuration = configuration
7
+ end
8
+
3
9
  def get(path, params = nil, options = {})
4
10
  ensure_credentials!
5
11
 
@@ -18,24 +24,28 @@ module ShutterstockRuby
18
24
  RestClient.post(build_url(path), body, add_bearer(options))
19
25
  end
20
26
 
27
+ protected
28
+ def self.client
29
+ @client ||= new(ShutterstockRuby.configuration)
30
+ end
31
+
21
32
  private
22
33
 
23
34
  def build_url(path)
24
- if ShutterstockRuby.configuration.access_token
35
+ if configuration.access_token
25
36
  "https://#{ShutterstockRuby::API_BASE}#{path}"
26
37
  else
27
- "https://#{ShutterstockRuby.configuration.api_client}:#{ShutterstockRuby.configuration.api_secret}@#{ShutterstockRuby::API_BASE}#{path}"
38
+ "https://#{configuration.api_client}:#{configuration.api_secret}@#{ShutterstockRuby::API_BASE}#{path}"
28
39
  end
29
40
  end
30
41
 
31
42
  def add_bearer(options)
32
- options[:authorization] = "Bearer #{ShutterstockRuby.configuration.access_token}" if ShutterstockRuby.configuration.access_token
43
+ options[:authorization] = "Bearer #{configuration.access_token}" if configuration.access_token
33
44
  options
34
45
  end
35
46
 
36
47
  def ensure_credentials!
37
- config = ShutterstockRuby.configuration
38
- fail(Exception, 'Missing credentials') if (config.api_client.nil? || config.api_secret.nil?) && config.access_token.nil?
48
+ fail(Exception, 'Missing credentials') if (configuration.api_client.nil? || configuration.api_secret.nil?) && configuration.access_token.nil?
39
49
  end
40
50
  end
41
51
  end
@@ -1,10 +1,15 @@
1
1
  module ShutterstockRuby
2
2
  # A class to hold all images related code.
3
- class Images
4
- extend Connections
3
+ class Images < Connections
5
4
 
6
- def self.search(query, options = {})
7
- JSON.parse(self.get('/images/search', { query: query }.merge(options)))
5
+ def search(query, options = {})
6
+ JSON.parse(get('/images/search', { query: query }.merge(options)))
7
+ end
8
+
9
+ class << self
10
+ def search(query, options = {})
11
+ client.search(query, options)
12
+ end
8
13
  end
9
14
  end
10
15
  end
@@ -1,20 +1,33 @@
1
1
  module ShutterstockRuby
2
2
  # A class to hold all videos related code.
3
- class Videos
4
- extend Connections
3
+ class Videos < Connections
5
4
 
6
- def self.search(query, options = {})
7
- JSON.parse(self.get('/videos/search', { query: query }.merge(options)))
5
+ def search(query, options = {})
6
+ JSON.parse(get('/videos/search', { query: query }.merge(options)))
8
7
  end
9
8
 
10
- def self.details(id, options = {})
11
- JSON.parse(self.get('/videos', { id: id }.merge(options)))
9
+ def details(id, options = {})
10
+ JSON.parse(get('/videos', { id: id }.merge(options)))
12
11
  end
13
12
 
14
- def self.purchase(id, subscription_id, size, options = {})
13
+ def purchase(id, subscription_id, size, options = {})
15
14
  params = { subscription_id: subscription_id, size: size }
16
15
  body = { videos: [ video_id: id ] }.to_json
17
- JSON.parse(self.post("/videos/licenses", body, params, options))
16
+ JSON.parse(post("/videos/licenses", body, params, options))
17
+ end
18
+
19
+ class << self
20
+ def search(query, options = {})
21
+ client.search(query, options)
22
+ end
23
+
24
+ def details(id, options = {})
25
+ client.details(id, options)
26
+ end
27
+
28
+ def purchase(id, subscription_id, size, options = {})
29
+ client.purchase(id, subscription_id, size, options)
30
+ end
18
31
  end
19
32
  end
20
33
  end
@@ -1,6 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe ShutterstockRuby do
3
+ RSpec.describe "ShutterstockRuby instance client" do
4
+
5
+ it 'has the correct default' do
6
+ client = ShutterstockRuby::Client.new
7
+
8
+ expect(client.configuration.access_token).to be nil
9
+ expect(client.configuration.api_client).to be nil
10
+ expect(client.configuration.api_secret).to be nil
11
+ end
12
+
13
+ it 'sets the correct configuration' do
14
+ token = SecureRandom.uuid
15
+ key = SecureRandom.uuid
16
+ secret = SecureRandom.uuid
17
+
18
+ client = ShutterstockRuby::Client.new(access_token: token, api_client: key, api_secret: secret)
19
+
20
+ expect(client.configuration.access_token).to equal(token)
21
+ expect(client.configuration.api_client).to equal(key)
22
+ expect(client.configuration.api_secret).to equal(secret)
23
+ end
24
+ end
25
+
26
+
27
+ RSpec.describe "ShutterstockRuby static client" do
4
28
  after :each do
5
29
  ShutterstockRuby.configuration.access_token = nil
6
30
  ShutterstockRuby.configuration.api_client = nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shutterstock-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nadav Shatz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-11 00:00:00.000000000 Z
11
+ date: 2017-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -82,6 +82,7 @@ files:
82
82
  - README.md
83
83
  - Rakefile
84
84
  - lib/shutterstock-ruby.rb
85
+ - lib/shutterstock-ruby/configuration.rb
85
86
  - lib/shutterstock-ruby/connections.rb
86
87
  - lib/shutterstock-ruby/images.rb
87
88
  - lib/shutterstock-ruby/videos.rb