-2

The following JSON is a transaction what will be sent to the Ripple Network to query accounts that hold cryptographic assets at a Gateway (somewhat like a bank, more like a trust account between its clients). This script is to be used in conjunction with PHP to fetch a Gateway's issued balances and ignored it's hot-wallet or day-to-day operations wallet. My question is what is the proper way to:

a. Assign JSON within a Ruby variable?

b. What is the best way to escape double quotes and deal with newlines where brackets and square brackets occur within the JSON syntax?

The JSON follows:

ripple_path="/home/rippled/build/rippled" 
conf = "--conf /etc/rippled/rippled.cfg"

puts "About to set the JSON lines "

gatewayStart = "\"method\": \"gateway_balances\","

paramsLine = "\"params\": [ {"

accountLine = "\"account\": \"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH\","

hotwalletLine = "\"hotwallet\": \"rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C\","

liLine = "\"ledger_index\": \"validated\","

strictLine = "\"strict\": "

trueLine = true

endLine = " } ] }"

balancesLine = "#{gatewayStart} #{paramsLine} #{accountLine} #>{hotwalletLine} #{liLine} #{strictLine} #{trueLine} #{endLine}"

lineString = "#{balancesLine.to_s}"

linetoJSON = "#{lineString}"

puts "linetoJSON: #{linetoJSON} "

cmd2=`#{ripple_path} #{conf} json gateway_balances #{linetoJSON}`

cmder="#{ripple_path} #{conf} json gateway_balances  #{linetoJSON}"

puts "Done."

The output is:

root@xagate:WorkingDirectory# ruby gatewaybal.rb 

About to set the JSON lines 

linetoJSON: "method": "gateway_balances", "params": [ { "account": 
"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH", "hotwallet":         "rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C", "ledger_index": "validated", "strict":rue  } ] } 

Loading: "/etc/rippled/rippled.cfg"

rippled [options] <command> <params>

General Options:

  -h [ --help ]         Display this message. 

.....

Done.

It is noteworthy that this command also returns a badSyntax error when executed manually via the command line. Please see here for the mirror of this issue raised on the ripple forums.

user2984057
  • 65
  • 1
  • 2
  • 9
  • The way to go is to assign your data via a hash that can easily be converted to json when needed. In an api for instance. – davegson Sep 18 '15 at 14:21
  • And what is up with that code? :O Why would you work like that? – davegson Sep 18 '15 at 14:23
  • @TheCha͢mp Because I have worked with ruby for ~3 weeks, that is why I am here asking for some help. Can you please provide a more detailed answer? – user2984057 Sep 18 '15 at 14:25
  • http://www.tutorialspoint.com/ruby/ruby_hashes.htm - I would also suggest the [ruby course on codecadamy](http://codecademy.com/en/tracks/ruby) – davegson Sep 18 '15 at 14:28
  • I think I get what you are inferring: json = Hash["gatewayStart" => "\"method\": \"gateway_balances\",", paramsLine=> paramsLine = "\"params\": [ {"] My next questions are how do I know if a hash would throw an error due to its datatype when called? How do you ensure that when you call the full hash that it can be converted to JSON? Also, what about the portion of my question where I asked about escaping double quotes and the newlines within the JSON? – user2984057 Sep 18 '15 at 14:33
  • There's plenty of information on [the internet](http://stackoverflow.com/questions/3183786/how-to-convert-a-ruby-hash-object-to-json) - you will have to be willing to search for it. Also, I highly recommend that codecadamy course – davegson Sep 18 '15 at 14:36
  • 1
    @TheCha͢mp I have worked through about half of the codecadamy ruby course to create [this package](https://github.com/whotooktwarden/rippled-sign-submit), so I guess I really should just finish it over this weekend. Thanks for pointing me in the right direction. Also what was with the 4 down-votes, don't you people know most of us are here to learn? C'mon now. – user2984057 Sep 18 '15 at 14:42
  • 2
    The downvotes are not personal. It's describing the content. It's like asking Jamie Oliver how to start an oven. It's totally fine to be a beginner - but at the same time you have to show some effort in your research. In this case that was not the case ;) – davegson Sep 18 '15 at 14:44

1 Answers1

0
jsonLine = "'{ \"account\": \"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH\", \"hotwallet\": \"rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C\", \"ledger_index\": \"validated\", \"strict\":  true  }'"

Is the proper way to assign this JSON within a single variable; this solution was provided by JoelKatz. The completed code is now available on GitHub.

user2984057
  • 65
  • 1
  • 2
  • 9