Skip to content

Commit 5f30386

Browse files
authored
Merge pull request #98 from Daniel1984/feature/restv2-pulse-support
restv2 adding pulse resource support
2 parents cac18d9 + a921f75 commit 5f30386

File tree

8 files changed

+233
-1
lines changed

8 files changed

+233
-1
lines changed

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
1.0.10
2+
- Adds function rest/v2 get_pulse_profile
3+
- Adds function rest/v2 get_public_pulse_history
4+
- Adds function rest/v2 submit_pulse
5+
- Adds function rest/v2 delete_pulse
6+
- Adds function rest/v2 get_private_pulse_history
7+
- Adds function rest/v2 submit_pulse_comment
8+
19
1.0.9
210

311
- Fixes bug with API v2 transfer

bitfinex-rb.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
44

55
Gem::Specification.new do |spec|
66
spec.name = 'bitfinex-rb'
7-
spec.version = '1.0.9'
7+
spec.version = '1.0.10'
88
spec.authors = ['Bitfinex']
99
spec.email = ['[email protected]']
1010
spec.summary = %q{Bitfinex API Wrapper}

examples/rest/v2/pulse.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require_relative '../../../lib/bitfinex.rb'
2+
3+
client = Bitfinex::RESTv2.new({
4+
:url => ENV['REST_URL'],
5+
:api_key => ENV['API_KEY'],
6+
:api_secret => ENV['API_SECRET']
7+
})
8+
9+
# Get pulse profile
10+
p client.get_pulse_profile('Bitfinex')
11+
12+
# Get public pulse history
13+
p client.get_public_pulse_history({ :limit => 5 })
14+
15+
# Submit new Pulse message
16+
pulse = client.submit_pulse({
17+
:title => '1234 5678 Foo Bar Baz Qux TITLE',
18+
:content => '1234 5678 Foo Bar Baz Qux Content',
19+
:isPublic => 0,
20+
:isPin => 1
21+
})
22+
p pulse
23+
24+
# Delete Pulse message
25+
p "About to delete pulse: #{pulse[:id]}"
26+
p client.delete_pulse(pulse[:id])
27+
28+
# Get private pulse history
29+
p client.get_private_pulse_history()
30+
31+
# Submit Pulse message comment
32+
# 1 - create pulse message
33+
pulse2 = client.submit_pulse({
34+
:title => '2 1234 5678 Foo Bar Baz Qux TITLE',
35+
:content => '2 1234 5678 Foo Bar Baz Qux Content',
36+
:isPublic => 0,
37+
:isPin => 1
38+
})
39+
40+
# 2 - submit comment for above pulse message
41+
p client.submit_pulse_comment({
42+
:parent => pulse2[:id],
43+
:title => 'comment 2 1234 5678 Foo Bar Baz Qux TITLE',
44+
:content => 'comment 2 1234 5678 Foo Bar Baz Qux Content',
45+
:isPublic => 0,
46+
:isPin => 1
47+
})

lib/bitfinex.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@
3434
require_relative './models/trading_ticker'
3535
require_relative './models/user_info'
3636
require_relative './models/wallet'
37+
require_relative './models/pulse_profile'
38+
require_relative './models/pulse'

lib/models/pulse.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require_relative './model'
2+
3+
module Bitfinex
4+
module Models
5+
class Pulse < Model
6+
BOOL_FIELDS = []
7+
FIELDS = {
8+
:id => 0,
9+
:mts_create => 1,
10+
:pulse_user_id => 3,
11+
:title => 5,
12+
:content => 6,
13+
:is_pin => 9,
14+
:is_public => 10,
15+
:comments_disabled => 11,
16+
:tags => 12,
17+
:attachments => 13,
18+
:meta => 14,
19+
:likes => 15,
20+
:profile => 18,
21+
:comments => 19
22+
}
23+
24+
FIELDS.each do |key, index|
25+
attr_accessor key
26+
end
27+
28+
def initialize (data)
29+
super(data, FIELDS, BOOL_FIELDS)
30+
end
31+
32+
def self.unserialize (data)
33+
Model.unserialize(data, FIELDS, BOOL_FIELDS)
34+
end
35+
end
36+
end
37+
end

lib/models/pulse_profile.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require_relative './model'
2+
3+
module Bitfinex
4+
module Models
5+
class PulseProfile < Model
6+
BOOL_FIELDS = []
7+
FIELDS = {
8+
:id => 0,
9+
:mts_create => 1,
10+
:nickname => 3,
11+
:picture => 5,
12+
:text => 6,
13+
:twitter_handle => 9,
14+
:followers => 11,
15+
:following => 12
16+
}
17+
18+
FIELDS.each do |key, index|
19+
attr_accessor key
20+
end
21+
22+
def initialize (data)
23+
super(data, FIELDS, BOOL_FIELDS)
24+
end
25+
26+
def self.unserialize (data)
27+
return Model.unserialize(data, FIELDS, BOOL_FIELDS)
28+
end
29+
end
30+
end
31+
end

lib/rest/v2.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require_relative './v2/wallet'
1010
require_relative './v2/funding'
1111
require_relative './v2/positions'
12+
require_relative './v2/pulse'
1213

1314
module Bitfinex
1415
class RESTv2
@@ -27,6 +28,7 @@ class RESTv2
2728
include Bitfinex::RESTv2Wallet
2829
include Bitfinex::RESTv2Funding
2930
include Bitfinex::RESTv2Positions
31+
include Bitfinex::RESTv2Pulse
3032

3133
def initialize(args = {})
3234
self.api_endpoint = args[:url] ? "#{args[:url]}/v2/" : "https://api.bitfinex.com/v2/"

lib/rest/v2/pulse.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
module Bitfinex
2+
module RESTv2Pulse
3+
###
4+
# Get Pulse Profile
5+
#
6+
# @param [string] nickname Nickname of pulse profile
7+
#
8+
# @return [Hash] the Pulse Profile
9+
#
10+
# @see https://docs.bitfinex.com/reference#rest-public-pulse-profile
11+
###
12+
def get_pulse_profile(nickname)
13+
resp = get("pulse/profile/#{nickname}").body
14+
Bitfinex::Models::PulseProfile.unserialize(resp)
15+
end
16+
17+
###
18+
# Get Public Pulse History
19+
#
20+
# @param [int] end (optional) Return only the entries after this timestamp
21+
# @param [int] limit (optional) Limit the number of entries to return. Default is 25.
22+
#
23+
# @return [Array] public pulse message
24+
#
25+
# @see https://docs.bitfinex.com/reference#rest-public-pulse-hist
26+
###
27+
def get_public_pulse_history(params = {})
28+
pulses = get("pulse/hist", params).body
29+
pulses.map { |p| deserialize_pulse_with_profile(p) }
30+
end
31+
32+
###
33+
# Get Private Pulse History
34+
#
35+
# @param [int] isPublic allows to receive the public pulse history with the UID_LIKED field
36+
#
37+
# @return [Array] private pulse message
38+
#
39+
# @see https://docs.bitfinex.com/reference#rest-auth-pulse-hist
40+
###
41+
def get_private_pulse_history(params = {})
42+
pulses = authenticated_post("auth/r/pulse/hist", params).body
43+
pulses.map { |p| deserialize_pulse_with_profile(p) }
44+
end
45+
46+
###
47+
# Submit new Pulse message
48+
#
49+
# @param [Hash] pulse
50+
#
51+
# @return [Hash] pulse
52+
#
53+
# @see https://docs.bitfinex.com/reference#rest-auth-pulse-add
54+
###
55+
def submit_pulse(pulse)
56+
resp = authenticated_post("auth/w/pulse/add", params: pulse).body
57+
Bitfinex::Models::Pulse.unserialize(resp)
58+
end
59+
60+
###
61+
# Submit Pulse message comment, requires :parent (pulse id) to
62+
# be present in request payload
63+
#
64+
# @param [Hash] pulse
65+
#
66+
# @return [Hash] pulse
67+
#
68+
# @see https://docs.bitfinex.com/reference#rest-auth-pulse-add
69+
###
70+
def submit_pulse_comment(pulse)
71+
if pulse[:parent].to_s.strip.empty?
72+
raise ":parent (pulse id value) is required for comments"
73+
end
74+
resp = authenticated_post("auth/w/pulse/add", params: pulse).body
75+
Bitfinex::Models::Pulse.unserialize(resp)
76+
end
77+
78+
###
79+
# Delete Pulse message
80+
#
81+
# @param [string] id pulse id
82+
#
83+
# @return [boolean] true if success, false if error
84+
#
85+
# @see https://docs.bitfinex.com/reference#rest-auth-pulse-del
86+
###
87+
def delete_pulse(id)
88+
resp = authenticated_post("auth/w/pulse/del", params: { :pid => id }).body
89+
if resp[0] == 1
90+
return true
91+
end
92+
return false
93+
end
94+
95+
private
96+
97+
def deserialize_pulse_with_profile(payload)
98+
pulse = Bitfinex::Models::Pulse.unserialize(payload)
99+
if pulse[:profile].any?
100+
pulse[:profile] = Bitfinex::Models::PulseProfile.unserialize(pulse[:profile][0])
101+
end
102+
pulse
103+
end
104+
end
105+
end

0 commit comments

Comments
 (0)