Class: Shutterstock::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/client/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configConfiguration (readonly)

Returns Config instance.

Returns:



12
13
14
# File 'lib/client/client.rb', line 12

def config
  @config
end

#optionsObject

Request options



15
16
17
# File 'lib/client/client.rb', line 15

def options
  @options
end

Instance Method Details

#api(request) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/client/client.rb', line 55

def api(request)

  raise AppNotConfigured, "Access Token not provided" if options[:access_token].nil? && request.send_authorization

  content_type = request.content_type
  response = connection.run_request(request.method, nil, nil, nil) do |req|
    req.url request.path
    req.headers['Authorization'] = 'Bearer '+options[:access_token] if request.send_authorization
    req.headers['Content-Type'] = request.content_type
    req.params = request.params if request.params
    req.body = (content_type =~ /json/ ? request.body.to_json : URI.encode_www_form(request.body)) if request.body
  end
  raise FailedResponse.new("Something went wrong: #{response.status}: #{response.body}", response.status) unless (response.status == request.success_status)
  response
end

#configure {|config| ... } ⇒ Object

Creates a new Shutterstock::Client instance and yields #config. Requires a block to be given.

Yields:

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/client/client.rb', line 19

def configure
  raise AppNotConfigured, "block not given" unless block_given?

  @config = Configuration.new
  yield config

  @config.api_url ||= "https://api.shutterstock.com/v2"
  raise AppNotConfigured, "Client ID not provided" if config.client_id.nil?
  raise AppNotConfigured, "Client Secret not provided" if config.client_secret.nil?
  # raise AppNotConfigured, "Access Token not provided" if config.access_token.nil? # Not needed for auth requests

  @options = {
    base_uri: config.api_url,
    client_id: config.client_id,
    client_secret: config.client_secret,
    access_token: config.access_token
  }

end

#configured?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/client/client.rb', line 39

def configured?
  !config.nil?
end

#connectionObject



43
44
45
46
47
48
# File 'lib/client/client.rb', line 43

def connection
  @connection ||= Faraday.new(config.api_url) do |conn|
    conn.response :json, content_type: /\bjson$/
    conn.adapter Faraday.default_adapter
  end
end

#request(&block) ⇒ Object



50
51
52
53
# File 'lib/client/client.rb', line 50

def request(&block)
  req = Request.new(&block)
  api(req)
end