Class: Cloudkey::API

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudkey/api.rb,
lib/cloudkey/security_level.rb

Defined Under Namespace

Modules: SecurityLevel

Constant Summary collapse

END_POINT =

New API versions that break compatibility would probably change the endpoint

"/api"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id, key, options = {}) ⇒ API

Returns a new instance of API.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cloudkey/api.rb', line 16

def initialize user_id, key, options={}
  raise "Can't connect without an user_id" unless user_id
  raise "Can't connect without an key" unless key
  
  @user_id, @key = user_id, key
  
  @base_url = options[:base_url] || 'http://api.dmcloud.net'
  @proxy = options[:proxy]
  @act_as_user = options[:act_as_user]
  
  @target = @base_url + END_POINT
end

Instance Attribute Details

#act_as_userObject

Returns the value of attribute act_as_user.



10
11
12
# File 'lib/cloudkey/api.rb', line 10

def act_as_user
  @act_as_user
end

#base_urlObject

Returns the value of attribute base_url.



10
11
12
# File 'lib/cloudkey/api.rb', line 10

def base_url
  @base_url
end

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/cloudkey/api.rb', line 10

def key
  @key
end

#proxyObject

Returns the value of attribute proxy.



10
11
12
# File 'lib/cloudkey/api.rb', line 10

def proxy
  @proxy
end

#targetObject (readonly)

Returns the value of attribute target.



11
12
13
# File 'lib/cloudkey/api.rb', line 11

def target
  @target
end

#user_idObject

Returns the value of attribute user_id.



10
11
12
# File 'lib/cloudkey/api.rb', line 10

def user_id
  @user_id
end

Class Method Details

.normalize(payload) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/cloudkey/api.rb', line 75

def self.normalize payload
  case payload
  when Array
    payload.collect { |element| normalize element }.join('')
  when Hash
    payload.to_a.sort_by {|a,b| a.to_s }.collect {|array| array.first.to_s + normalize(array.last)}.join('')
  else
    payload.to_s
  end
end

.sign(message, secret) ⇒ Object



45
46
47
# File 'lib/cloudkey/api.rb', line 45

def self.sign message, secret
  Digest::MD5.hexdigest(message + secret)
end

.sign_url(url, secret, security_policy = SecurityPolicy.new) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cloudkey/api.rb', line 49

def self.sign_url url, secret, security_policy = SecurityPolicy.new
  (url, query) = url.split("?")
  
  expires = security_policy.expires.to_s
  
  rand   = (('a'..'z').to_a + (1..9).to_a).shuffle[0..7].join('')
  food   = security_policy.level.to_s
  food  << url  << expires.to_s
  food  << rand << secret << security_policy.private_parameters.join('')
  food  << security_policy.encoded_public_parameters if security_policy.encoded_public_parameters
  
  digest = Digest::MD5.hexdigest(food) 
  
  result = url
  result << "?"
  result << query + '&' if query
  result << "auth="
  result << expires.to_s  << "-"
  result << security_policy.level.to_s << "-"
  result << rand << "-"
  result << digest      
  result << "-#{security_policy.encoded_public_parameters}" if security_policy.encoded_public_parameters
  
  result
end

Instance Method Details

#fileObject



41
42
43
# File 'lib/cloudkey/api.rb', line 41

def file
  File.new self
end

#mediaObject



37
38
39
# File 'lib/cloudkey/api.rb', line 37

def media
  Media.new self 
end

#user_infosObject



29
30
31
32
33
34
35
# File 'lib/cloudkey/api.rb', line 29

def user_infos
  if act_as_user
    "#{user_id}/#{act_as_user}"
  else
    user_id
  end
end