blockspring 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b8ab9e420b558bd558d99170c309107e2ca61d4
4
- data.tar.gz: 4dd5631ac3ff7b68a93821a1ede92a424bc76d9a
3
+ metadata.gz: 6f9300da8c89dc588c7ba063837a3526193d0f80
4
+ data.tar.gz: 5d1cd62f90d2e67963b9c9f3485f85b773a1346d
5
5
  SHA512:
6
- metadata.gz: 6d79e1411ff725877b7837a232060dbf45359af428e837776455d8577eeadd36253e70900a613919171287c36ed271c9c1126cae488c361ff6f1153fb9163933
7
- data.tar.gz: 38294d2c8ffc1bd2d24d83f955c8f7bb050291010ce848085648efb7366c3910217a45f09da0012230159b3e65cc62c8c41171ab37e3b9f7e43d5193578e5863
6
+ metadata.gz: bc774e071b1e48cd62f073a3a8e99eedc8d9c7bb42c42ec5a9cd4fd17f7ad12d184a12a29f82434cdec85024d33c66f1de2ac38e8f142ecc9a7e8f73e977e2a5
7
+ data.tar.gz: 9ad9b568f998010bbe6f18964a65e1d1821a8f0eb18385f8d7e14da51b3474e5a4a3fcc60f8b84140c4e16b72efcf28a7c080568195896934be79afd14142cb4
@@ -5,6 +5,78 @@ require 'mime/types'
5
5
  require "tempfile"
6
6
 
7
7
  module Blockspring
8
+ def self.parse(input_params, json_parsed = true)
9
+ @request = Request.new
10
+
11
+ if json_parsed == true
12
+ params = input_params
13
+ else
14
+ begin
15
+ params = JSON.parse(input_params)
16
+ rescue
17
+ raise "You didn't pass valid json inputs."
18
+ end
19
+ end
20
+
21
+ if !(params.is_a?(Hash))
22
+ raise "Can't parse keys/values from your json inputs."
23
+ end
24
+
25
+ if !(params.has_key?("_blockspring_spec") && params["_blockspring_spec"])
26
+ @request.instance_variable_set("@params", params)
27
+ else
28
+ for var_name in params.keys
29
+ if (var_name == "_blockspring_spec")
30
+ # pass
31
+ elsif ((var_name == "_errors") && params[var_name].is_a?(Array))
32
+ for error in params[var_name]
33
+ if (error.is_a?(Hash)) && (error.has_key?("title"))
34
+ @request.addError(error)
35
+ end
36
+ end
37
+ elsif ((var_name == "_headers") && params[var_name].is_a?(Hash))
38
+ headers = params[var_name]
39
+ if(headers.is_a?(Hash))
40
+ @request.addHeaders(stringify_keys(headers))
41
+ else
42
+ @request.addHeaders(headers)
43
+ end
44
+ elsif (
45
+ params[var_name].is_a?(Hash) and
46
+ params[var_name].has_key?("filename") and
47
+ params[var_name]["filename"] and
48
+ # either data or url must exist and not be empty
49
+ (
50
+ (params[var_name].has_key?("data") and params[var_name]["data"]) or
51
+ (params[var_name].has_key?("url") and params[var_name]["url"]))
52
+ )
53
+ suffix = "-%s" % params[var_name]["filename"]
54
+ tmp_file = Tempfile.new(["",suffix])
55
+ if (params[var_name].has_key?("data"))
56
+ begin
57
+ tmp_file.write(Base64.decode64(params[var_name]["data"]))
58
+ @request.params[var_name] = tmp_file.path
59
+ rescue
60
+ @request.params[var_name] = params[var_name]
61
+ end
62
+ else
63
+ begin
64
+ tmp_file.write(RestClient.get(params[var_name]["url"]))
65
+ @request.params[var_name] = tmp_file.path
66
+ rescue
67
+ @request.params[var_name] = params[var_name]
68
+ end
69
+ end
70
+ tmp_file.close
71
+ else
72
+ @request.params[var_name] = params[var_name]
73
+ end
74
+ end
75
+ end
76
+
77
+ return @request
78
+ end
79
+
8
80
  def self.run(block, data = {}, api_key = nil )
9
81
  if !(data.is_a?(Hash))
10
82
  raise "your data needs to be a dictionary."
@@ -21,75 +93,57 @@ module Blockspring
21
93
  response = e.response
22
94
  end
23
95
 
96
+ results = response.body
97
+
98
+ begin
99
+ return JSON.parse(results)
100
+ rescue
101
+ return results
102
+ end
103
+ end
104
+
105
+ def self.runParsed(block, data = {}, api_key = nil )
106
+ if !(data.is_a?(Hash))
107
+ raise "your data needs to be a dictionary."
108
+ end
109
+
110
+ data = data.to_json
111
+ api_key = api_key || ENV['BLOCKSPRING_API_KEY'] || ""
112
+ blockspring_url = ENV['BLOCKSPRING_URL'] || 'https://sender.blockspring.com'
113
+ block = block.split("/")[-1]
114
+
115
+ begin
116
+ response = RestClient.post "https://sender.blockspring.com/api_v2/blocks/#{block}?api_key=#{api_key}", data, :content_type => :json
117
+ rescue => e
118
+ response = e.response
119
+ end
120
+
121
+ results = response.body
122
+
123
+
24
124
  begin
25
- body = JSON.parse(response.body)
125
+ parsed_results = JSON.parse(results)
126
+
127
+ if (!parsed_results.is_a?(Hash))
128
+ return parsed_results
129
+ else
130
+ parsed_results["_headers"] = response.headers
131
+ end
26
132
  rescue
27
- body = response.body
133
+ return results
28
134
  end
29
135
 
30
- return body
136
+ return self.parse(parsed_results, true)
31
137
  end
32
138
 
33
139
  def self.define(block)
34
- @request = Request.new
35
140
  @response = Response.new
36
141
 
37
142
  #stdin parsing
38
143
  if(!STDIN.tty?)
39
- begin
40
- params = JSON.parse($stdin.read)
41
- rescue
42
- raise "You didn't pass valid json inputs."
43
- end
44
-
45
- if !(params.is_a?(Hash))
46
- raise "Can't parse keys/values from your json inputs."
47
- end
48
-
49
- if !(params.has_key?("_blockspring_spec") && params["_blockspring_spec"])
50
- @request.instance_variable_set("@params", params)
51
- else
52
- for var_name in params.keys
53
- if (var_name == "_blockspring_spec")
54
- # pass
55
- elsif ((var_name == "_errors") && params[var_name].is_a?(Array))
56
- for error in params[var_name]
57
- if (error.is_a?(Hash)) && (error.has_key?("title"))
58
- @request.addError(error)
59
- end
60
- end
61
- elsif (
62
- params[var_name].is_a?(Hash) and
63
- params[var_name].has_key?("filename") and
64
- params[var_name]["filename"] and
65
- # either data or url must exist and not be empty
66
- (
67
- (params[var_name].has_key?("data") and params[var_name]["data"]) or
68
- (params[var_name].has_key?("url") and params[var_name]["url"]))
69
- )
70
- suffix = "-%s" % params[var_name]["filename"]
71
- tmp_file = Tempfile.new(["",suffix])
72
- if (params[var_name].has_key?("data"))
73
- begin
74
- tmp_file.write(Base64.decode64(params[var_name]["data"]))
75
- @request.params[var_name] = tmp_file.path
76
- rescue
77
- @request.params[var_name] = params[var_name]
78
- end
79
- else
80
- begin
81
- tmp_file.write(RestClient.get(params[var_name]["url"]))
82
- @request.params[var_name] = tmp_file.path
83
- rescue
84
- @request.params[var_name] = params[var_name]
85
- end
86
- end
87
- tmp_file.close
88
- else
89
- @request.params[var_name] = params[var_name]
90
- end
91
- end
92
- end
144
+ @request = self.parse($stdin.read, false)
145
+ else
146
+ @request = Request.new
93
147
  end
94
148
 
95
149
  #args parsing
@@ -121,6 +175,7 @@ module Blockspring
121
175
  def initialize
122
176
  @params = {}
123
177
  @_errors = []
178
+ @_headers = {}
124
179
  end
125
180
 
126
181
  attr_reader :params
@@ -133,6 +188,14 @@ module Blockspring
133
188
  def addError(error)
134
189
  @_errors.push(error)
135
190
  end
191
+
192
+ def addHeaders(headers)
193
+ @_headers = headers
194
+ end
195
+
196
+ def getHeaders
197
+ return @_headers
198
+ end
136
199
  end
137
200
 
138
201
  class Response
@@ -176,4 +239,25 @@ module Blockspring
176
239
  puts @result.to_json
177
240
  end
178
241
  end
242
+
243
+ def self.transform_hash(original, options={}, &block)
244
+ original.inject({}){|result, (key,value)|
245
+ value = if (options[:deep] && Hash === value)
246
+ transform_hash(value, options, &block)
247
+ else
248
+ value
249
+ end
250
+ block.call(result,key,value)
251
+ result
252
+ }
253
+ end
254
+
255
+ def self.stringify_keys(hash)
256
+ transform_hash(hash) {|hash, key, value|
257
+ hash[key.to_s] = value
258
+ }
259
+ end
260
+
261
+ private_class_method :transform_hash
262
+ private_class_method :stringify_keys
179
263
  end
@@ -1,3 +1,3 @@
1
1
  module Blockspring
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blockspring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Pinkus
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-11-16 00:00:00.000000000 Z
13
+ date: 2014-12-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler