Class: HnApi::HnClient
- Inherits:
-
Object
- Object
- HnApi::HnClient
- Defined in:
- lib/hn_api.rb
Class Method Summary collapse
-
._uniffi_allocate(pointer) ⇒ Object
A private helper for initializing instances of the class from a raw pointer, bypassing any initialization logic and ensuring they are GC’d properly.
-
._uniffi_define_finalizer_by_pointer(pointer, object_id) ⇒ Object
A private helper for registering an object finalizer.
-
._uniffi_lower(inst) ⇒ Object
A private helper for lowering instances into a raw pointer.
Instance Method Summary collapse
- #get_new_stories ⇒ Object
- #get_story(id) ⇒ Object
- #get_user(username) ⇒ Object
-
#initialize ⇒ HnClient
constructor
A new instance of HnClient.
Constructor Details
#initialize ⇒ HnClient
Returns a new instance of HnClient.
828 829 830 831 832 |
# File 'lib/hn_api.rb', line 828 def initialize() pointer = HnApi.rust_call(:hn_api_d6e5_HnClient_new,) @pointer = pointer ObjectSpace.define_finalizer(self, self.class._uniffi_define_finalizer_by_pointer(pointer, self.object_id)) end |
Class Method Details
._uniffi_allocate(pointer) ⇒ Object
A private helper for initializing instances of the class from a raw pointer, bypassing any initialization logic and ensuring they are GC’d properly.
799 800 801 802 803 804 805 |
# File 'lib/hn_api.rb', line 799 def self._uniffi_allocate(pointer) pointer.autorelease = false inst = allocate inst.instance_variable_set :@pointer, pointer ObjectSpace.define_finalizer(inst, _uniffi_define_finalizer_by_pointer(pointer, inst.object_id)) return inst end |
._uniffi_define_finalizer_by_pointer(pointer, object_id) ⇒ Object
A private helper for registering an object finalizer. N.B. it’s important that this does not capture a reference to the actual instance, only its underlying pointer.
810 811 812 813 814 815 816 817 |
# File 'lib/hn_api.rb', line 810 def self._uniffi_define_finalizer_by_pointer(pointer, object_id) Proc.new do |_id| HnApi.rust_call( :ffi_hn_api_d6e5_HnClient_object_free, pointer ) end end |
._uniffi_lower(inst) ⇒ Object
A private helper for lowering instances into a raw pointer. This does an explicit typecheck, because accidentally lowering a different type of object in a place where this type is expected, could lead to memory unsafety.
822 823 824 825 826 827 |
# File 'lib/hn_api.rb', line 822 def self._uniffi_lower(inst) if not inst.is_a? self raise TypeError.new "Expected a HnClient instance, got #{inst}" end return inst.instance_variable_get :@pointer end |
Instance Method Details
#get_new_stories ⇒ Object
841 842 843 844 |
# File 'lib/hn_api.rb', line 841 def get_new_stories() result = HnApi.rust_call(:hn_api_d6e5_HnClient_get_new_stories,@pointer,) return result.consumeIntoSequenceu32 end |
#get_story(id) ⇒ Object
845 846 847 848 849 |
# File 'lib/hn_api.rb', line 845 def get_story(id) id = id.to_i result = HnApi.rust_call(:hn_api_d6e5_HnClient_get_story,@pointer,id) return result.consumeIntoOptionalTypeStory end |
#get_user(username) ⇒ Object
836 837 838 839 840 |
# File 'lib/hn_api.rb', line 836 def get_user(username) username = username.to_s result = HnApi.rust_call(:hn_api_d6e5_HnClient_get_user,@pointer,RustBuffer.allocFromString(username)) return result.consumeIntoOptionalTypeUser end |