Class: FotoliaRest::Client

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

Constant Summary collapse

BASE_URL =
'api.fotolia.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, username, password) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/fotolia_rest/client.rb', line 10

def initialize(api_key, username, password)
  @api_key = api_key
  @username = username
  @password = password
  @session_id = nil
  @logger = defined?(Rails) ? Rails.logger : nil
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/fotolia_rest/client.rb', line 8

def api_key
  @api_key
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/fotolia_rest/client.rb', line 8

def password
  @password
end

#session_idObject (readonly)

Returns the value of attribute session_id.



8
9
10
# File 'lib/fotolia_rest/client.rb', line 8

def session_id
  @session_id
end

#usernameObject (readonly)

Returns the value of attribute username.



8
9
10
# File 'lib/fotolia_rest/client.rb', line 8

def username
  @username
end

Instance Method Details

#download_media(url) ⇒ Object



63
64
65
# File 'lib/fotolia_rest/client.rb', line 63

def download_media(url)
  open(url, 'rb', :http_basic_authentication => [ @api_key, @session_id ])
end

#execute(method, function, args = {}, http_method = :get) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fotolia_rest/client.rb', line 34

def execute(method, function, args={}, http_method=:get)
  begin
    uri = URI.parse(compose_uri(method, function))
    http = Net::HTTP.new(uri.host, uri.port)
    http.open_timeout = http.read_timeout = 5 # seconds
    if http_method == :get
      uri.query = URI.encode_www_form(fotolia_parameters(args))
      request = Net::HTTP::Get.new(uri.request_uri)
      request.basic_auth api_key, @session_id
      request['session_id'] = @session_id if logged_in?
      response = http.request(request)
    else
      request = Net::HTTP::Post.new(uri.request_uri)
      request.basic_auth api_key, @session_id
      request.set_form_data(args)
      request['session_id'] = @session_id if logged_in?
      response = http.request(request)
    end
    Result.parse(response)
  rescue Timeout::Error => e
    log(:error, "Timeout error with #{method} #{args.inspect}")
    raise FotoliaRest::FotoliaError.new("Fotolia Communication Error: timeout")
  rescue => e
    log(:error, "Fotolia client error with #{method} #{args.inspect} #{e.message}")
    e.backtrace.each{|line| log(:debug, line)}
    raise FotoliaRest::FotoliaError.new("Fotolia Communication Error: #{e.message}")
  end
end

#log(level, message) ⇒ Object



18
19
20
21
22
# File 'lib/fotolia_rest/client.rb', line 18

def log(level, message)
  if @logger
    @logger.send(level.to_sym, message)
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)


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

def logged_in?
  @session_id && @session_id != ''
end

#loginObject



24
25
26
27
28
# File 'lib/fotolia_rest/client.rb', line 24

def 
  result = execute('user', 'loginUser', {:login => username, :pass => password}, :post)
  @session_id = result.response['session_token'] if result.success?
  return result
end