module Blockspring
Constants
- VERSION
Public Class Methods
define(block)
click to toggle source
# File lib/blockspring.rb, line 169 def self.define(block) response = Response.new #stdin parsing if(!STDIN.tty?) request = self.parse($stdin.read, false) else request = Request.new end #args parsing if (ARGV.length > 0) argv = {} for arg in ARGV found_match = /([^=]*)\=(.*)/.match(arg) if found_match found_match = found_match.captures if found_match[0][0..1] == "--" argv[ found_match[0][2..-1] ] = found_match[1] else argv[ found_match[0] ] = found_match[1] end end end else argv = {} end for key in argv.keys request.params[key] = argv[key] end block.call(request, response) end
parse(input_params, json_parsed = true)
click to toggle source
# File lib/blockspring.rb, line 9 def self.parse(input_params, json_parsed = true) request = Request.new if json_parsed == true params = input_params else begin params = JSON.parse(input_params) rescue raise "You didn't pass valid json inputs." end end if !(params.is_a?(Hash)) raise "Can't parse keys/values from your json inputs." end if !(params.has_key?("_blockspring_spec") && params["_blockspring_spec"]) request.instance_variable_set("@params", params) else for var_name in params.keys if (var_name == "_blockspring_spec") # pass elsif ((var_name == "_errors") && params[var_name].is_a?(Array)) for error in params[var_name] if (error.is_a?(Hash)) && (error.has_key?("title")) request.addError(error) end end elsif ((var_name == "_headers") && params[var_name].is_a?(Hash)) headers = params[var_name] if(headers.is_a?(Hash)) request.addHeaders(stringify_keys(headers)) else request.addHeaders(headers) end elsif ( params[var_name].is_a?(Hash) and params[var_name].has_key?("filename") and params[var_name]["filename"] and # either data or url must exist and not be empty ( (params[var_name].has_key?("data") and params[var_name]["data"]) or (params[var_name].has_key?("url") and params[var_name]["url"])) ) suffix = "-%s" % params[var_name]["filename"] tmp_file = Tempfile.new(["",suffix]) if (params[var_name].has_key?("data")) begin tmp_file.write(Base64.decode64(params[var_name]["data"])) request.params[var_name] = tmp_file.path rescue request.params[var_name] = params[var_name] end else begin tmp_file.write(RestClient.get(params[var_name]["url"])) request.params[var_name] = tmp_file.path rescue request.params[var_name] = params[var_name] end end @handles << tmp_file tmp_file.close else request.params[var_name] = params[var_name] end end end return request end
run(block, data = {}, options = {})
click to toggle source
# File lib/blockspring.rb, line 82 def self.run(block, data = {}, options = {}) if (options.is_a?(String)) options = { "api_key" => options, "cache" => false, "expiry" => 0 } end unless (options.has_key?("api_key")) options["api_key"] = nil end if !(data.is_a?(Hash)) raise "your data needs to be a dictionary." end data = data.to_json api_key = options["api_key"] || ENV['BLOCKSPRING_API_KEY'] || "" cache = options.has_key?("cache") ? options["cache"] : false expiry = options.has_key?("expiry") ? options["expiry"] : nil blockspring_url = ENV['BLOCKSPRING_URL'] || 'https://sender.blockspring.com' block = block.split("/")[-1] begin response = RestClient.post "#{blockspring_url}/api_v2/blocks/#{block}?api_key=#{api_key}&cache=#{cache}&expiry=#{expiry}", data, :content_type => :json rescue => e response = e.response end results = response.body begin return JSON.parse(results) rescue return results end end
runParsed(block, data = {}, options = {})
click to toggle source
# File lib/blockspring.rb, line 122 def self.runParsed(block, data = {}, options = {}) if (options.is_a?(String)) options = { "api_key" => options } end unless (options.has_key?("api_key")) options["api_key"] = nil end if !(data.is_a?(Hash)) raise "your data needs to be a dictionary." end data = data.to_json api_key = options["api_key"] || ENV['BLOCKSPRING_API_KEY'] || "" cache = options.has_key?("cache") ? options["cache"] : false expiry = options.has_key?("expiry") ? options["expiry"] : nil blockspring_url = ENV['BLOCKSPRING_URL'] || 'https://sender.blockspring.com' block = block.split("/")[-1] begin response = RestClient.post "#{blockspring_url}/api_v2/blocks/#{block}?api_key=#{api_key}&cache=#{cache}&expiry=#{expiry}", data, :content_type => :json rescue => e response = e.response end results = response.body begin parsed_results = JSON.parse(results) if (!parsed_results.is_a?(Hash)) return parsed_results else parsed_results["_headers"] = response.headers end rescue return results end return self.parse(parsed_results, true) end
Private Class Methods
stringify_keys(hash)
click to toggle source
# File lib/blockspring.rb, line 285 def self.stringify_keys(hash) transform_hash(hash) {|hash, key, value| hash[key.to_s] = value } end
transform_hash(original, options={}, &block)
click to toggle source
# File lib/blockspring.rb, line 273 def self.transform_hash(original, options={}, &block) original.inject({}){|result, (key,value)| value = if (options[:deep] && Hash === value) transform_hash(value, options, &block) else value end block.call(result,key,value) result } end