Sha256: b084c7716242665bdd06ae4799b6d822910ea5bfde699d06429bedf9343e1737

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 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 => e
      puts "NDS API GEM: #{e} (#{url})"
      return e
    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.path,
        'Content-Type' => 'application/json'
      )
      req.basic_auth api_user, api_password
      # req.use_ssl = true
      req.body = data.to_json
      response = Net::HTTP.new(url.host, url.port).request(req)
      JSON.parse(response.body)
    rescue => e
      puts "NDS API GEM: #{e} (#{post_or_put}:#{url})"
      return 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

2 entries across 2 versions & 1 rubygems

Version Path
nds_api-0.1.6 lib/nds_api/http.rb
nds_api-0.1.5 lib/nds_api/http.rb