undefined method `method' for "String":String - ruby-on-rails

I'm currently getting this error:
undefined method `street_address' for "Not Set":String
My goal is to handle an error that occurs when a user's address comes back nil.
Here's the code with where the error occurs:
def address
if params[formatted_address["address"]].nil?
address = ''
street_address = ''
country = ''
region = ''
city = ''
zip = ''
else
address = ActiveSupport::JSON.decode(formatted_address["address"])
street_address = address["street_address"]
country = address["country"]
region = address["region"]
city = address["locality"]
zip = address["postal_code"]
end
end
info do
{
name: user_info.name,
email: user_info.email,
nickname: user_info.preferred_username,
first_name: user_info.given_name,
last_name: user_info.family_name,
gender: user_info.gender,
phone: user_info.phone_number,
birthdate: user_info.birthdate,
street_address: address.street_address, <-- error occurs here
country: address.country,
region: address.region,
city: address.city,
zip: address.zip,
urls: { website: user_info.website }
}
end

in the else statement i just created a replica JSON with the values that i wanted to come back
def address
if formatted_address["address"].nil?
address = JSON.parse('{
"street_address": "",
"country": "",
"region": "",
"locality": "",
"postal_code": ""
}')
else
address = ActiveSupport::JSON.decode(formatted_address["address"])
end
end
info do
{
name: user_info.name,
email: user_info.email,
nickname: user_info.preferred_username,
first_name: user_info.given_name,
last_name: user_info.family_name,
gender: user_info.gender,
phone: user_info.phone_number,
birthdate: user_info.birthdate,
street_address: address["street_address"],
country: address["country"],
region: address["region"],
city: address["locality"],
zip: address["postal_code"],
urls: { website: user_info.website }
}
end

If you want to save the blank spaces in address fields in case of blank address parameters, then you should set defaults to ' ' in the migration/table.

Related

STRIPE NameError: undefined local variable or method `request' for main:Object

I am trying to update a connected account so it can accept the terms of service and be able to create a payout. however, when I try to run the code in the console so it updates it doesn't update and that error regarding the ip address pops up. I am following stripe's documentation to update accounts.
I have tried not including ip into the tos_acceptance, but the ip is neccessary!
I tried 'ip' and putting quotes around request.remote_ip
"acct_id_12345", {
tos_acceptance: {
date: Time.now.to_i,
ip: request.remote_ip,
},
},
)
the error that appears is:
NameError: undefined local variable or method `request' for main:Object
You're running the code somewhere where the request method does not exist, eg. outside of a controller.
I just needed to name the request variable this is the final product:
account = Stripe::Account.create({
country: "US",
type: "custom",
requested_capabilities: ["transfers", "card_payments"],
email: current_user.email,
business_type: "company",
company: {
name: business.name,
address: {
city: business.city,
country: 'US',
line1: business.address,
postal_code: business.zip_code,
state: business.state,
},
phone: '3128880912',
tax_id: '000000000',
},
external_account: {
country: "US",
object: "bank_account",
account_number: params[:account_number],
routing_number: params[:routing_number],
},
settings: {
payouts: {
schedule: {
interval: 'monthly',
monthly_anchor: 1,
},
},
},
business_profile: {
url: business.website,
mcc: 5734,
},
tos_acceptance: {
date: Time.now.to_i,
ip: '181.48.147.209',
},
})

Passing API POST to stripe with business type?

Stripe news API update for Connect accounts doesn't allow the "legal_entity" param for new stripe accounts. The new updated way is for "business_type".. But the issue i have is that I need to pass data from either of the 2 choices for business_type of "individual" or "company.
This is the old way that worked:
acct = Stripe::Account.create({
:country => stripe_account_params[:country],
:type => "custom",
legal_entity: {
first_name: stripe_account_params[:first_name].capitalize,
last_name: stripe_account_params[:last_name].capitalize,
type: stripe_account_params[:account_type],
dob: {
day: stripe_account_params[:dob_day],
month: stripe_account_params[:dob_month],
year: stripe_account_params[:dob_year]
},
address: {
line1: stripe_account_params[:address_line1],
city: stripe_account_params[:address_city],
state: stripe_account_params[:address_state],
postal_code: stripe_account_params[:address_postal]
},
ssn_last_4: stripe_account_params[:ssn_last_4]
},
tos_acceptance: {
date: Time.now.to_i,
ip: request.remote_ip
}
})
The new way (my attempt):
acct = Stripe::Account.create({
:country => stripe_account_params[:country],
:type => "custom",
:business_type => stripe_account_params[:account_type],
requested_capabilities: ['card_payments'],
# company: {
# name: stripe_account_params[:business_name],
# phone: stripe_account_params[:business_phone],
# phone: stripe_account_params[:business_tax_id],
# address: {
# line1: stripe_account_params[:business_address_line1],
# city: stripe_account_params[:business_address_city],
# state: stripe_account_params[:business_address_state],
# postal_code: stripe_account_params[:business_address_postal]
# },
# },
individual: {
address: stripe_account_params[:address_line1],
first_name: stripe_account_params[:first_name],
last_name: stripe_account_params[:last_name],
ssn_last_4: stripe_account_params[:ssn_last_4],
# phone: stripe_account_params[:business_tax_id],
dob: {
day: stripe_account_params[:dob_day],
month: stripe_account_params[:dob_month],
year: stripe_account_params[:dob_year]
},
address: {
line1: stripe_account_params[:address_line1],
city: stripe_account_params[:address_city],
state: stripe_account_params[:address_state],
postal_code: stripe_account_params[:address_postal]
},
},
tos_acceptance: {
date: Time.now.to_i,
ip: request.remote_ip
}
})
With the section i have commented out, not commented out, I get this error:
(If i choose individual with the commented out area, it will work)
I tried simply not defining the address, etc. and loosely having the params and see if Stripe will decide where they go and that didn't work so it seems as if they need to be defined like above but i don't know how to distinguish them.
You cannot provide both company and individual parameters. Only
provide them accordingly with the business_type on the account.
Now, the fields are named the same within stripe:
https://stripe.com/docs/api/accounts/create
So i am unsure how i can pass this through. Any suggestions on how to do this?

Migrating from Mandrill to SparkPost -- Rails API

I am migrating from Mandrill to SparkPost and have a Rails back-end.
The data structure I currently have is the following --
message = {
subject: "Welcome",
merge_vars: [{
rcpt: user.email,
vars: email_vars(user)
}],
to:[{
email: user.email,
name: user.name
}],
track_clicks: true,
track_opens: true,
inline_css: true,
}
This sends the response --
m = Mandrill::API.new
template_content = []
result = m.messages.send_template 'email-confirmation', template_content, message
Would I need to update the JSON data structure at all?
Once JSON is good, how do I pass values to specific template with SparkPost?
I attempted the following --
m = SparkPost::Client.new()
template_content = []
result = m.messages.send_template 'email-confirmation', template_content, message
But I have also seen this --
host = 'https://api.sparkpost.com'
SparkPost::Request.request("#{host}/api/v1/transmissions", API_KEY, {
recipients: [
{
address: { email: user.email },
substitution_data: {
first_name: user.name,
email: user.email
}
}
],
content: {
template_id: 'email-confirmation'
},
substitution_data: {
name: user.name,
email: user.email
}
})
Appreciate the help!
If you're using the official gem, it has a convenient method called send_payload which you can use to send a prepared payload.
The substitution_data inside recipients collection is a per recipient substitution.
For example, I've following templates.
To send using this template, this is my complete code
sp = SparkPost::Client.new() # pass api key or get api key from ENV
payload = {
recipients: [
{
address: { email: 'RECIPIENT1' },
substitution_data: {
name: 'User one',
username: 'userone'
}
}, {
address: { email: 'RECIPIENT2' },
substitution_data: {
name: 'User two',
username: 'user2'
}
}
],
content: {
template_id: 'test-template'
},
substitution_data: {
company: 'Awesome company'
}
}
response = sp.transmission.send_payload(payload)
p response
The email will look like
Hello User one, Your username, userone, is created. Thanks Awesome company

How to convert Json in Ruby on Rails?

I'm creating an API with Rails and Neo4j and i have a query looks like this on my model
Neo4j::Session.query.match('(User)').where("lower(User.first_name) =~ '.*#{params[:name].downcase}.*'").return('User')
this query return json looks like
[
{
User: {
user: {
username: null,
password: null,
first_name: "ayman",
last_name: "eldeeb",
email: "ayman#gmail.com",
phone: "44555",
avatar: "url",
birthdate: "1990-12-26"
}
}
}
]
Now, how to convert this json in Ruby on Rails to this below?
{
users: [
{
id: 0,
username: null,
first_name: "adham",
last_name: "eldeeb",
phone: "010220234",
email: null,
avatar: "url",
birthdate: null
}
]
}
Not sure, but this can probably work..
require 'json' #this has to be in the top
.....
#data = JSON.parse(Neo4j::Session.query.match('(User)').where("lower(User.first_name) =~ '.*#{params[:name].downcase}.*'").return('User'))
Now #data will be a ruby variable parsed from the JSON output..
Now to get your desired output, maybe the following snippet can help
#desired_data = []
#data.each do |d|
#desired_data << d[:User][:user] # this could be d["User"]["user"] if this dosen't work
end

Rewriting Javascript Objects to Dart Maps/Lists

I'm trying to convert the following javascript object to a Dart map:
var users = {
1 : {
first_name: 'James',
last_name: 'Smith',
email: 'jsmith#example.com',
},
2 : {
first_name: 'Robin',
last_name: 'Doe',
email: 'rdoe#example.com',
}
I've tried:
var users = {
"1" : {
first_name: 'James',
last_name: 'Smith',
email: 'jsmith#example.com',
},
"2" : {
first_name: 'Robin',
last_name: 'Doe',
email: 'rdoe#example.com',
}
but I'm unable to use it as a map with the numbers in quotes or without(throws errors).
var keys = users.getKeys(); //NoSuchMethodError : method not found: 'getKeys'
assert(keys.length == 2);
assert(new Set.from(keys).contains('2'));
Use (single or double) quotes for attribute names:
var users = {
"1" : {
"first_name": "James",
"last_name": "Smith",
"email": "jsmith#example.com",
},
"2" : {
"first_name": "Robin",
"last_name": "Doe",
"email": "rdoe#example.com"
}
};
Also, getKeys method does not exists, use keys instead:
var keys = users.keys;
assert(keys.length == 2);
assert(keys.contains("2"));
assert(users["1"]["first_name"] == "James");

Resources