require 'railsonfire/helpers/key' require 'railsonfire/helpers/constants' require 'httparty' require 'json' require 'yaml' require 'active_support/core_ext/hash/keys' module Railsonfire include Railsonfire::Helper def self.config @config ||= File.exists?(RAILSONFIRE_FILE) ? stringify(YAML.load(File.read(RAILSONFIRE_FILE))) : {} end def self.login token, github_token @user = Railsonfire::User.new token, github_token end def self.user @user end def self.project id = Railsonfire.config["id"] if id @project ||= Project.new user.railsonfire_token, id else @project ||= Project.new user.railsonfire_token end end class Project include Helper include Helper::Keys attr_accessor :name, :owner, :url attr_reader :ssh_public_key, :ssh_private_key, :uuid def initialize token, id = nil @token = token load_project_data token, id if id end def create send_signal :starting_create ssh_key = create_ssh_key @ssh_public_key = ssh_key.ssh_public_key @ssh_private_key = ssh_key.rsa_private_key keys = create_keys ssh_key body = {:name => name, :owner => owner, :repo_url => url}.merge(keys) response = HTTParty.post("#{Railsonfire::DOMAIN}/projects?token=#@token", :headers => {"Content-Type" => "application/json"}, :body => body.to_json) case response.code when 201 @uuid = response.body Railsonfire.config["id"] = @uuid send_signal :created true when 404 shell.say "Could not authenticate with Railsonfire. Please logout and back in via the railsonfire gem." notify("create-authenticate", "Could not authenticate with Railsonfire") false else shell.say "An Error occured on the Railsonfire Servers and we were notified. Please try again or contact us at flo@railsonfire.com" notify("create-error", "Error occured when creating a project") false end end private def load_project_data token, id response = HTTParty.get("#{Railsonfire::DOMAIN}/projects/#{id}/project_data?access_token=#{token}") if response.code != 200 shell.say "An Error occured on the Railsonfire server" notify("load_project", "Project data couldn't be loaded", {:token => token, :id => id}) return nil else data = JSON.parse(response.body) @name = data["name"] @owner = data["owner"] @ssh_public_key = data["public_key"] end end end class User attr_reader :github_token, :railsonfire_token def initialize railsonfire_token, github_token @railsonfire_token = railsonfire_token @github_token = github_token end def config JSON.parse(HTTParty.get(Railsonfire::DOMAIN + "/projects/#{Railsonfire.config["id"]}/get_config?railsonfire_token=#{@railsonfire_token}").body) end def set_config key, value HTTParty.post(Railsonfire::DOMAIN + "/projects/#{Railsonfire.config["id"]}/add_config?railsonfire_token=#{@railsonfire_token}", :headers => {"Content-Type" => "application/json"}, :body => {"config" => {key => value}}.to_json) end def remove_config key HTTParty.delete(Railsonfire::DOMAIN + "/projects/#{Railsonfire.config["id"]}/remove_config?railsonfire_token=#{@railsonfire_token}&key=#{key}") end end private def self.stringify hash hash.tap do |h| h.stringify_keys! h.each do |key, value| stringify h[key] if h[key].is_a?(Hash) end end end end