Skip to content

Commit

Permalink
Merge pull request #13 from theodi/return-json
Browse files Browse the repository at this point in the history
Return JSON on the console
  • Loading branch information
pezholio committed Jan 7, 2016
2 parents af2acd3 + 08780a5 commit 396c1e1
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 16 deletions.
41 changes: 25 additions & 16 deletions lib/multichain/cli.rb
Expand Up @@ -37,24 +37,33 @@ def verify hex
end

desc 'send_url recipient, URL', 'Send a URL to a recipient'
method_option :json,
type: :boolean,
aliases: '-j',
description: 'Return JSON on the console'
def send_url recipient, url
data = Client.new.send_url recipient, url
out = "You sent '#{url}' to '#{recipient}'\n"
out << "\n"
out << "The transaction id is\n"
out << " #{data[:id]}\n"
out << "\n"
out << "The URL\n"
out << " #{url}\n"
out << "hashed to\n"
out << " #{data[:hash]}\n"
out << "at\n"
out << " #{Time.at(data[:timestamp].to_i).to_datetime}\n"
out << "\n"
out << "Verify the hash with\n"
out << " multichain verify #{data[:hex]}\n"

puts out

if options[:json]
puts data.to_json
else
out = "You sent '#{data[:url]}' to '#{data[:recipient]}'\n"
out << "\n"
out << "The transaction id is\n"
out << " #{data[:id]}\n"
out << "\n"
out << "The URL\n"
out << " #{url}\n"
out << "hashed to\n"
out << " #{data[:hash]}\n"
out << "at\n"
out << " #{Time.at(data[:timestamp].to_i).to_datetime}\n"
out << "\n"
out << "Verify the hash with\n"
out << " multichain verify #{data[:hex]}\n"

puts out
end
end
end
end
24 changes: 24 additions & 0 deletions spec/multichain/cli_spec.rb
Expand Up @@ -92,5 +92,29 @@ module Multichain
""")
end
end

context 'send URL and return JSON' do
let :output do
capture :stdout do
Timecop.freeze Time.local 2016, 01, 06, 16, 12 do
subject.options = {json: true}
subject.send_url 'stu', 'http://uncleclive.herokuapp.com/multichain'
end
end
end

it 'sends a URL to a wallet and returns JSON', :vcr do
expect(output).to match (
{
recipient: 'stu',
url: 'http://uncleclive.herokuapp.com/multichain',
hash: '32fffb8bdf96b34ad87649fd89ebca4e6a251db396e56fd4ea244ff3942941c8',
timestamp: '1452096720',
hex: '313435323039363732307c687474703a2f2f756e636c65636c6976652e6865726f6b756170702e636f6d2f6d756c7469636861696e7c7b22416363657074223a226170706c69636174696f6e2f6a736f6e227d7c33326666666238626466393662333461643837363439666438396562636134653661323531646233393665353666643465613234346666333934323934316338',
id: 'ef10c7b72b19c001ae38a3d4c5bc9f0a5b1adddc4b09088e99e473191820e86b'
}.to_json
)
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 396c1e1

Please sign in to comment.