Class: Shutterstock::Client
- Inherits:
-
Object
- Object
- Shutterstock::Client
- Includes:
- Singleton
- Defined in:
- lib/client/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Configuration
readonly
Config instance.
-
#options ⇒ Object
Request options.
Instance Method Summary collapse
- #api(request) ⇒ Object
- #configure {|config| ... } ⇒ Object
- #configured? ⇒ Boolean
- #connection ⇒ Object
- #request(&block) ⇒ Object
Instance Attribute Details
#config ⇒ Configuration (readonly)
Returns Config instance.
12 13 14 |
# File 'lib/client/client.rb', line 12 def config @config end |
#options ⇒ Object
Request options
15 16 17 |
# File 'lib/client/client.rb', line 15 def @options end |
Instance Method Details
#api(request) ⇒ Object
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 [:access_token].nil? && request. content_type = request.content_type response = connection.run_request(request.method, nil, nil, nil) do |req| req.url request.path req.headers['Authorization'] = 'Bearer '+[:access_token] if request. 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.
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
39 40 41 |
# File 'lib/client/client.rb', line 39 def configured? !config.nil? end |
#connection ⇒ Object
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 |