Sha256: e59daf44a84845b31a9acc4af401c9e21cf98c76b4c64fa808b907cff58f1b5b
Contents?: true
Size: 1.28 KB
Versions: 6
Compression:
Stored size: 1.28 KB
Contents
require 'json' require 'net/http' require 'open-uri' require 'uri' module NdsApi class Http def initialize(options = {}) @options = options.dup end def get(url) JSON.parse(open(url, http_basic_authentication: [api_user, api_password]).read) rescue StandardError => error puts "NDS API ERROR: #{error} (GET:#{url})" raise error end def post(url, data) http_action('post', url, data) end def put(url, data) http_action('put', url, data) end private def http_action(post_or_put, url, data) url = URI.parse(url) req = http_verb_object(post_or_put).new( url, 'Content-Type' => 'application/json' ) req.basic_auth api_user, api_password req.body = data.to_json http = Net::HTTP.new(url.host, url.port) http.use_ssl = true response = http.request(req) JSON.parse(response.body) rescue StandardError => error puts "NDS API ERROR: #{error} (#{post_or_put}:#{url})" raise e end def http_verb_object(post_or_put) return Net::HTTP::Post if post_or_put == 'post' return Net::HTTP::Put if post_or_put == 'put' end def api_user @options[:user] end def api_password @options[:password] end end end
Version data entries
6 entries across 6 versions & 1 rubygems