Class: NotionRuby::ResourceProxy
- Inherits:
-
Object
- Object
- NotionRuby::ResourceProxy
- Includes:
- CRUD
- Defined in:
- lib/notion_ruby/resource_proxy.rb
Overview
ResourceProxy lets us create a virtual proxy for any API resource, utilizing method_missing to handle passing messages to the real object
Direct Known Subclasses
API::Blocks::Children::Proxy, API::Blocks::Proxy, API::Databases::Proxy, API::Pages::Proxy, API::Search::Proxy, API::Users::Proxy
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Make connection and path_prefix readable.
-
#id ⇒ Object
readonly
Make connection and path_prefix readable.
-
#params ⇒ Object
readonly
Make connection and path_prefix readable.
-
#path_prefix ⇒ Object
readonly
Make connection and path_prefix readable.
Instance Method Summary collapse
- #build_prefix(first_argument, endpoint) ⇒ Object
-
#initialize(connection, path_prefix, params = {}, &block) ⇒ ResourceProxy
constructor
Instantiates proxy with the connection and path_prefix.
-
#method_missing(message, *args, &block) ⇒ Object
Method_missing takes any message passed to the ResourceProxy and sends it to the real object.
-
#raw ⇒ Object
Raw is the raw response from the faraday Use this if you need access to status codes or header values.
- #respond_to_missing?(symbol, _include_private) ⇒ Boolean
-
#subject ⇒ Object
Subject is the response body parsed as json.
Methods included from CRUD
Constructor Details
#initialize(connection, path_prefix, params = {}, &block) ⇒ ResourceProxy
Instantiates proxy with the connection and path_prefix
connection - [NotionRuby::Connection] object path_prefix - String param - Hash or String
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/notion_ruby/resource_proxy.rb', line 27 def initialize(connection, path_prefix, params = {}, &block) unless params.is_a? Hash @id = params params = {} end @connection = connection @path_prefix = path_prefix @params = params @block = block if block subject if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(message, *args, &block) ⇒ Object
Method_missing takes any message passed to the ResourceProxy and sends it to the real object
message - Message object args* - Arguments passed
46 47 48 |
# File 'lib/notion_ruby/resource_proxy.rb', line 46 def method_missing(, *args, &block) subject.send(, *args, &block) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Make connection and path_prefix readable
18 19 20 |
# File 'lib/notion_ruby/resource_proxy.rb', line 18 def connection @connection end |
#id ⇒ Object (readonly)
Make connection and path_prefix readable
18 19 20 |
# File 'lib/notion_ruby/resource_proxy.rb', line 18 def id @id end |
#params ⇒ Object (readonly)
Make connection and path_prefix readable
18 19 20 |
# File 'lib/notion_ruby/resource_proxy.rb', line 18 def params @params end |
#path_prefix ⇒ Object (readonly)
Make connection and path_prefix readable
18 19 20 |
# File 'lib/notion_ruby/resource_proxy.rb', line 18 def path_prefix @path_prefix end |
Instance Method Details
#build_prefix(first_argument, endpoint) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/notion_ruby/resource_proxy.rb', line 74 def build_prefix(first_argument, endpoint) if !first_argument.is_a?(Hash) && !first_argument.nil? File.join(path_prefix, "/#{endpoint}/#{first_argument}") else File.join(path_prefix, "/#{endpoint}") end end |
#raw ⇒ Object
Raw is the raw response from the faraday Use this if you need access to status codes or header values
58 59 60 |
# File 'lib/notion_ruby/resource_proxy.rb', line 58 def raw connection.get(path_prefix) { |req| req.params.merge! params } end |
#respond_to_missing?(symbol, _include_private) ⇒ Boolean
50 51 52 |
# File 'lib/notion_ruby/resource_proxy.rb', line 50 def respond_to_missing?(symbol, _include_private) subject.keys(symbol) end |
#subject ⇒ Object
Subject is the response body parsed as json
Returns json
67 68 69 70 71 72 |
# File 'lib/notion_ruby/resource_proxy.rb', line 67 def subject @subject ||= connection.get(path_prefix) do |req| req.params.merge! params @block&.call(req) end.body end |