xrpc 0.1.4 → 0.1.6

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: 3dec847e75b45ae7c5f96ab323ea51056b487aa7d2751b701124a176dd02033d
4
- data.tar.gz: b596ec43d3389cb83e62be7704b76b2ff5a977b6bd48181ffe26fa54b285afc7
3
+ metadata.gz: 0f3d51cb6ae781b3eb9c5d63e2169cb50190953addaa2c893818b6e3da7e6e4b
4
+ data.tar.gz: dd43f705448252a67bca0b722dc7feb8042722d76d55d8b9e79d21994a15affb
5
5
  SHA512:
6
- metadata.gz: eb999aacd7356b46141e42be25f268be91895f5108489b20b3e3c36ec0ff81170513f733181e3de2944dd1e804a750f86b363663efda983e61f36dbfd48030bf
7
- data.tar.gz: 6b7f1335ff4745914a227ce006c9d07157dc6b1fed5375a92db3abc29e421d6846704950df591a3ad7aea136361c7becb17040c399692707982fe12281536b9b
6
+ metadata.gz: e1622cf6190d363d507d1db065d00e8db8acbb744ed049c85de0fc71b206d3ae693ea79dd9a66d3f9be14f488821fedc1b70a6d6c6c8adbdda7bfaef9cf7c3f4
7
+ data.tar.gz: eb7834ed2d23dbb57ca42686603ba8cdae429a7070923f44d0db5efb3e5fce8d08f2cee1c5f6f843d468c719315e23d3fcec69e6d362fae91ceaa7e92efa70f0
data/lib/xrpc/client.rb CHANGED
@@ -27,8 +27,8 @@ module XRPC
27
27
  end
28
28
 
29
29
  def method_missing(method_name, **params)
30
- response = self.class.get("/xrpc/#{method_name.to_s.gsub("_", ".")}", query: params, headers: @headers).body
31
- response.empty? ? response.code : JSON.parse(response)
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)
32
32
  end
33
33
  end
34
34
 
@@ -41,8 +41,8 @@ module XRPC
41
41
  end
42
42
 
43
43
  def method_missing(method_name, **params)
44
- response = self.class.post("/xrpc/#{method_name.to_s.gsub("_", ".")}", body: params.to_json, headers: @headers).body
45
- response.empty? ? response.code : JSON.parse(response)
44
+ response = self.class.post("/xrpc/#{method_name.to_s.gsub("_", ".")}", body: params.to_json, headers: @headers)
45
+ response.body.empty? ? response.code : JSON.parse(response.body)
46
46
  end
47
47
  end
48
48
  end
@@ -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.4"
4
+ VERSION = "0.1.6"
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.4
4
+ version: 0.1.6
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-07-24 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