Class: Cloudkey::API
- Inherits:
-
Object
- Object
- Cloudkey::API
- 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
-
#act_as_user ⇒ Object
Returns the value of attribute act_as_user.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#key ⇒ Object
Returns the value of attribute key.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Class Method Summary collapse
- .normalize(payload) ⇒ Object
- .sign(message, secret) ⇒ Object
- .sign_url(url, secret, security_policy = SecurityPolicy.new) ⇒ Object
Instance Method Summary collapse
- #file ⇒ Object
-
#initialize(user_id, key, options = {}) ⇒ API
constructor
A new instance of API.
- #media ⇒ Object
- #user_infos ⇒ Object
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, ={} 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 = [:base_url] || 'http://api.dmcloud.net' @proxy = [:proxy] @act_as_user = [:act_as_user] @target = @base_url + END_POINT end |
Instance Attribute Details
#act_as_user ⇒ Object
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_url ⇒ Object
Returns the value of attribute base_url.
10 11 12 |
# File 'lib/cloudkey/api.rb', line 10 def base_url @base_url end |
#key ⇒ Object
Returns the value of attribute key.
10 11 12 |
# File 'lib/cloudkey/api.rb', line 10 def key @key end |
#proxy ⇒ Object
Returns the value of attribute proxy.
10 11 12 |
# File 'lib/cloudkey/api.rb', line 10 def proxy @proxy end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
11 12 13 |
# File 'lib/cloudkey/api.rb', line 11 def target @target end |
#user_id ⇒ Object
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 , secret Digest::MD5.hexdigest( + 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
#user_infos ⇒ Object
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 |