From this http://www.rubydoc.info/github/Shopify/active_merchant/ActiveMerchant%2FBilling%2FBase.gateway
I should just initialize an instance of active_merchant using this
gateway = ActiveMerchant::Billing::Base.gateway( gateway_name ).new(
:username => :some_credential
:password => :some_other_credential
)
But I don't know :username or :password in advance, however they are in the fixtures file https://github.com/activemerchant/active_merchant/blob/master/test/fixtures.yml here. So how to do this properly?
For an example, in the fixtures.yml file we can see this..
adyen:
username: ''
password: ''
merchant_account: ''
Accordingly we can initialize with this..
gateway = ActiveMerchant::Billing::Base.gateway( 'adien' ).new(
username: => :some_credential
password: => :some_other_credential
merchant_account: => some_more_credential
)
I need to be able to initialize the gateway instance without hard-coding the username:, password: and merchant_account: parameters in the above example.
Thanks in advance!
You should take a look at environment variables. They let you define variables in a safe palce and refer to them when needed.
Somewhere you would define PASSWORD=mysecretpassword and then in the Rails code you refer to it as ENV["PASSWORD"]
There are many ways of doing this. Take a look here:
http://railsapps.github.io/rails-environment-variables.html
The username, password, and merchant_account are instance variables so you need to change them with instance_variable_set
gateway.instance_variable_set(:#username, :some_username)
gateway.instance_variable_set(:#password, :some_password)
gateway.instance_variable_set(:#merchant_account, :some_merchant_account)
I think i can answer my own question. One should pass every different initialization with its own hash..
require 'active_merchant'
settings = {name: 'braintree_blue', merchant_id: 'x', public_key: 'y', private_key: 'z'}
another_settings = {name: 'trust_commerce', login: 'TestMerchant', password: 'password'}
Gateway = ActiveMerchant::Billing::Base::gateway(settings[:name]).new(settings)
Another_Gateway = ActiveMerchant::Billing::Base::gateway(another_settings[:name]).new(another_settings)
puts Gateway.options
puts Another_Gateway.options
Note that I could pass different options without the need to hardcode any keys, which is what i desired.
Related
Ideally I want to create a fake email based on the Faker generated email, but I want to achieve something like: faker_first_name#mydomain.com. The documentation shows you can do it for the first part but not the actual domain. Is there a way to achieve this?
20.times do
u = User.new(first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
email: Faker::Name.first_name"#THISPART.com",
)
u.save
end
Update Dec 2019:
Faker version v2.8.0 introduced the domain support - Release v2.8.0
Now, It is possible to pass the domain while creating the email address.
Following are the possible options:
Faker::Internet.email #=> "eliza#mann.net"
Faker::Internet.email(name: 'Nancy') #=> "nancy#terry.biz"
Faker::Internet.email(name: 'Janelle Santiago', separators: '+') #=> janelle+santiago#becker.com"
Faker::Internet.email(domain: 'example.com') #=> alice#example.com"
Note: Above code sample is from the faker documentation
Old Answer:
Well there is no such provision to pass domain name to the method
But, you can make use of Faker::Internet.user_name
User.new(
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
email: "#{Faker::Internet.user_name}#customdomain.com"
)
I think you just missed the string concat: +
:006 > Faker::Name.first_name+"#THISPART.com"
=> "Irving#THISPART.com"
And if you meant keeping the same name, save it before:
fn = Faker::Name.first_name
sn = Faker::Name.last_name
u = User.create(
:forename => fn,
:surname => sn,
:email => "#{fn}.#{sn}#yourdomain.net",
Faker::Name.first_name will always generate a new random value.
Recent versions of Faker has a built in support for custom email subdomains.
Faker::Internet.email(domain: 'customdomain.com')
In our program, each customer gets their own database. We e-mail them a link that connects them to their database. The link contains a GUID that lets the program know which database to connect to.
How do I dynamically and programatically connect ActiveRecord to the right db?
You can also do this easily without hardcoding anything and run migrations automatically:
customer = CustomerModel.find(id)
spec = CustomerModel.configurations[RAILS_ENV]
new_spec = spec.clone
new_spec["database"] = customer.database_name
ActiveRecord::Base.establish_connection(new_spec)
ActiveRecord::Migrator.migrate("db/migrate_data/", nil)
I find it useful to re-establish the old connection on a particular model afterwards:
CustomerModel.establish_connection(spec)
you can change the connection to ActiveRecord at any time by calling ActiveRecord::Base.establish_connection(...)
IE:
ActiveRecord::Base.establish_connection({:adapter => "mysql", :database => new_name, :host => "olddev",
:username => "root", :password => "password" })
It's been a while since this question has been created, but I have to say that there is another way too:
conn_config = ActiveRecord::Base.connection_config
conn_config[:database] = new_database
ActiveRecord::Base.establish_connection conn_config
class Database
def self.development!
ActiveRecord::Base.establish_connection(:development)
end
def self.production!
ActiveRecord::Base.establish_connection(ENV['PRODUCTION_DATABASE'])
end
def self.staging!
ActiveRecord::Base.establish_connection(ENV['STAGING_DATABASE'])
end
end
And in .env (with dotenv-rails gem for instance):
PRODUCTION_DATABASE=postgres://...
STAGING_DATABASE=postgres://...
And now you can:
Database.development!
User.count
Database.production!
User.count
Database.staging!
User.count
# etc.
I have been using Omniauth to retrieve an environment that contains the steam username.
The output looks like this:
<OmniAuth::AuthHash::InfoHash image="https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d4/d45a66fee7932d270ec32d4457d865b485245cf1_medium.jpg" location="YT, CA" name="Daiki" nickname="Corybantic Walrus" urls=#<OmniAuth::AuthHash FriendList=#<URI::HTTP:0x0000000614f590 URL:http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=4A8837BF7A6C439681B57F4962D8B011&steamid=76561198128055024&relationship=friend> Profile="http://steamcommunity.com/id/hatterkiller/">> provider="steam" uid="76561198128055024">
I don't really know how to format this inside Stack Overflow, so here is a cleaner Pastebin.
The information I need is inside the <Omniauth::AuthHash::Infohash thing of the code. Is there a way to use Ruby to retrieve the username (nickname) and to put it inside an array?
Sort of like this, but this only worked for the previous output format:
auth = request.env['omniauth.auth']
session[:current_user] = { :nickname => auth.info['nickname'],
:image => auth.info['image'],
:uid => auth.uid }
Something like this:
session[:current_user] = {
nickname: auth[:info][:nickname],
image: auth[:info][:image],
uid: auth[:uid]
}
I have 1000 users that i will be retrieving from Twitter, and I would like to save them at one shot, as opposed to doing 1000 insertions individually.
How can I do this on Mongoid? Something like this would rock:
TwitterUser.createMany([{:name=>u1}, {:name=>u2},{:name=>u3}] )
You should use the Mongo ruby driver to do this. You can pass an array of hashes to the insert method to create multiple documents at once (more info on this google groups discussion). Mongoid makes it easy to access the ruby driver.
The code would look something like this:
user_list = twitter_accounts.map do |account|
# create a hash of all the fields to be stored in each document
{ 'name' => account.name,
'username' => account.username
# some other fields...
}
end
Mongoid.master['twitter_users'].insert(user_list)
You almost got it, it's create, not createMany. You can use it like this:
TwitterUser.create([
{ username: "u1", display_name: "Display Name 1" },
{ username: "u2", display_name: "Display Name 2" },
{ username: "u3", display_name: "Display Name 3" }
])
Also, as #bowsersenior points out, it's a good idea to use it with Array#Map:
TwitterUser.create(
#users_array.map do |u|
{ username: u.username, display_name: u.name }
end
)
From the Mongoid#Persistence Docs:
Model.create
Insert a document or multiple documents into the database
Model.create!
Insert a document or multiple documents into the database, raising an error if a validation error occurs.
Just use MongoidModel.create directly.
In our program, each customer gets their own database. We e-mail them a link that connects them to their database. The link contains a GUID that lets the program know which database to connect to.
How do I dynamically and programatically connect ActiveRecord to the right db?
You can also do this easily without hardcoding anything and run migrations automatically:
customer = CustomerModel.find(id)
spec = CustomerModel.configurations[RAILS_ENV]
new_spec = spec.clone
new_spec["database"] = customer.database_name
ActiveRecord::Base.establish_connection(new_spec)
ActiveRecord::Migrator.migrate("db/migrate_data/", nil)
I find it useful to re-establish the old connection on a particular model afterwards:
CustomerModel.establish_connection(spec)
you can change the connection to ActiveRecord at any time by calling ActiveRecord::Base.establish_connection(...)
IE:
ActiveRecord::Base.establish_connection({:adapter => "mysql", :database => new_name, :host => "olddev",
:username => "root", :password => "password" })
It's been a while since this question has been created, but I have to say that there is another way too:
conn_config = ActiveRecord::Base.connection_config
conn_config[:database] = new_database
ActiveRecord::Base.establish_connection conn_config
class Database
def self.development!
ActiveRecord::Base.establish_connection(:development)
end
def self.production!
ActiveRecord::Base.establish_connection(ENV['PRODUCTION_DATABASE'])
end
def self.staging!
ActiveRecord::Base.establish_connection(ENV['STAGING_DATABASE'])
end
end
And in .env (with dotenv-rails gem for instance):
PRODUCTION_DATABASE=postgres://...
STAGING_DATABASE=postgres://...
And now you can:
Database.development!
User.count
Database.production!
User.count
Database.staging!
User.count
# etc.