Class: Spout::Helpers::SendJson
- Inherits:
-
Object
- Object
- Spout::Helpers::SendJson
- Defined in:
- lib/spout/helpers/send_json.rb
Overview
Generates JSON web requests for POST and PATCH.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(url, args = {}) ⇒ SendJson
constructor
A new instance of SendJson.
- #patch ⇒ Object
- #post ⇒ Object
Constructor Details
#initialize(url, args = {}) ⇒ SendJson
Returns a new instance of SendJson.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/spout/helpers/send_json.rb', line 21 def initialize(url, args = {}) @params = args @url = URI.parse(url) @http = Net::HTTP.new(@url.host, @url.port) if @url.scheme == "https" @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_NONE end rescue @error = "Invalid URL: #{url.inspect}" puts @error.red end |
Class Method Details
.patch(url, *args) ⇒ Object
16 17 18 |
# File 'lib/spout/helpers/send_json.rb', line 16 def patch(url, *args) new(url, *args).patch end |
.post(*args) ⇒ Object
12 13 14 |
# File 'lib/spout/helpers/send_json.rb', line 12 def post(*args) new(*args).post end |
Instance Method Details
#patch ⇒ Object
49 50 51 52 |
# File 'lib/spout/helpers/send_json.rb', line 49 def patch @params["_method"] = "patch" post end |
#post ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/spout/helpers/send_json.rb', line 35 def post return unless @error.nil? header = { "Content-Type" => "application/json", "Accept" => "application/json" } response = @http.start do |http| http.post(@url.path, @params.to_json, header) end [JSON.parse(response.body), response] rescue => e puts "POST ERROR".red puts e.to_s.white nil end |