tangled
alpha
login
or
join now
mackuba.eu
/
didkit
1
fork
atom
A library for handling DID identifiers used in Bluesky AT Protocol
1
fork
atom
overview
issues
pulls
pipelines
added more request helpers
mackuba.eu
7 months ago
9ac59883
68b526d0
+36
1 changed file
expand all
collapse all
unified
split
lib
didkit
requests.rb
+36
lib/didkit/requests.rb
···
0
0
0
0
1
module DIDKit
2
module Requests
3
def get_response(url, options = {})
···
34
else
35
return response
36
end
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
37
end
38
end
39
end
···
1
+
require 'json'
2
+
require 'net/http'
3
+
require 'uri'
4
+
5
module DIDKit
6
module Requests
7
def get_response(url, options = {})
···
38
else
39
return response
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}"
73
end
74
end
75
end