Sha256: f95e95f3a7a5c69654d2746e18b18a8428c7729d23863fe4a52ee2ac5bae79d2

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

module Calendly
  class Resource
    attr_reader :client

    def initialize(client)
      @client = client
    end

    private

    def get_request(url, params: {})
      handle_response request(url, Net::HTTP::Get, params: params)
    end

    def post_request(url, body:)
      handle_response request(url, Net::HTTP::Post, body: body)
    end

    def patch_request(url, body:)
      handle_response request(url, Net::HTTP::Patch, body: body)
    end

    def put_request(url, body)
      handle_response request(url, Net::HTTP::Put, body: body)
    end

    def delete_request(url, params: {})
      handle_response request(url, Net::HTTP::Delete, params: params)
    end

    def request(url, req_type, body: {}, params: {}, base_url: Client::BASE_URL)
      uri = URI("#{base_url}/#{url}")

      if params.any?
        params = URI.decode_www_form(uri.query || "") + params.to_a
        uri.query = URI.encode_www_form params
      end

      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE

      request = req_type.new(uri)
      request["Content-Type"] = "application/json"
      request["Authorization"] = "Bearer #{client.api_key}"
      request.body = body.to_json if body.any?

      http.request(request)
    end

    def handle_response(response)
      body = JSON.parse(response.read_body)
      case response.code
      when 400
        raise Error, "#{body["title"]}. #{body["message"]}"
      when 401
        raise Error, "#{body["title"]}. #{body["message"]}"
      when 403
        raise Error, "#{body["title"]}. #{body["message"]}"
      when 404
        raise Error, "#{body["title"]}. #{body["message"]}"
      when 429
        raise Error, "#{body["title"]}. #{body["message"]}"
      when 500
        raise Error, "#{body["title"]}. #{body["message"]}"
      when 503
        raise Error, "#{body["title"]}. #{body["message"]}"
      else
        body
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
calendlyr-0.3.3 lib/calendly/resource.rb