Class: RightScriptAttachmentInternal

Inherits:
Object
  • Object
show all
Extended by:
RightScale::Api::BaseExtend, RightScale::Api::InternalExtend
Includes:
RightScale::Api::Base, RightScale::Api::Internal
Defined in:
lib/rest_connection/rightscale/right_script_attachment_internal.rb

Overview

API 0.1

Instance Attribute Summary

Attributes included from RightScale::Api::Base

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RightScale::Api::BaseExtend

[], create, deny_methods, filters, find, find_all, find_by, find_by_cloud_id, find_by_id, find_by_nickname, find_by_nickname_speed, find_with_filter

Methods included from RightScale::Api::BaseConnection

#connection

Methods included from RightScale::Api::InternalConnection

#connection

Methods included from RightScale::Api::Base

#[], #[]=, #destroy, #initialize, #method_missing, #rs_id, #save

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RightScale::Api::Base

Class Method Details

.get_s3_upload_params(right_script_href) ⇒ Object



54
55
56
57
58
59
# File 'lib/rest_connection/rightscale/right_script_attachment_internal.rb', line 54

def self.get_s3_upload_params(right_script_href)
  url = self.resource_plural_name + "/get_s3_upload_params"
  params = {"right_script_href" => right_script_href}
  params = {self.resource_singular_name => params}
  connection.get(url, params)
end

.resource_plural_nameObject



46
47
48
# File 'lib/rest_connection/rightscale/right_script_attachment_internal.rb', line 46

def self.resource_plural_name
  "right_script_attachments"
end

.resource_singular_nameObject



50
51
52
# File 'lib/rest_connection/rightscale/right_script_attachment_internal.rb', line 50

def self.resource_singular_name
  "right_script_attachment"
end

Instance Method Details

#downloadObject

def self.upload(filepath, right_script_href)

  hsh = get_s3_upload_params(right_script_href)
  params = {}
  hsh.keys.each { |k| params[k.gsub(/-/,"_").to_sym] = hsh[k] }
  params[:file] = File.new(filepath, 'rb')
  req = RestClient::Request.new({
    :method => :post,
    :url => hsh["url"],
    :payload => params,
    :multipart => true,
  })
  s = req.payload.to_s
  splitter = s.split("\r\n").first
  a = s.split(/#{splitter}-?-?\r\n/)
  a.push(a.delete(a.detect { |n| n =~ %r{name="file";} }))
  new_payload = a.join(splitter + "\r\n") + splitter + "--\r\n"

  uri = URI.parse(hsh["url"])
  net_http = Net::HTTP::Post.new(uri.request_uri)
  req.transmit(uri, net_http, new_payload)
  # TODO: Precondition Failing

  callback_uri = URI.parse(hsh["success_action_redirect"])
  connection.get(callback_uri.request_uri)
end


89
90
91
92
# File 'lib/rest_connection/rightscale/right_script_attachment_internal.rb', line 89

def download
  self.reload unless @params["authenticated_s3_url"]
  RestClient.get(@params["authenticated_s3_url"])
end

#download_to_file(path = Dir.pwd) ⇒ Object



94
95
96
97
# File 'lib/rest_connection/rightscale/right_script_attachment_internal.rb', line 94

def download_to_file(path=Dir.pwd)
  data = self.download
  File.open(File.join(path, @params["filename"]), 'w') { |f| f.write(data) }
end

#reloadObject



99
100
101
102
# File 'lib/rest_connection/rightscale/right_script_attachment_internal.rb', line 99

def reload
  uri = URI.parse(self.href || "#{resource_plural_name}/#{@params["id"]}")
  @params ? @params.merge!(connection.get(uri.path)) : @params = connection.get(uri.path)
end

#resource_plural_nameObject



38
39
40
# File 'lib/rest_connection/rightscale/right_script_attachment_internal.rb', line 38

def resource_plural_name
  "right_script_attachments"
end

#resource_singular_nameObject



42
43
44
# File 'lib/rest_connection/rightscale/right_script_attachment_internal.rb', line 42

def resource_singular_name
  "right_script_attachment"
end