Class: Giddy::Mediator

Inherits:
Object
  • Object
show all
Defined in:
lib/giddy/mediator.rb

Constant Summary collapse

ROOTPATH =
"https://connect.gettyimages.com/v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, token = nil, secure_token = nil) ⇒ Mediator

Returns a new instance of Mediator.



6
7
8
9
10
11
12
13
# File 'lib/giddy/mediator.rb', line 6

def initialize(username, password, token=nil, secure_token=nil)
  @username = username
  @password = password
  @token = token
  @secure_token = secure_token
  @system_id = nil
  @system_password = nil
end

Instance Attribute Details

#secure_tokenObject (readonly)

Returns the value of attribute secure_token.



4
5
6
# File 'lib/giddy/mediator.rb', line 4

def secure_token
  @secure_token
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/giddy/mediator.rb', line 4

def token
  @token
end

Instance Method Details

#create_sessionObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/giddy/mediator.rb', line 20

def create_session
  data = { 
    :SystemId => @system_id || Giddy.config.system_id,
    :SystemPassword => @system_password || Giddy.config.system_password,
    :UserName => @username,
    :UserPassword => @password
  }
  result = fetch "session", nil, nil, :CreateSession, data
  unless result["ResponseHeader"]["Status"] == "success"
    raise error_type(result), "Error authenticating: #{result["ResponseHeader"]}"
  end
  @token = result["CreateSessionResult"]["Token"]
  @secure_token = result["CreateSessionResult"]["SecureToken"]
end

#error_type(result) ⇒ Object



54
55
56
57
58
59
# File 'lib/giddy/mediator.rb', line 54

def error_type(result)
  statuses = result["ResponseHeader"]["StatusList"].map { |s| s["Code"] }
  return ImageNotFound if statuses.include? "ImageNotFound"
  return InvalidUsernameOrPassword if statuses.include? "InvalidUsernameOrPassword"
  InvalidRequest
end

#fetch(path, token, bodyname, name, data) ⇒ Object



61
62
63
64
65
66
# File 'lib/giddy/mediator.rb', line 61

def fetch(path, token, bodyname, name, data)
  body = { :RequestHeader => { :Token => token }, "#{bodyname || name}RequestBody" => data }
  headers = { 'Content-Type' => 'application/json' }
  url = "#{ROOTPATH}/#{path}/#{name}"
  HTTParty.post(url, :body => body.to_json, :headers => headers)
end

#gettyup(path, name, data, bodyname, secure) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/giddy/mediator.rb', line 35

def gettyup(path, name, data, bodyname, secure)
  token = secure ? @secure_token : @token
  if token.nil?
    create_session
    token = secure ? @secure_token : @token
  end

  result = fetch(path, token, bodyname, name, data)

  if reauth_needed?(result)
    create_session
    gettyup path, name, data, bodyname, secure
  elsif result["ResponseHeader"]["Status"] == "success"
    result["#{name}Result"]
  else
    raise error_type(result), "Error fetching #{name}: #{result["ResponseHeader"]}"
  end
end

#reauth_needed?(result) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'lib/giddy/mediator.rb', line 68

def reauth_needed?(result)
  return true if result.body == "<h1>Developer Inactive</h1>"
  reauth_codes = [ "AUTH-012", "AUTH-010" ]
  if result["ResponseHeader"]["StatusList"].length > 0
    reauth_codes.include? result["ResponseHeader"]["StatusList"].first["Code"]
  else
    false
  end
end

#set_system_credentials(system_id, system_password) ⇒ Object



15
16
17
18
# File 'lib/giddy/mediator.rb', line 15

def set_system_credentials(system_id, system_password)
  @system_id = system_id
  @system_password = system_password
end