Class: Panda::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/panda/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_params = {}) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/panda/connection.rb', line 10

def initialize(auth_params={}) 
  params = { :api_host => US_API_HOST, :api_port => API_PORT }.merge!(auth_params)
  @api_version = 2

  @cloud_id   = params["cloud_id"]    || params[:cloud_id]
  @access_key = params["access_key"]  || params[:access_key]
  @secret_key = params["secret_key"]  || params[:secret_key]
  @api_host   = params["api_host"]    || params[:api_host]
  @api_port   = params["api_port"]    || params[:api_port]
  @prefix     = params["prefix_url"]  || "v#{api_version}"
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



8
9
10
# File 'lib/panda/connection.rb', line 8

def access_key
  @access_key
end

#api_hostObject

Returns the value of attribute api_host.



8
9
10
# File 'lib/panda/connection.rb', line 8

def api_host
  @api_host
end

#api_portObject

Returns the value of attribute api_port.



8
9
10
# File 'lib/panda/connection.rb', line 8

def api_port
  @api_port
end

#api_versionObject

Returns the value of attribute api_version.



8
9
10
# File 'lib/panda/connection.rb', line 8

def api_version
  @api_version
end

#cloud_idObject

Returns the value of attribute cloud_id.



8
9
10
# File 'lib/panda/connection.rb', line 8

def cloud_id
  @cloud_id
end

#secret_keyObject

Returns the value of attribute secret_key.



8
9
10
# File 'lib/panda/connection.rb', line 8

def secret_key
  @secret_key
end

Instance Method Details

#api_schemeObject



67
68
69
# File 'lib/panda/connection.rb', line 67

def api_scheme
  api_port.to_i == 443 ? 'https' : 'http'
end

#api_urlObject



63
64
65
# File 'lib/panda/connection.rb', line 63

def api_url
  "#{api_scheme}://#{api_host}:#{api_port}/#{@prefix}"
end

#delete(request_uri, params = {}) ⇒ Object



42
43
44
45
# File 'lib/panda/connection.rb', line 42

def delete(request_uri, params={})
  sp = signed_params("DELETE", request_uri, params)
  http_client.delete(request_uri, sp)
end

#get(request_uri, params = {}) ⇒ Object

Authenticated requests



27
28
29
30
# File 'lib/panda/connection.rb', line 27

def get(request_uri, params={})
  sp = signed_params("GET", request_uri, params)
  http_client.get(request_uri, sp)
end

#http_clientObject



22
23
24
# File 'lib/panda/connection.rb', line 22

def http_client
  Panda::HttpClient::Faraday.new(api_url)
end

#post(request_uri, params = {}) ⇒ Object



32
33
34
35
# File 'lib/panda/connection.rb', line 32

def post(request_uri, params={})
  sp = signed_params("POST", request_uri, params)
  http_client.post(request_uri, sp)
end

#put(request_uri, params = {}) ⇒ Object



37
38
39
40
# File 'lib/panda/connection.rb', line 37

def put(request_uri, params={})
  sp = signed_params("PUT", request_uri, params)
  http_client.put(request_uri, sp)
end

#setup_bucket(params = {}) ⇒ Object

Shortcut to setup your bucket



72
73
74
75
76
77
78
79
80
# File 'lib/panda/connection.rb', line 72

def setup_bucket(params={})
  granting_params = { 
    :s3_videos_bucket => params[:bucket],
    :aws_access_key => params[:access_key],
    :aws_secret_key => params[:secret_key]
  }

  put("/clouds/#{@cloud_id}.json", granting_params)
end

#signed_params(verb, request_uri, params = {}, timestamp_str = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/panda/connection.rb', line 52

def signed_params(verb, request_uri, params = {}, timestamp_str = nil)
  auth_params = stringify_keys(params)
  auth_params['cloud_id']   = cloud_id unless request_uri =~ /^\/clouds/
  auth_params['access_key'] = access_key
  auth_params['timestamp']  = timestamp_str || Time.now.utc.iso8601(6)

  params_to_sign = auth_params.reject{|k,v| ['file'].include?(k.to_s)}
  auth_params['signature']  = ApiAuthentication.generate_signature(verb, request_uri, api_host, secret_key, params_to_sign)
  auth_params
end

#signed_query(*args) ⇒ Object

Signing methods



48
49
50
# File 'lib/panda/connection.rb', line 48

def signed_query(*args)
  ApiAuthentication.hash_to_query(signed_params(*args))
end

#to_hashObject



82
83
84
85
86
87
88
# File 'lib/panda/connection.rb', line 82

def to_hash
  hash = {}
  [:api_host, :api_port, :access_key, :secret_key, :api_version, :cloud_id].each do |a|
    hash[a] = send(a)
  end
  hash
end