xrpc 0.1.5 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 888100afa57e9d231ced56deeb8c2081106e80010a39222471b96f52f4a4ec0e
4
- data.tar.gz: e67558c7515a072013cb7cec64348c15e5a0a50c0a63704a653a4ba3102b7e16
3
+ metadata.gz: 89265cba7bc367d5a468be6b3ea0c0deb34fff4f2427f6b313ad6ea61da73848
4
+ data.tar.gz: 1c3c6b6971e69e3cb5d5baf27edc66663267a09dee219f730c94386720bc4c5a
5
5
  SHA512:
6
- metadata.gz: afedadd059ba04cd2089b45493f9e452a7f5cf415273a53581af91349d6b6fa5a025ec91ed905e6cb3dc42fe73a9f04b2d4dd6454bdec1444fb2ecb01c59b94f
7
- data.tar.gz: 5aa3f9656bad8a5512c50dd7e2aab8b529912d4877cf032bf287176640b8d79c87f72dad615221a49c666831e45aae004e5e6ad8874a9a22ea83ab959351c529
6
+ metadata.gz: 2580f8cb019432556ca537833620af3bbb3b44011ceaa07a34fc8d4672a3e8a00289e2eb35e0443f7c27fae7b162b25f36f1f7215a216fc31689c58a2bbb551c
7
+ data.tar.gz: 60e7829c7f44abb67e3064cb17bbc6efebf5fa196666fbae10ec7c19a14832749baa25168badd88c077a7cf2ee6a5bbe126066a33cf2baf50a09cbbbb001c104
data/lib/xrpc/client.rb CHANGED
@@ -9,13 +9,28 @@ module XRPC
9
9
  end
10
10
 
11
11
  class Client
12
- attr_reader :get, :post
12
+ attr_accessor :headers, :token
13
13
 
14
14
  def initialize(base_url, token = nil)
15
- @headers = { :"Content-Type" => "application/json", :Authorization => "Bearer #{token}" }
15
+ @token = token
16
+ @headers = { :"Content-Type" => "application/json", :Authorization => "Bearer #{@token}" }
16
17
  @base_url = base_url
17
- @get = GetRequest.new(base_url, @headers)
18
- @post = PostRequest.new(base_url, @headers)
18
+ end
19
+
20
+ def headers!
21
+ @headers = { :"Content-Type" => "application/json", :Authorization => "Bearer #{@token}" }
22
+ end
23
+
24
+ def get
25
+ GetRequest.new(base_url, @headers)
26
+ end
27
+
28
+ def post
29
+ PostRequest.new(base_url, @headers)
30
+ end
31
+
32
+ def inspect
33
+ "XRPC::Client(\"#{@base_url}\")"
19
34
  end
20
35
 
21
36
  class GetRequest
@@ -27,8 +42,9 @@ module XRPC
27
42
  end
28
43
 
29
44
  def method_missing(method_name, **params)
30
- response = self.class.get("/xrpc/#{method_name.to_s.gsub("_", ".")}", query: params, headers: @headers)
31
- response.body.empty? ? response.code : JSON.parse(response.body)
45
+ self.class.get("/xrpc/#{method_name.to_s.gsub("_", ".")}", query: params, headers: @headers).then do |response|
46
+ response.body.empty? ? response.code : JSON.parse(response.body)
47
+ end
32
48
  end
33
49
  end
34
50
 
@@ -0,0 +1,60 @@
1
+ module XRPC
2
+ def self.xrpc_proc(lexicon, type, args_list, server = "https://bsky.social")
3
+ case type
4
+ when :query
5
+ return XRPC::Query.new(lexicon, args_list, server)
6
+ when :procedure
7
+ return XRPC::Procedure.new(lexicon, args_list, server)
8
+ else
9
+ raise ArgumentError, "Invalid type: #{type}. Expected :query or :procedure."
10
+ end
11
+ end
12
+
13
+ def self.xrpc_object_from_json(json_hash)
14
+ json_hash[:id] ? lexicon = json_hash[:id] : lexicon = json_hash["id"]
15
+ json_hash[:defs] ? defs = json_hash[:defs] : defs = json_hash["defs"]
16
+ defs[:main] ? main_def = defs[:main] : main_def = defs["main"]
17
+ main_def[:type] ? type = main_def[:type].to_sym : type = main_def["type"].to_sym
18
+ main_def[:parameters] ? args_list = main_def[:parameters][:required] : args_list = main_def["parameters"]["required"]
19
+ xrpc_proc(lexicon, type, args_list)
20
+ end
21
+ end
22
+
23
+ module XRPC
24
+ class BaseLexicon
25
+ attr_accessor :lexicon, :args_list, :server, :headers
26
+
27
+ def initialize(lexicon, args_list, server, headers: { "Content-Type" => "application/json" })
28
+ @lexicon = lexicon
29
+ @args_list = args_list
30
+ @server = server
31
+ @headers = headers
32
+ end
33
+
34
+ def call(*args, **kwargs)
35
+ make_request(Hash[args_list.zip(args)].merge kwargs)
36
+ end
37
+
38
+ def make_request(payload)
39
+ raise NotImplementedError, "Subclasses must implement the .make_request method."
40
+ end
41
+
42
+ private
43
+
44
+ def base_url
45
+ "#{server}/xrpc/#{lexicon}"
46
+ end
47
+ end
48
+
49
+ class Query < BaseLexicon
50
+ def make_request(payload)
51
+ HTTParty.get(base_url, query: payload, headers: headers).parsed_response
52
+ end
53
+ end
54
+
55
+ class Procedure < BaseLexicon
56
+ def make_request(payload)
57
+ HTTParty.post(base_url, body: payload, headers: headers).parsed_response
58
+ end
59
+ end
60
+ end
data/lib/xrpc/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module XRPC
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.7"
5
5
  end
data/lib/xrpc.rb CHANGED
@@ -1 +1,2 @@
1
1
  require "xrpc/client"
2
+ require "xrpc/lexicon"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xrpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shreyan Jain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-10 00:00:00.000000000 Z
11
+ date: 2023-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -61,6 +61,7 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - "./lib/xrpc.rb"
63
63
  - "./lib/xrpc/client.rb"
64
+ - "./lib/xrpc/lexicon.rb"
64
65
  - "./lib/xrpc/server.rb"
65
66
  - "./lib/xrpc/version.rb"
66
67
  homepage: https://github.com/ShreyanJain9/xrpc