lib/client/client.rb in factor-0.0.6 vs lib/client/client.rb in factor-0.0.7

- old
+ new

@@ -1,15 +1,16 @@ require 'rubygems' require 'rest_client' require 'zip' require 'zip/zipfilesystem' +require 'zip/zip' require 'open-uri' module Factor module Client class Client - HOST = "http://localhost:3000" + HOST = "http://factor.io" def register(email,password) end def login(email, password) @@ -28,19 +29,63 @@ end engine end def load_credentials(engine) - - credentials_definition = rest_get("credentials") - credentials = JSON.parse(credentials_definition["bag"]) + credentials = rest_get("credentials") engine.load_credentials(credentials) engine end + def set_credential(key,value) + # this is a PUT not POST because it is technically editing, not creating a new one + rest_put("credentials",{:key=>key,:value=>value}) + end + + def get_credential(key="") + rest_get("credentials",{:key=>key}) + end + + def remove_credential(key="") + rest_delete("credentials",{:key=>key}) + end + + + + + # workflows + def add_workflow(key,definition) + rest_post("workflows",{:name=>key,:definition=>definition}) + end + + # def get_workflow() + # rest_get("workflows") + # end + + def remove_workflow(name="") + rest_delete("workflows",{:name=>name}) + end + + + # channels + def add_channel(key,path) + file=zip(path) + rest_post("channels",{:name=>key,:zip=>file}) + end + + # def get_channel() + # rest_get("channels") + # end + + def remove_channel(name="") + rest_delete("channels",{:name=>name}) + end + + + def load_channels(engine) # get list of channels channels = rest_get("channels") @@ -106,12 +151,49 @@ zip_file.extract(f, f_path) unless File.exist?(f_path) end end FileUtils.rm(zip) if remove_after end + + def zip(directory) + path=directory.dup + path.sub!(%r[/$],'') + zip_path = File.join(path,File.basename(path))+'.zip' + FileUtils.rm zip_path, :force=>true + Zip::ZipFile.open(zip_path, 'w') do |zip| + Dir["#{path}/**/**"].reject{|f|f==zip_path}.each do |file| + zip.add(file.sub(path+'/',''),file) + end + end + File.open(zip_path,'r') + end - def rest_get(path) - JSON.parse(RestClient.get "#{HOST}/#{path}.json?auth_token=#{@token}") + def rest_get(path,params={}) + params["auth_token"]=@token + + param_string=params.map{|key,value| "#{key}=#{value}"}.join("&") + + JSON.parse(RestClient.get("#{HOST}/#{path}.json?#{param_string}")) + end + + def rest_put(path,params={}) + params["auth_token"]=@token + + JSON.parse(RestClient.put("#{HOST}/#{path}.json",params)) + end + + def rest_post(path,params={}) + #params["auth_token"]=@token + + JSON.parse(RestClient.post("#{HOST}/#{path}.json?auth_token=#{@token}",params)) + end + + def rest_delete(path,params={}) + params["auth_token"]=@token + + param_string=params.map{|key,value| "#{key}=#{value}"}.join("&") + + JSON.parse(RestClient.delete("#{HOST}/#{path}.json?#{param_string}")) end end end end \ No newline at end of file