Skip to content

Commit

Permalink
Merge pull request #37 from robertkowalski/put-get
Browse files Browse the repository at this point in the history
BEP44: put & get
  • Loading branch information
prdn authored Dec 28, 2017
2 parents 744299c + c61938a commit 7cc3545
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
.DS_Store
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: ruby
rvm:
- ruby

install:
- bundle install

script:
- bundle exec rspec
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ Sends a single request to a RPC server/worker.
[Example](https://github.com/bitfinexcom/grenache-ruby-http/blob/master/examples/client.rb).


### client.put(data, options)

- `data`
- `:v`: <String> value to store
- `options`
- `:timeout` Timeout for the request

Puts a value into the DHT.

### client.get(hash)
- `hash` <String> Hash of the data to receive

Retrieves a stored value from the DHT via a `hash` <String>.

### client.listen(key, port, options)
- `name` <String> Name of the service to announce
- `port` <Number> Port to listen
Expand Down
2 changes: 1 addition & 1 deletion examples/client.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative '../lib/grenache-ruby-http.rb'


c = Grenache::Http.new grape_address: "http://127.0.0.1:40002/"
c = Grenache::Http.new grape_address: "http://127.0.0.1:30001/"

start_time = Time.now

Expand Down
18 changes: 18 additions & 0 deletions examples/put_get.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require_relative '../lib/grenache-ruby-http.rb'

c = Grenache::Http.new grape_address: "http://127.0.0.1:30001/"

err,res_hash = c.put({ "v" => "pineapple test" })
if !err

puts "response, hash is: #{res_hash}"
else
puts err
end

err,resp = c.get(res_hash)
if !err
puts "response of #{res_hash}: #{resp}"
else
puts err
end
2 changes: 1 addition & 1 deletion examples/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }

c = Grenache::Http.new grape_address: "http://127.0.0.1:40002/"
c = Grenache::Http.new grape_address: "http://127.0.0.1:30001/"

c.listen('rpc_test', 5004) do |msg|
#[StandardError.new("Error!"),"hello #{msg.payload}"]
Expand Down
1 change: 1 addition & 0 deletions grenache-ruby-http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "oj", "~> 2.18"
spec.add_runtime_dependency "thin", "~> 1.7"

spec.add_development_dependency "rspec", "~> 3.5.0"
end
14 changes: 14 additions & 0 deletions lib/grenache/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ def request(key, payload, params={})
return [e, nil]
end

def put(payload, params={})
resp = link.send('put', payload, params)
return [nil, resp]
rescue Exception => e
return [e, nil]
end

def get(t_hash, params={})
resp = link.send('get', t_hash, params)
return [nil, Oj.dump(resp)]
rescue Exception => e
return [e, nil]
end

private

def tls?
Expand Down

0 comments on commit 7cc3545

Please sign in to comment.