Class: Panda::Connection
- Inherits:
-
Object
- Object
- Panda::Connection
- 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
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#api_host ⇒ Object
Returns the value of attribute api_host.
-
#api_port ⇒ Object
Returns the value of attribute api_port.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#cloud_id ⇒ Object
Returns the value of attribute cloud_id.
-
#format ⇒ Object
Returns the value of attribute format.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
Instance Method Summary collapse
- #api_url ⇒ Object
- #delete(request_uri, params = {}) ⇒ Object
-
#get(request_uri, params = {}) ⇒ Object
Authenticated requests.
-
#heroku=(url) ⇒ Object
Setup connection for Heroku.
-
#initialize(auth_params = {}, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #post(request_uri, params = {}) ⇒ Object
- #put(request_uri, params = {}) ⇒ Object
-
#raise_error=(bool) ⇒ Object
Raise exception on non JSON parsable response if set.
-
#region=(region) ⇒ Object
Set the correct api_host for US/EU.
-
#setup_bucket(params = {}) ⇒ Object
Shortcut to setup your bucket.
- #signed_params(verb, request_uri, params = {}, timestamp_str = nil) ⇒ Object
-
#signed_query(*args) ⇒ Object
Signing methods.
- #to_hash ⇒ Object
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={}, ={}) @raise_error = false @api_version = 2 @format = "hash" if auth_params.class == String self.format = [:format] || ["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_key ⇒ Object
Returns the value of attribute access_key.
3 4 5 |
# File 'lib/panda/connection.rb', line 3 def access_key @access_key end |
#api_host ⇒ Object
Returns the value of attribute api_host.
3 4 5 |
# File 'lib/panda/connection.rb', line 3 def api_host @api_host end |
#api_port ⇒ Object
Returns the value of attribute api_port.
3 4 5 |
# File 'lib/panda/connection.rb', line 3 def api_port @api_port end |
#api_version ⇒ Object
Returns the value of attribute api_version.
3 4 5 |
# File 'lib/panda/connection.rb', line 3 def api_version @api_version end |
#cloud_id ⇒ Object
Returns the value of attribute cloud_id.
3 4 5 |
# File 'lib/panda/connection.rb', line 3 def cloud_id @cloud_id end |
#format ⇒ Object
Returns the value of attribute format.
3 4 5 |
# File 'lib/panda/connection.rb', line 3 def format @format end |
#secret_key ⇒ Object
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_url ⇒ Object
100 101 102 |
# File 'lib/panda/connection.rb', line 100 def api_url "http://#{@api_host}:#{@api_port}/#{@prefix}" end |
#delete(request_uri, params = {}) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/panda/connection.rb', line 76 def delete(request_uri, params={}) @connection = RestClient::Resource.new(api_url) rescue_restclient_exception do query = signed_query("DELETE", request_uri, params) body_of @connection[request_uri + '?' + query].delete end end |
#get(request_uri, params = {}) ⇒ Object
Authenticated requests
54 55 56 57 58 59 60 |
# File 'lib/panda/connection.rb', line 54 def get(request_uri, params={}) @connection = RestClient::Resource.new(api_url) rescue_restclient_exception do query = signed_query("GET", request_uri, params) body_of @connection[request_uri + '?' + query].get 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
62 63 64 65 66 67 |
# File 'lib/panda/connection.rb', line 62 def post(request_uri, params={}) @connection = RestClient::Resource.new(api_url) rescue_restclient_exception do body_of @connection[request_uri].post(signed_params("POST", request_uri, params)) end end |
#put(request_uri, params = {}) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/panda/connection.rb', line 69 def put(request_uri, params={}) @connection = RestClient::Resource.new(api_url) rescue_restclient_exception do body_of @connection[request_uri].put(signed_params("PUT", request_uri, params)) 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
105 106 107 108 109 110 111 112 113 |
# File 'lib/panda/connection.rb', line 105 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
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/panda/connection.rb', line 89 def signed_params(verb, request_uri, params = {}, = nil) auth_params = stringify_keys(params) auth_params['cloud_id'] = @cloud_id auth_params['access_key'] = @access_key auth_params['timestamp'] = || 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
85 86 87 |
# File 'lib/panda/connection.rb', line 85 def signed_query(*args) ApiAuthentication.hash_to_query(signed_params(*args)) end |
#to_hash ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/panda/connection.rb', line 115 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 |