Module: Blockspring
- Defined in:
- lib/blockspring.rb,
lib/blockspring/version.rb
Defined Under Namespace
Classes: Request, Response
Constant Summary
collapse
- VERSION =
"0.1.2"
Class Method Summary
collapse
Class Method Details
.define(block) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/blockspring.rb', line 167
def self.define(block)
@response = Response.new
if(!STDIN.tty?)
@request = self.parse($stdin.read, false)
else
@request = Request.new
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
|
.parse(input_params, json_parsed = true) ⇒ 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
32
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
|
# File 'lib/blockspring.rb', line 8
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")
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))
= params[var_name]
if(.is_a?(Hash))
@request.(stringify_keys())
else
@request.()
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
return @request
end
|
.run(block, data = {}, options = {}) ⇒ Object
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 80
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 = {}) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/blockspring.rb', line 120
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.
end
rescue
return results
end
return self.parse(parsed_results, true)
end
|