Class: Getty::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Images, Sessions
Defined in:
lib/getty/client.rb

Constant Summary collapse

DEFAULT_CONNECTION_MIDDLEWARE =
[
  # Faraday::Response::Logger,
  FaradayMiddleware::EncodeJson,
  FaradayMiddleware::Mashify,
  FaradayMiddleware::ParseJson
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Images

#download_image, #image_details, #largest_image_authorizations, #search

Methods included from Sessions

#create_session, #renew_session

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize the client class that will be used for all getty API requests.



20
21
22
23
24
25
26
27
28
# File 'lib/getty/client.rb', line 20

def initialize(options={})
  @system_id = options[:system_id] || Getty.system_id
  @system_pwd = options[:system_pwd] || Getty.system_pwd
  @user_name = options[:user_name] || Getty.user_name
  @user_pwd = options[:user_pwd] || Getty.user_pwd
  @connection_middleware = options[:connection_middleware] || Getty.connection_middleware || []
  @connection_middleware += DEFAULT_CONNECTION_MIDDLEWARE
  @ssl = options[:ssl] || { :verify => false }
end

Instance Attribute Details

#system_idObject (readonly)

Returns the value of attribute system_id.



17
18
19
# File 'lib/getty/client.rb', line 17

def system_id
  @system_id
end

#system_pwdObject (readonly)

Returns the value of attribute system_pwd.



17
18
19
# File 'lib/getty/client.rb', line 17

def system_pwd
  @system_pwd
end

#user_nameObject (readonly)

Returns the value of attribute user_name.



17
18
19
# File 'lib/getty/client.rb', line 17

def user_name
  @user_name
end

#user_pwdObject (readonly)

Returns the value of attribute user_pwd.



17
18
19
# File 'lib/getty/client.rb', line 17

def user_pwd
  @user_pwd
end

Instance Method Details

#api_urlObject

Base URL for api requests.



48
49
50
# File 'lib/getty/client.rb', line 48

def api_url
  "https://connect.gettyimages.com/v1"
end

#connectionObject

Sets up the connection to be used for all requests based on options passed during initialization.



36
37
38
39
40
41
42
43
44
# File 'lib/getty/client.rb', line 36

def connection
  params = {}
  @connection ||= Faraday::Connection.new(:url => api_url, :ssl => ssl, :params => params, :headers => default_headers) do |builder|
    @connection_middleware.each do |middleware|
      builder.use *middleware
    end
    builder.adapter Faraday.default_adapter
  end
end

#return_error_or_body(response, response_body) ⇒ Object

Helper method to return errors or desired response data as appropriate.

Added just for convenience to avoid having to traverse farther down the response just to get to returned data.



56
57
58
59
60
61
62
# File 'lib/getty/client.rb', line 56

def return_error_or_body(response, response_body)
  if response.status == 200
    response_body
  else
    raise Getty::APIError.new(response.status, response.body)
  end
end

#sslObject



30
31
32
# File 'lib/getty/client.rb', line 30

def ssl
  @ssl
end