From 7f459e15c6dbacc79a2e703aa6fb50c1da62bb8c Mon Sep 17 00:00:00 2001 From: Daniel1984 Date: Thu, 30 Jul 2020 13:01:56 +0300 Subject: [PATCH 1/5] restv2 adding pulse resource support --- CHANGELOG | 8 +++ examples/rest/v2/pulse.rb | 47 +++++++++++++++++ lib/bitfinex.rb | 2 + lib/models/pulse.rb | 37 +++++++++++++ lib/models/pulse_profile.rb | 31 +++++++++++ lib/rest/v2.rb | 2 + lib/rest/v2/pulse.rb | 101 ++++++++++++++++++++++++++++++++++++ 7 files changed, 228 insertions(+) create mode 100644 examples/rest/v2/pulse.rb create mode 100644 lib/models/pulse.rb create mode 100644 lib/models/pulse_profile.rb create mode 100644 lib/rest/v2/pulse.rb diff --git a/CHANGELOG b/CHANGELOG index 466f3d9..17b4e42 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,11 @@ +1.0.10 +- Adds function rest/v2 get_pulse_profile +- Adds function rest/v2 get_public_pulse_history +- Adds function rest/v2 submit_pulse +- Adds function rest/v2 delete_pulse +- Adds function rest/v2 get_private_pulse_history +- Adds function rest/v2 submit_pulse_comment + 1.0.9 - Fixes bug with API v2 transfer diff --git a/examples/rest/v2/pulse.rb b/examples/rest/v2/pulse.rb new file mode 100644 index 0000000..1b1e3ae --- /dev/null +++ b/examples/rest/v2/pulse.rb @@ -0,0 +1,47 @@ +require_relative '../../../lib/bitfinex.rb' + +client = Bitfinex::RESTv2.new({ + :url => ENV['REST_URL'], + :api_key => ENV['API_KEY'], + :api_secret => ENV['API_SECRET'] +}) + +# Get pulse profile +p client.get_pulse_profile('Bitfinex') + +# Get public pulse history +p client.get_public_pulse_history({ :limit => 5 }) + +# Submit new Pulse message +pulse = client.submit_pulse({ + :title => '1234 5678 Foo Bar Baz Qux TITLE', + :content => '1234 5678 Foo Bar Baz Qux Content', + :isPublic => 0, + :isPin => 1 +}) +p pulse + +# Delete Pulse message +p "About to delete pulse: #{pulse[:id]}" +p client.delete_pulse(pulse[:id]) + +# Get private pulse history +p client.get_private_pulse_history() + +# Submit Pulse message comment +# 1 - create pulse message +pulse2 = client.submit_pulse({ + :title => '2 1234 5678 Foo Bar Baz Qux TITLE', + :content => '2 1234 5678 Foo Bar Baz Qux Content', + :isPublic => 0, + :isPin => 1 +}) + +# 2 - submit comment for above pulse message +p client.submit_pulse_comment({ + :parent => pulse2[:id], + :title => 'comment 2 1234 5678 Foo Bar Baz Qux TITLE', + :content => 'comment 2 1234 5678 Foo Bar Baz Qux Content', + :isPublic => 0, + :isPin => 1 +}) \ No newline at end of file diff --git a/lib/bitfinex.rb b/lib/bitfinex.rb index 6cc9fd1..863af91 100644 --- a/lib/bitfinex.rb +++ b/lib/bitfinex.rb @@ -34,3 +34,5 @@ require_relative './models/trading_ticker' require_relative './models/user_info' require_relative './models/wallet' +require_relative './models/pulse_profile' +require_relative './models/pulse' diff --git a/lib/models/pulse.rb b/lib/models/pulse.rb new file mode 100644 index 0000000..567225e --- /dev/null +++ b/lib/models/pulse.rb @@ -0,0 +1,37 @@ +require_relative './model' + +module Bitfinex + module Models + class Pulse < Model + BOOL_FIELDS = [] + FIELDS = { + :id => 0, + :mts_create => 1, + :pulse_user_id => 3, + :title => 5, + :content => 6, + :is_pin => 9, + :is_public => 10, + :comments_disabled => 11, + :tags => 12, + :attachments => 13, + :meta => 14, + :likes => 15, + :profile => 18, + :comments => 19 + } + + FIELDS.each do |key, index| + attr_accessor key + end + + def initialize (data) + super(data, FIELDS, BOOL_FIELDS) + end + + def self.unserialize (data) + Model.unserialize(data, FIELDS, BOOL_FIELDS) + end + end + end +end diff --git a/lib/models/pulse_profile.rb b/lib/models/pulse_profile.rb new file mode 100644 index 0000000..d3bb7a2 --- /dev/null +++ b/lib/models/pulse_profile.rb @@ -0,0 +1,31 @@ +require_relative './model' + +module Bitfinex + module Models + class PulseProfile < Model + BOOL_FIELDS = [] + FIELDS = { + :id => 0, + :mts_create => 1, + :nickname => 3, + :picture => 5, + :text => 6, + :twitter_handle => 9, + :followers => 11, + :following => 12 + } + + FIELDS.each do |key, index| + attr_accessor key + end + + def initialize (data) + super(data, FIELDS, BOOL_FIELDS) + end + + def self.unserialize (data) + return Model.unserialize(data, FIELDS, BOOL_FIELDS) + end + end + end +end diff --git a/lib/rest/v2.rb b/lib/rest/v2.rb index ff0f2be..8b03c50 100644 --- a/lib/rest/v2.rb +++ b/lib/rest/v2.rb @@ -9,6 +9,7 @@ require_relative './v2/wallet' require_relative './v2/funding' require_relative './v2/positions' +require_relative './v2/pulse' module Bitfinex class RESTv2 @@ -27,6 +28,7 @@ class RESTv2 include Bitfinex::RESTv2Wallet include Bitfinex::RESTv2Funding include Bitfinex::RESTv2Positions + include Bitfinex::RESTv2Pulse def initialize(args = {}) self.api_endpoint = args[:url] ? "#{args[:url]}/v2/" : "https://api.bitfinex.com/v2/" diff --git a/lib/rest/v2/pulse.rb b/lib/rest/v2/pulse.rb new file mode 100644 index 0000000..47f5322 --- /dev/null +++ b/lib/rest/v2/pulse.rb @@ -0,0 +1,101 @@ +module Bitfinex + module RESTv2Pulse + ### + # Get Pulse Profile + # + # @param [string] nickname Nickname of pulse profile + # + # @return [Hash] the Pulse Profile + # + # @see https://docs.bitfinex.com/reference#rest-public-pulse-profile + ### + def get_pulse_profile(nickname) + resp = get("pulse/profile/#{nickname}").body + Bitfinex::Models::PulseProfile.unserialize(resp) + end + + ### + # Get Public Pulse History + # + # @param [int] end (optional) Return only the entries after this timestamp + # @param [int] limit (optional) Limit the number of entries to return. Default is 25. + # + # @return [Array] public pulse message + # + # @see https://docs.bitfinex.com/reference#rest-public-pulse-hist + ### + def get_public_pulse_history(params = {}) + pulses = get("pulse/hist", params).body + pulses.map { |p| deserialize_pulse_with_profile(p) } + end + + ### + # Get Private Pulse History + # + # @param [int] isPublic allows to receive the public pulse history with the UID_LIKED field + # + # @return [Array] private pulse message + # + # @see https://docs.bitfinex.com/reference#rest-auth-pulse-hist + ### + def get_private_pulse_history(params = {}) + pulses = authenticated_post("auth/r/pulse/hist", params).body + pulses.map { |p| deserialize_pulse_with_profile(p) } + end + + ### + # Submit new Pulse message + # + # @param [Hash] pulse + # + # @return [Hash] pulse + # + # @see https://docs.bitfinex.com/reference#rest-auth-pulse-add + ### + def submit_pulse(pulse) + resp = authenticated_post("auth/w/pulse/add", params: pulse).body + Bitfinex::Models::Pulse.unserialize(resp) + end + + ### + # Submit Pulse message comment + # + # @param [Hash] pulse + # + # @return [Hash] pulse + # + # @see https://docs.bitfinex.com/reference#rest-auth-pulse-add + ### + def submit_pulse_comment(pulse) + resp = authenticated_post("auth/w/pulse/add", params: pulse).body + Bitfinex::Models::Pulse.unserialize(resp) + end + + ### + # Delete Pulse message + # + # @param [string] id pulse id + # + # @return [boolean] true if success, false if error + # + # @see https://docs.bitfinex.com/reference#rest-auth-pulse-del + ### + def delete_pulse(id) + resp = authenticated_post("auth/w/pulse/del", params: { :pid => id }).body + if resp[0] == 1 + return true + end + return false + end + + private + + def deserialize_pulse_with_profile(payload) + pulse = Bitfinex::Models::Pulse.unserialize(payload) + if pulse[:profile].any? + pulse[:profile] = Bitfinex::Models::PulseProfile.unserialize(pulse[:profile][0]) + end + pulse + end + end +end From 1f3a11d7f8efd7b2206e369eeae63f9961358f89 Mon Sep 17 00:00:00 2001 From: Daniel1984 Date: Thu, 30 Jul 2020 13:05:24 +0300 Subject: [PATCH 2/5] bumping version --- bitfinex-rb.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitfinex-rb.gemspec b/bitfinex-rb.gemspec index 2f5ecb8..4682a94 100644 --- a/bitfinex-rb.gemspec +++ b/bitfinex-rb.gemspec @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) Gem::Specification.new do |spec| spec.name = 'bitfinex-rb' - spec.version = '1.0.9' + spec.version = '1.0.10' spec.authors = ['Bitfinex'] spec.email = ['developers@bitfinex.com'] spec.summary = %q{Bitfinex API Wrapper} From b750421ba8adf7ed272d73ecf1a03b0e789ddd55 Mon Sep 17 00:00:00 2001 From: Daniel1984 Date: Thu, 30 Jul 2020 13:19:02 +0300 Subject: [PATCH 3/5] updating docs --- lib/rest/v2/pulse.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rest/v2/pulse.rb b/lib/rest/v2/pulse.rb index 47f5322..134f409 100644 --- a/lib/rest/v2/pulse.rb +++ b/lib/rest/v2/pulse.rb @@ -58,7 +58,8 @@ def submit_pulse(pulse) end ### - # Submit Pulse message comment + # Submit Pulse message comment, requires :parent (pulse id) to + # be present in request payload # # @param [Hash] pulse # From f768b73f110f1c5c047407ac8e9a77421f424cd0 Mon Sep 17 00:00:00 2001 From: Daniel1984 Date: Thu, 20 Aug 2020 07:53:45 +0300 Subject: [PATCH 4/5] raising exception for pulse comments that are missing :parent attribute --- lib/rest/v2/pulse.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/rest/v2/pulse.rb b/lib/rest/v2/pulse.rb index 134f409..4dfba89 100644 --- a/lib/rest/v2/pulse.rb +++ b/lib/rest/v2/pulse.rb @@ -68,6 +68,9 @@ def submit_pulse(pulse) # @see https://docs.bitfinex.com/reference#rest-auth-pulse-add ### def submit_pulse_comment(pulse) + if pulse[:parent].to_s.strip.empty? + raise ":parent (pulse id value) is required for comments" + end resp = authenticated_post("auth/w/pulse/add", params: pulse).body Bitfinex::Models::Pulse.unserialize(resp) end From a921f757c93748f9523eb451aa693ca13e6abd42 Mon Sep 17 00:00:00 2001 From: Daniel1984 Date: Wed, 23 Dec 2020 14:44:32 +0200 Subject: [PATCH 5/5] adding newline --- examples/rest/v2/pulse.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/rest/v2/pulse.rb b/examples/rest/v2/pulse.rb index 1b1e3ae..db6546b 100644 --- a/examples/rest/v2/pulse.rb +++ b/examples/rest/v2/pulse.rb @@ -44,4 +44,4 @@ :content => 'comment 2 1234 5678 Foo Bar Baz Qux Content', :isPublic => 0, :isPin => 1 -}) \ No newline at end of file +})