Module: Blockspring
- Defined in:
- lib/blockspring.rb,
lib/blockspring/version.rb
Defined Under Namespace
Classes: Request, Response
Constant Summary
collapse
- VERSION =
"0.0.7"
Class Method Summary
collapse
Class Method Details
.define(block) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/blockspring.rb', line 33
def self.define(block)
@request = Request.new
@response = Response.new
if(!STDIN.tty?)
begin
params = JSON.parse($stdin.read)
rescue
raise "You didn't pass valid json inputs."
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")
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 (
params[var_name].is_a?(Hash) and
params[var_name].has_key?("filename") and
params[var_name]["filename"] and
(
(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
tmp_file.close
else
@request.params[var_name] = params[var_name]
end
end
end
end
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
|
.run(block, data = {}, api_key = nil) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/blockspring.rb', line 8
def self.run(block, data = {}, api_key = nil )
if !(data.is_a?(Hash))
raise "your data needs to be a dictionary."
end
data = data.to_json
api_key = api_key || ENV['BLOCKSPRING_API_KEY'] || ""
blockspring_url = ENV['BLOCKSPRING_URL'] || 'https://sender.blockspring.com'
block = block.split("/")[-1]
begin
response = RestClient.post "https://sender.blockspring.com/api_v2/blocks/#{block}?api_key=#{api_key}", data, :content_type => :json
rescue => e
response = e.response
end
begin
body = JSON.parse(response.body)
rescue
body = response.body
end
return body
end
|