Class: Panda::Connection

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

Constant Summary collapse

API_PORT =
80
US_API_HOST =
"api.pandastream.com"
EU_API_HOST =
"api.eu.pandastream.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_params = {}, options = {}) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(auth_params={}, options={})
  @raise_error = false
  @api_version = 2
  @format = "hash"

  if auth_params.class == String
    self.format = options[:format] || options["format"]
    init_from_uri(auth_params)
  else
    self.format = auth_params[:format] || auth_params["format"]
    init_from_hash(auth_params)
  end
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



3
4
5
# File 'lib/panda/connection.rb', line 3

def access_key
  @access_key
end

#api_hostObject

Returns the value of attribute api_host.



3
4
5
# File 'lib/panda/connection.rb', line 3

def api_host
  @api_host
end

#api_portObject

Returns the value of attribute api_port.



3
4
5
# File 'lib/panda/connection.rb', line 3

def api_port
  @api_port
end

#api_versionObject

Returns the value of attribute api_version.



3
4
5
# File 'lib/panda/connection.rb', line 3

def api_version
  @api_version
end

#cloud_idObject

Returns the value of attribute cloud_id.



3
4
5
# File 'lib/panda/connection.rb', line 3

def cloud_id
  @cloud_id
end

#formatObject

Returns the value of attribute format.



3
4
5
# File 'lib/panda/connection.rb', line 3

def format
  @format
end

#secret_keyObject

Returns the value of attribute secret_key.



3
4
5
# File 'lib/panda/connection.rb', line 3

def secret_key
  @secret_key
end

Instance Method Details

#api_urlObject



107
108
109
# File 'lib/panda/connection.rb', line 107

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

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



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

def delete(request_uri, params={})
  rescue_restclient_exception do
    connection.delete do |req|
      req.url File.join(connection.path_prefix, request_uri)
      req.body = signed_params("DELETE", request_uri, params)
    end.body
  end
end

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

Authenticated requests



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

def get(request_uri, params={})
  rescue_restclient_exception do
    query = signed_params("GET", request_uri, params)
    connection.get do |req|
      req.url File.join(connection.path_prefix, request_uri), query
    end.body
  end
end

#heroku=(url) ⇒ Object

Setup connection for Heroku



35
36
37
# File 'lib/panda/connection.rb', line 35

def heroku=(url)
  init_from_uri(url)
end

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



64
65
66
67
68
69
70
71
# File 'lib/panda/connection.rb', line 64

def post(request_uri, params={})
  rescue_restclient_exception do
    connection.post do |req|
      req.url File.join(connection.path_prefix, request_uri)
      req.body = signed_params("POST", request_uri, params)
    end.body
  end
end

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



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

def put(request_uri, params={})
  rescue_restclient_exception do
    connection.put do |req|
      req.url File.join(connection.path_prefix, request_uri)
      req.body = signed_params("PUT", request_uri, params)
    end.body
  end
end

#raise_error=(bool) ⇒ Object

Raise exception on non JSON parsable response if set



40
41
42
# File 'lib/panda/connection.rb', line 40

def raise_error=(bool)
  @raise_error = bool
end

#region=(region) ⇒ Object

Set the correct api_host for US/EU



24
25
26
27
28
29
30
31
32
# File 'lib/panda/connection.rb', line 24

def region=(region)
  if(region.to_s == "us")
    self.api_host = US_API_HOST
  elsif(region.to_s == "eu")
    self.api_host = EU_API_HOST
  else
    raise "Region Unknown"
  end
end

#setup_bucket(params = {}) ⇒ Object

Shortcut to setup your bucket



112
113
114
115
116
117
118
119
120
# File 'lib/panda/connection.rb', line 112

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

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

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



96
97
98
99
100
101
102
103
104
105
# File 'lib/panda/connection.rb', line 96

def signed_params(verb, request_uri, params = {}, timestamp_str = nil)
  auth_params = stringify_keys(params)
  auth_params['cloud_id']   = @cloud_id
  auth_params['access_key'] = @access_key
  auth_params['timestamp']  = timestamp_str || Time.now.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



92
93
94
# File 'lib/panda/connection.rb', line 92

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

#to_hashObject



122
123
124
125
126
127
128
# File 'lib/panda/connection.rb', line 122

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