Class: NotionRuby::ResourceProxy

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CRUD

#create, #get, #patch

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(message, *args, &block)
  subject.send(message, *args, &block)
end

Instance Attribute Details

#connectionObject (readonly)

Make connection and path_prefix readable



18
19
20
# File 'lib/notion_ruby/resource_proxy.rb', line 18

def connection
  @connection
end

#idObject (readonly)

Make connection and path_prefix readable



18
19
20
# File 'lib/notion_ruby/resource_proxy.rb', line 18

def id
  @id
end

#paramsObject (readonly)

Make connection and path_prefix readable



18
19
20
# File 'lib/notion_ruby/resource_proxy.rb', line 18

def params
  @params
end

#path_prefixObject (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

#rawObject

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/notion_ruby/resource_proxy.rb', line 50

def respond_to_missing?(symbol, _include_private)
  subject.keys(symbol)
end

#subjectObject

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