A library for handling DID identifiers used in Bluesky AT Protocol

added more request helpers

+36
+36
lib/didkit/requests.rb
··· 1 + require 'json' 2 + require 'net/http' 3 + require 'uri' 4 + 1 5 module DIDKit 2 6 module Requests 3 7 def get_response(url, options = {}) ··· 34 38 else 35 39 return response 36 40 end 41 + end 42 + end 43 + 44 + def get_data(url, options = {}) 45 + content_type = options.delete(:content_type) 46 + response = get_response(url, options) 47 + 48 + if response.is_a?(Net::HTTPSuccess) && content_type_matches(response, content_type) && (data = response.body) 49 + data 50 + else 51 + raise APIError.new(response) 52 + end 53 + end 54 + 55 + def get_json(url, options = {}) 56 + JSON.parse(get_data(url, options)) 57 + end 58 + 59 + def content_type_matches(response, expected_type) 60 + content_type = response['Content-Type'] 61 + 62 + case expected_type 63 + when String 64 + content_type == expected_type 65 + when Regexp 66 + content_type =~ expected_type 67 + when :json 68 + content_type =~ /^application\/json(;.*)?$/ 69 + when nil 70 + true 71 + else 72 + raise ArgumentError, "Invalid expected_type: #{expected_type.inspect}" 37 73 end 38 74 end 39 75 end