Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restv2 adding pulse resource support #98

Merged
merged 5 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion bitfinex-rb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['[email protected]']
spec.summary = %q{Bitfinex API Wrapper}
Expand Down
47 changes: 47 additions & 0 deletions examples/rest/v2/pulse.rb
Original file line number Diff line number Diff line change
@@ -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({
Daniel1984 marked this conversation as resolved.
Show resolved Hide resolved
: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
})
2 changes: 2 additions & 0 deletions lib/bitfinex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
37 changes: 37 additions & 0 deletions lib/models/pulse.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions lib/models/pulse_profile.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/rest/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_relative './v2/wallet'
require_relative './v2/funding'
require_relative './v2/positions'
require_relative './v2/pulse'

module Bitfinex
class RESTv2
Expand All @@ -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/"
Expand Down
105 changes: 105 additions & 0 deletions lib/rest/v2/pulse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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, requires :parent (pulse id) to
# be present in request payload
#
# @param [Hash] pulse
#
# @return [Hash] 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

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