Sha256: af964ee623cf6168adf37c0344dec2bce7238ef613d88393916d148831eb5e4f

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'open-uri'

module Cosensee
  module Api
    # for page-data API
    class PageData
      def export(project_name:, sid:)
        uri = "https://scrapbox.io/api/page-data/export/#{project_name}.json"
        send_request(uri, sid)
      end

      def download(project_name:, sid:, filename:)
        res = export(project_name:, sid:)

        begin
          File.binwrite(filename, res)
        rescue SystemCallError => e
          raise Cosensee::Error, "Failed to write to file '#{filename}': #{e.message}"
        end
      end

      private

      def send_request(uri, sid)
        cookies = "connect.sid=#{sid}"
        parsed_uri = URI.parse(uri)

        begin
          response = parsed_uri.open('Cookie' => cookies)
          response.read
        rescue OpenURI::HTTPError => e
          raise Cosensee::Error, "HTTP error while accessing #{uri}: #{e.message}"
        rescue SocketError => e
          raise Cosensee::Error, "Network error: #{e.message}"
        rescue URI::InvalidURIError => e
          raise Cosensee::Error, "Invalid URI: #{e.message}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cosensee-0.8.0 lib/cosensee/api/page_data.rb