Sha256: 42642086d9862f0bc64f1f14bcac3476ccbcf5a5a14039e164223575288f969e

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

require "uri"
require "net/http"

module Pushpad
  module Request
    extend self

    def head(endpoint, options = {})
      perform(Net::HTTP::Head, endpoint, options)
    end

    def get(endpoint, options = {})
      perform(Net::HTTP::Get, endpoint, options)
    end

    def post(endpoint, body, options = {})
      perform(Net::HTTP::Post, endpoint, options) do |request|
        request.body = body
      end
    end
    
    def patch(endpoint, body, options = {})
      perform(Net::HTTP::Patch, endpoint, options) do |request|
        request.body = body
      end
    end
    
    def delete(endpoint, options = {})
      perform(Net::HTTP::Delete, endpoint, options)
    end

    private

    def perform(method, endpoint, options)
      uri = URI.parse(endpoint)
      request = method.new(path_and_query(uri, options[:query_parameters]), headers)

      yield request if block_given?

      https(uri, request)
    end

    def path_and_query(uri, query_parameters)
      [uri.path, query(query_parameters)].compact.join("?")
    end

    def query(parameters)
      parameters && !parameters.empty? ? URI.encode_www_form(parameters) : nil
    end

    def https(uri, request)
      Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https|
        https.request(request)
      end
    end

    def headers
      raise "You must set Pushpad.auth_token" unless Pushpad.auth_token
      {
        "Authorization" => %(Token token="#{Pushpad.auth_token}"),
        "Content-Type" => "application/json;charset=UTF-8",
        "Accept" => "application/json"
      }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pushpad-1.5.0 lib/pushpad/request.rb
pushpad-1.4.0 lib/pushpad/request.rb
pushpad-1.3.0 lib/pushpad/request.rb