xrpc 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/xrpc/version.rb +1 -1
  3. data/lib/xrpc.rb +34 -5
  4. metadata +2 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7567ce5cde80c939278f9e0cc7a61010dd268c9d2104904c655cf0ad200dbe8
4
- data.tar.gz: 5c13b34f9c0ca6482f3d5040a1419c1c01d59421f19157488351ab344923e8bd
3
+ metadata.gz: cd2edff5dc4387dbbfeb6ecf00860e265cc93ed214eb2f7fb1f2fc8c192a1949
4
+ data.tar.gz: f07553b8c5d1d4e80e4a4f4e22b9e1b3871808f717e308432d9ba68cf51ae75b
5
5
  SHA512:
6
- metadata.gz: 5d9ddd10f82a2c7ffe866caabb58e2632f0ccc743b9640ce1f6223854f3a57b6080f28880ba11a066ff7d7261b4f744c8152cdcd928964b2a354fbf5e4af53f3
7
- data.tar.gz: e4a1cf0085fbe3c8c890d5077d6f3c73daf88351f9346749f68e6fec76c941fb457a6ba06cfbb2745bd3748a7a5c0d6723449c4f4c9753dc4bc5bd0e7f9323ea
6
+ metadata.gz: 5366e5bf46e307c3d7eebd426c79aa488cc8741797a9feedbd58891dc1a21b8ef308e1d03d7d2eeae2e5e948e29ef78ed1223fe96ba7d355ef861e240af9d2a5
7
+ data.tar.gz: f01c679d2164544877f8338a107966953756818e8d3e7fdcec2fd765a76fbd03dd04c812370ea632799ed4994d89c810e2a8fac69e7077acd45d18cf7b0067b6
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.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
data/lib/xrpc.rb CHANGED
@@ -4,20 +4,49 @@ require "httparty"
4
4
  require "json"
5
5
 
6
6
  module XRPC
7
+ def request(pds, endpoint_location, params)
8
+ Endpoint.new(pds, endpoint_location).get(params)
9
+ end
10
+
11
+ module_function :request
12
+
7
13
  class Endpoint
8
14
  attr_reader :request_uri
9
15
 
10
- def initialize(pds, endpoint_location, *params)
16
+ def initialize(pds, endpoint_location, authenticated: false, token: nil)
11
17
  @pds = pds
12
18
  @endpoint_location = endpoint_location
13
- @params = params
19
+ @authenticated = authenticated
20
+ @headers = default_headers()
21
+ if token # Ideally, you shouldn't pass the token when creating the endpoint
22
+ @headers = default_authenticated_headers(token)
23
+ end
24
+ end
25
+
26
+ def default_headers
27
+ { "Content-Type" => "application/json" }
28
+ end
29
+
30
+ def authenticated?() @authenticated end
31
+
32
+ def default_authenticated_headers(access_token)
33
+ default_headers.merge({
34
+ Authorization: "Bearer #{access_token}",
35
+ })
36
+ end
37
+
38
+ def authenticate(token) # This is the proper place to authenticate with a token
39
+ # This is still a pretty weird way to authenticate, but it works (for now)
40
+ if not @authenticated == true
41
+ raise Error, "Non-authenticated endpoint cannot be authenticated"
42
+ end
43
+ @headers = default_authenticated_headers(token)
14
44
  end
15
45
 
16
46
  def get(params)
17
- query_params = URI.encode_www_form(params)
47
+ query_params = URI.encode_www_form(params) # e.g. "foo=bar&baz=qux" from (foo: "bar", baz: "qux")
18
48
  @request_uri = URI("#{@pds}/xrpc/#{@endpoint_location}?#{query_params}")
19
-
20
- response = HTTParty.get(@request_uri)
49
+ response = HTTParty.get(@request_uri, headers: @headers)
21
50
  JSON.parse(response.body)
22
51
  end
23
52
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xrpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shreyan Jain
8
- - Tynan Burke
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2023-05-15 00:00:00.000000000 Z
11
+ date: 2023-05-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: json