Class: ActivePostgrest::Client
- Inherits:
-
Object
- Object
- ActivePostgrest::Client
- Defined in:
- lib/active_postgrest/client.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
- #anonymous ⇒ Object
- #delete(resource, params, prefer: 'return=representation', schema: nil) ⇒ Object
- #explain(resource, params = {}, schema: nil) ⇒ Object
- #get(resource, params = {}, count: :exact, schema: nil) ⇒ Object
-
#initialize(base_url = ENV.fetch('POSTGREST_URL'), jwt_token = nil) ⇒ Client
constructor
A new instance of Client.
- #openapi ⇒ Object
- #patch(resource, params, body, prefer: 'return=representation', schema: nil) ⇒ Object
- #post(resource, body, prefer: 'return=representation', schema: nil) ⇒ Object
- #table_schema(table) ⇒ Object
- #tables ⇒ Object
- #with_token(jwt) ⇒ Object
Constructor Details
#initialize(base_url = ENV.fetch('POSTGREST_URL'), jwt_token = nil) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 |
# File 'lib/active_postgrest/client.rb', line 8 def initialize(base_url = ENV.fetch('POSTGREST_URL'), jwt_token = nil) @base_url = base_url @auth_header = "Bearer #{jwt_token}" if jwt_token @conn = Faraday.new(base_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |f| f.request :json f.response :json end end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
6 7 8 |
# File 'lib/active_postgrest/client.rb', line 6 def base_url @base_url end |
Instance Method Details
#anonymous ⇒ Object
79 80 81 |
# File 'lib/active_postgrest/client.rb', line 79 def anonymous self.class.new(@base_url) end |
#delete(resource, params, prefer: 'return=representation', schema: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/active_postgrest/client.rb', line 68 def delete(resource, params, prefer: 'return=representation', schema: nil) response = @conn.delete(resource) do |req| auth_headers(req) req.params.update(params) req.headers['Prefer'] = prefer req.headers['Content-Profile'] = schema if schema end raise_on_error!(response) response end |
#explain(resource, params = {}, schema: nil) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/active_postgrest/client.rb', line 29 def explain(resource, params = {}, schema: nil) @conn.get(resource, params) do |req| auth_headers(req) req.headers['Accept'] = 'application/vnd.pgrst.plan+text; for="application/json"; options=verbose' req.headers['Accept-Profile'] = schema if schema end.body end |
#get(resource, params = {}, count: :exact, schema: nil) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/active_postgrest/client.rb', line 37 def get(resource, params = {}, count: :exact, schema: nil) response = @conn.get(resource, params) do |req| auth_headers(req) req.headers['Prefer'] = "count=#{count}" req.headers['Accept-Profile'] = schema if schema end raise_on_error!(response) response end |
#openapi ⇒ Object
17 18 19 |
# File 'lib/active_postgrest/client.rb', line 17 def openapi @openapi ||= @conn.get('/').body end |
#patch(resource, params, body, prefer: 'return=representation', schema: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_postgrest/client.rb', line 57 def patch(resource, params, body, prefer: 'return=representation', schema: nil) response = @conn.patch(resource, body) do |req| auth_headers(req) req.params.update(params) req.headers['Prefer'] = prefer req.headers['Content-Profile'] = schema if schema end raise_on_error!(response) response end |
#post(resource, body, prefer: 'return=representation', schema: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/active_postgrest/client.rb', line 47 def post(resource, body, prefer: 'return=representation', schema: nil) response = @conn.post(resource, body) do |req| auth_headers(req) req.headers['Prefer'] = prefer req.headers['Content-Profile'] = schema if schema end raise_on_error!(response) response end |
#table_schema(table) ⇒ Object
25 26 27 |
# File 'lib/active_postgrest/client.rb', line 25 def table_schema(table) openapi.dig('definitions', table) || {} end |
#tables ⇒ Object
21 22 23 |
# File 'lib/active_postgrest/client.rb', line 21 def tables openapi['paths']&.keys&.filter_map { |p| p.delete_prefix('/').then { |s| s.empty? ? nil : s } } || [] end |
#with_token(jwt) ⇒ Object
83 84 85 |
# File 'lib/active_postgrest/client.rb', line 83 def with_token(jwt) self.class.new(@base_url, jwt) end |