Module: Coconut
- Defined in:
- lib/coconutrb.rb
Defined Under Namespace
Constant Summary collapse
- COCONUT_URL =
ENV["COCONUT_URL"] || "https://api.coconut.co"
- USER_AGENT =
"Coconut/2.4.0 (Ruby)"- API_KEY =
ENV["COCONUT_API_KEY"]
Class Method Summary collapse
- .config(options = {}) ⇒ Object
- .get(path, api_key = nil) ⇒ Object
- .hash_params_to_string(params) ⇒ Object
- .submit(config_content, api_key = nil) ⇒ Object
- .submit!(config_content, opts = {}) ⇒ Object
Class Method Details
.config(options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/coconutrb.rb', line 57 def self.config(={}) if conf_file = [:conf] raise Error, "Config file `#{conf_file}' not found" if ! File.exists?(conf_file) conf = File.read(conf_file).strip.split("\n") else conf = [] end if vars = [:vars] vars.each do |name,value| conf << "var #{name} = #{value}" end end if source = [:source] conf << "set source = #{source}" end if webhook = [:webhook] if webhook.is_a?(Hash) webhook = hash_params_to_string(webhook) end conf << "set webhook = #{webhook}" end if api_version = [:api_version] conf << "set api_version = #{api_version}" end if outputs = [:outputs] outputs.each do |format, cdn| if cdn.is_a?(Hash) cdn = hash_params_to_string(cdn) end conf << "-> #{format} = #{cdn}" end end new_conf = [] new_conf.concat conf.select{|l| l.start_with?("var")}.sort new_conf << "" new_conf.concat conf.select{|l| l.start_with?("set")}.sort new_conf << "" new_conf.concat conf.select{|l| l.start_with?("->")}.sort return new_conf.join("\n") end |
.get(path, api_key = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/coconutrb.rb', line 38 def self.get(path, api_key=nil) api_key ||= API_KEY uri = URI("#{COCONUT_URL}#{path}") headers = {"User-Agent" => USER_AGENT, "Content-Type" => "text/plain", "Accept" => "application/json"} req = Net::HTTP::Get.new(uri.path, headers) req.basic_auth api_key, '' response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme.include?("https")) do |http| http.request(req) end if response.code.to_i != 200 return nil else return MultiJson.decode(response.body) end end |
.hash_params_to_string(params) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/coconutrb.rb', line 108 def self.hash_params_to_string(params) params.map{|k,v| if k.to_s == "url" "#{v}" else "#{k}=#{v}" end }.join(", ") end |
.submit(config_content, api_key = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/coconutrb.rb', line 13 def self.submit(config_content, api_key=nil) api_key ||= API_KEY uri = URI("#{COCONUT_URL}/v1/job") headers = {"User-Agent" => USER_AGENT, "Content-Type" => "text/plain", "Accept" => "application/json"} req = Net::HTTP::Post.new(uri.path, headers) req.basic_auth api_key, '' req.body = config_content response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme.include?("https")) do |http| http.request(req) end return MultiJson.decode(response.body) end |
.submit!(config_content, opts = {}) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/coconutrb.rb', line 29 def self.submit!(config_content, opts={}) result = submit(config_content, opts) if result["status"] == "error" raise Error, "#{result["message"]} (#{result["error_code"]})" else return result end end |