Sha256: 6bf0ae721525fe2f0345bbc5f803b9c77271c1acc54f45e3df5217b425593ed7

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require 'net/http'

module Pdfmonkey
  class Adapter
    def initialize(config: Pdfmonkey.configuration)
      @config = config
    end

    def call(method, resource)
      response = send_request(method, resource)

      if response.is_a?(Net::HTTPSuccess)
        extract_attributes(response, resource)
      else
        extract_errors(response)
      end
    rescue StandardError => e
      { errors: [e.message], status: 'error' }
    end

    private def build_get_request(uri, _resource)
      Net::HTTP::Get.new(uri, headers)
    end

    private def build_post_request(uri, resource)
      request = Net::HTTP::Post.new(uri, headers)
      request.body = resource.to_json
      request
    end

    private def extract_attributes(response, resource)
      member = resource.class.const_get('MEMBER')
      JSON.parse(response.body).fetch(member)
    end

    private def extract_errors(response)
      payload = JSON.parse(response.body)
      errors = payload['errors'].to_a.map { |error| error['detail'] }

      { errors: errors, status: 'error' }
    end

    private def headers
      {
        'Authorization' => "Bearer #{config.private_key}",
        'Content-Type' => 'application/json'
      }
    end

    private def send_request(method, resource)
      uri = URI(url_for(resource))
      request = send("build_#{method}_request", uri, resource)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = (uri.scheme == 'https')
      http.request(request)
    end

    private def url_for(resource)
      collection = resource.class.const_get('COLLECTION')
      endpoint = "#{config.host}/#{config.namespace}/#{collection}"
      endpoint += "/#{resource.id}" if resource.id
      endpoint
    end

    private

    attr_reader :config
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pdfmonkey-0.4.0 lib/pdfmonkey/adapter.rb