Sha256: ef189c86a25558507e0a12a87392139b528ac6722ef9c7ba108d9c1e1461fa56

Contents?: true

Size: 1017 Bytes

Versions: 1

Compression:

Stored size: 1017 Bytes

Contents

require "net/http"
require "json"

module Mergent
  class Client
    def self.post(resource, params) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
      uri = URI("https://api.mergent.co/v1/#{resource}")
      headers = {
        Authorization: "Bearer #{Mergent.api_key}",
        "Content-Type": "application/json"
      }
      request = Net::HTTP::Post.new(uri, headers)
      request.body = params.to_json

      https = Net::HTTP.new(uri.host, uri.port)
      https.use_ssl = true
      response = https.request(request)

      case response
      when Net::HTTPSuccess
        JSON.parse(response.read_body, object_class: Mergent::Object)
      else
        begin
          body = JSON.parse(response.read_body)
        rescue JSON::ParserError
          body = {}
        end
        raise Mergent::Error, body["message"]
      end
    rescue EOFError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, SocketError
      raise Mergent::ConnectionError
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mergent-0.1.0 lib/mergent/client.rb