Sha256: e0e4d188a64658105b5c01ba87f2f744eba906c130dbd8c12eb25254ef230830

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Reattract
  # Makes requests using the Connection object
  class Request
    attr_accessor :connection

    def initialize(path:, bearer_token: generate_bearer)
      @connection = Reattract::Connection.new(
        authorization: authorization(bearer_token),
        path:          path
      )
    end

    def get(query: {}, pagination: nil, sort: nil)
      if pagination
        query[:page]  = pagination[:page]
        query[:limit] = pagination[:limit]
      end
      query[:s] = sort if sort
      connection.get(query: query)
    end

    def patch(body: {})
      connection.patch(body: body)
    end

    def post(body: {})
      connection.post(body: body)
    end

    def put(body: {})
      connection.put(body: body)
    end

    private

    def generate_bearer
      Reattract::JwtGenerator.new.generate
    end

    def prepare_request_with_body(req, body)
      req.url endpoint
      req.body = body.to_json
    end

    def authorization(bearer_token)
      "Bearer #{bearer_token}"
    end

    def conn
      @conn ||= Connection.build(authorization: authorization)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reattract-0.4.3 lib/reattract/request.rb
reattract-0.4.1 lib/reattract/request.rb