简体   繁体   中英

Non specific MVC framework ruby gems and example code to Rails framework

I have been learning Ruby on Rails, but I still have issues when it comes to Ruby gems with examples that are irb based and not Rails or Sinatra framework based. I am trying to implement the Block.io Bitcoin API functionality. But the code I find is Ruby only, so I am not sure where to create a config file for the API Key and also whether I need to create a controller to make this work in the views for Rails.

The gem and examples are on: https://github.com/BlockIo/gem-block-io

I installed this gem via bundle install on Rails

gem install block_io -v=1.0.6

The Ruby example show the following:

>> require 'block_io'
>> BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2

In Rails which config file would I enter the above api_key and pin?

In the example they show the code to get your address as follows:

BlockIo.get_my_address

Do I need to create a function in a controller such as:

def address
 @my_address = BlockIo.get_my_addresses
end

and in the view use:

<%= @my_address %>

I need some guidance with regards to the above, any comment or assistance will be greatly appreciated.

require 'block_io' can go into Gemfile like gem 'block_io' . Rails/bundler will require it automaticaly for you as long as the gem name is also the file name you want to require from this gem.

BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2 can be put into an initilizer like config/initializers/block_io.rb . This way set_options is called only once when Rails starts a server or console or runner.

Put it like this into the file config/initializers/block_io.rb

BlockIo.set_options :api_key=> ENV['BLOCK_IO_API_KEY'], :pin => ENV['BLOCK_IO_PIN'], :version => 2

With the environment variables in use you don't commit any secret into your repo.

Now you should be able to call BlockIo.get_my_address within any action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM