Ruby/Highrise API - Not saving phone number? - ruby

So I've been using this same block of code for about 9 months and suddenly it has stopped saving a phone number into Highrise when a new Lead is generated... Any idea where things are going sideways? I never throws an error and happily saves the contact with everything except for the telephone number.
# create a contact Highrise from supplied information
#person = Highrise::Person.new(first_name: #lead.first_name.humanize,
last_name: #lead.last_name.humanize,
background: "automatically created by LSAL app",
contact_data: { email_addresses: [ { address: #lead.email, location: "Home" } ],
phones: [ { number: #lead.phone, location: "Mobile" } ] } )
#person.save # save contact

#person = Highrise::Person.create(:company_name => account.company, :first_name => account.first_name, :last_name => account.last_name, :contact_data => { :email_addresses => [{ :email_address => {:address => account.email, } }], :addresses => [{:address => {:city => account.city, :location =>"Work", :state => account.state, :country => account.country,:street => "#{account.address1} #{account.address2}" , :zip => account.zip} }], :phone_numbers => [{ :phone_number => { :number => number, :location =>"Work"}}] } )
Hope its helpful

Related

Fedex Ruby Gem: Customs Value is required - how to add?

I'm working with the Ruby gem 'FedEx', https://github.com/jazminschroeder/fedex.
I've set up my code for a development mode and I'm testing making a shipment.
However, I get stuck with the following error:
C:/Ruby22/lib/ruby/gems/2.2.0/gems/fedex-.10.1/lib/fedex/request/shipment.rb:134:in 'failure_response': Customs Value is required. (Fedex:: RateError) from C: /Ruby22/lib/ruby/gems/2.2.0/gems/fedex-.10.1/lib/fedex/request/shipment.rb:32:in 'process_request' from C: /Ruby22/lib/ruby/gems/2.2.0/gems/fedex-3.10.1/lib/fedex/shipment.rb:57:in 'ship' from C: /Ruby22/bin/css_fedex_v1.rb:92:in ''
It seems that I need to parse a 'Customs Value', probably as part of my 'packages' hash. However, I'm unable to find the relevant field for me to enter this in. Anyone who's experienced this and found a solution?
My code is as below:
require 'fedex'
fedex = Fedex::Shipment.new(:key => '***',
:password => '***',
:account_number => '***',
:meter => '***',
:mode => 'development')
shipper = { :name => "***",
:company => "***",
:phone_number => "***",
:address => "***",
:city => "***",
:postal_code => "***",
:country_code => "DK" }
recipient = { :name => "***",
:company => "***",
:phone_number => "***",
:address => "***",
:city => "***",
:postal_code => "***",
:country_code => "GB",
:residential => "false" }
packages = []
packages << {:weight => {:units => "LB", :value => 1}}
shipping_options = {:packaging_type => "YOUR_PACKAGING",
:drop_off_type => "REGULAR_PICKUP"}
rate = fedex.rate(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:shipping_options => shipping_options)
ship = fedex.ship(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:service_type => "INTERNATIONAL_PRIORITY",
:shipping_options => shipping_options)
puts ship[:completed_shipment_detail][:operational_detail][:transit_time]
Customs value is declared in their docs:
https://github.com/jazminschroeder/fedex/commit/9f1d4c67b829aaa4eeba9090c1a45d3bd507aab3#diff-4f122efb7c0d98120d8b7f0cd00998e4R106
customs_value = { :currency => "USD",
:amount => "200" }
As I understand you can pass it into the commodities hash or keep it separate.

How to make a Ruby application to send money to specific users?

I'm a Ruby user, who is trying to make a Ruby application that allows me to pay to my friends. My friends have Paypal account individually (i.e. sungpahfriend#hello.com ). I have a particular amount of money that I have to pay to my friends.
I have a business account. ( sungpah#hello.com ).
But I can't find how I can pay to my friend.
Do I have to put my credit card information or just my email address and amount of money? Also where should I put my friend's email address?
I really want to know how to this effectively. I'm looking forward to getting a feedback!
Best
require 'paypal-sdk-rest'
PayPal::SDK::Core::Config.load('config/paypal.yml', ENV['RACK_ENV'] || 'development')
PayPal::SDK.logger = Logger.new(STDERR)
PayPal::SDK.logger.level = Logger::INFO
PayPal::SDK.configure({
:mode => "sandbox",
:client_id => "xxx-xx",
:client_secret => "xx-xx"
})
#payment = PayPal::SDK::REST::Payment.new({
:intent => "sale",
:payer => {
:payment_method => "credit_card",
:funding_instruments => [{
:credit_card => {
:type => "visa",
:number => "4567516310777851",
:expire_month => "11",
:expire_year => "2018",
:cvv2 => "874",
:first_name => "Joe",
:last_name => "Shopper",
:billing_address => {
:line1 => "52 N Main ST",
:city => "Johnstown",
:state => "OH",
:postal_code => "43210",
:country_code => "US" }}}]},
:transactions => [{
:item_list => {
:items => [{
:name => "item",
:sku => "item",
:price => "1",
:currency => "USD",
:quantity => 1 }]},
:amount => {
:total => "1.00",
:currency => "USD" },
:description => "This is the payment transaction description." }]})
# Create Payment and return the status(true or false)
if #payment.create
#payment.id # Payment Id
else
#payment.error # Error Hash
end
This might be a good case for the Payouts API, which is also supported by the Ruby SDK.
To fund the payout with minimal fees, you'll want to have the payout amount already in your PayPal account or link to your bank account.

Nested hash iteration: How to iterate a merge over an ( (array of hashes) within a hash )

I'm trying to do as the title says. Here is my code:
school.each { |x| school[:students][x].merge!(semester:"Summer") }
I think I pinpointed the problem to the "[x]" above. If I substitute an array position such as "[2]" it works fine. How can make the iteration work?
If the info above is not enough or you'd like to offer a better solution, please see the details below. Thanks!
The error message I get:
file.rb:31:in []': no implicit conversion of Array into Integer (TypeError)
from file.rb:31:inblock in '
from file.rb:31:in each'
from file.rb:31:in'
The nested hash below before alteration:
school = {
:name => "Happy Funtime School",
:location => "NYC",
:instructors => [
{:name=>"Blake", :subject=>"being awesome" },
{:name=>"Ashley", :subject=>"being better than blake"},
{:name=>"Jeff", :subject=>"karaoke"}
],
:students => [
{:name => "Marissa", :grade => "B"},
{:name=>"Billy", :grade => "F"},
{:name => "Frank", :grade => "A"},
{:name => "Sophie", :grade => "C"}
]
}
I'm trying to append :semester=>"Summer" to each of the last four hashes. Here is what I'm trying to go for:
# ...preceding code is the same. Changed code below...
:students => [
{:name => "Marissa", :grade => "B", :semester => "Summer"},
{:name=>"Billy", :grade => "F", :semester => "Summer"},
{:name => "Frank", :grade => "A", :semester => "Summer"},
{:name => "Sophie", :grade => "C", :semester => "Summer"}
]
}
Just iterate over the students:
school[:students].each { |student| student[:semester] = "Summer" }
Or, using merge:
school[:students].each { |student| student.merge!(semester: "Summer") }
The issue is that when you do array.each {|x| do something}, x actually refers to each element in the array.
For example, in the first iteration of the loop,
x = {:name => "Marissa", :grade => "B"}
So what you are really doing is trying to reference:
school[:student][{:name => "Marissa", :grade => "B"}]
Which will not work
What you could do instead is create a for loop to track the index.
for i in 0 ... school[:student].count
school[:students][i].merge!(semester:"Summer")
end
Edit: Stefan's solution is much better than mine, but I will leave this up to show where you went wrong.
I would do as below using Hash#store :
require 'awesome_print'
school = {
:name => "Happy Funtime School",
:location => "NYC",
:instructors => [
{
:name => "Blake",
:subject => "being awesome"
},
{
:name => "Ashley",
:subject => "being better than blake"
},
{
:name => "Jeff",
:subject => "karaoke"
}
],
:students => [
{
:name => "Marissa",
:grade => "B"
},
{
:name => "Billy",
:grade => "F"
},
{
:name => "Frank",
:grade => "A"
},
{
:name => "Sophie",
:grade => "C"
}
]
}
school[:students].each{|h| h.store(:semester ,"Summer")}
ap school,:index => false,:indent => 10
output
{
:name => "Happy Funtime School",
:location => "NYC",
:instructors => [
{
:name => "Blake",
:subject => "being awesome"
},
{
:name => "Ashley",
:subject => "being better than blake"
},
{
:name => "Jeff",
:subject => "karaoke"
}
],
:students => [
{
:name => "Marissa",
:grade => "B",
:semester => "Summer"
},
{
:name => "Billy",
:grade => "F",
:semester => "Summer"
},
{
:name => "Frank",
:grade => "A",
:semester => "Summer"
},
{
:name => "Sophie",
:grade => "C",
:semester => "Summer"
}
]
}

Nesting a given array into children, based on condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like this:
tweets = [
{
:user_id => 234567,
:username => "A",
:created_at => "2012-10-12 10:20:30"
},
{
:user_id => 234568,
:username => "B",
:created_at => "2012-10-12 10:20:34"
},
{
:user_id => 234569,
:username => "C",
:created_at => "2012-10-12 10:20:35"
},
{
:user_id => 234570,
:username => "D",
:created_at => "2012-10-12 10:20:40"
}
]
and another array, like this:
followers = [
{
:user_id => 234567,
:follower_ids => [234568, 56654]
},
{
:user_id => 234568,
:follower_ids => [234569, 454445]
},
{
:user_id => 234569,
:follower_ids => [234570, 56333]
},
{
:user_id => 234570,
:follower_ids => [45566, 61145]
}
]
I want to nest it into a deep structure, where one is made into children of another. For making the children, the condition to be satisfied is:
any other tweet which has a greater created_at than the other, and
whose user_id is included in the follower_ids list if that tweet in
the followers array is considered to be a child
and the expected output for the given data is like this:
arranged_tweets = [
{
:user_id => 234567,
:username => "A",
:created_at => "2012-10-12 10:20:30",
:children => [
{
:user_id => 234568,
:username => "B",
:created_at => "2012-10-12 10:20:34",
:children => [
{
:user_id => 234569,
:username => "C",
:created_at => "2012-10-12 10:20:35",
:children => [
{
:user_id => 234570,
:username => "D",
:created_at => "2012-10-12 10:20:40"
}
]
}
]
}
]
}
]
Untested, but should give you the idea:
arranged_tweets = tweets.collect do |tweet|
arranged_tweet(tweet, tweets - [tweet])
end
def arranged_tweet(tweet, other_tweets)
{ :user_id => tweet[:user_id], ...
:children => children(tweet, other_tweets) }
end
def children(tweet, other_tweets)
other_tweets.find_all { |other| is_child?(other, tweet) }.collect do |other|
arranged_tweet(other, other_tweets - [other])
end
end
def is_child?(tweet, parent_tweet)
parent_tweet[:created_at] > tweet[:created_at] &&
is_follower?(tweet[:user_id], parent_tweet[:user_id])
end
def is_follower?(user_id, other_user_id)
followers[other_user_id][:follower_ids].include?(user_id)
end

Elastic Search Multiple Fields

I'm using Tire to index and search a database of cars:
create :mappings => {
:cars => {
:properties => {
:id => {:type => 'string', :index => 'not_analyzed', :include_in_all => true},
:user_id => {:type => 'string', :index => 'not_analyzed'},
:name => {:type => 'string', :analyzer => 'snowball' },
:time => {:type => 'long', :index => 'not_analyzed'},
:origin_country => {:type => 'string', :analyzer => 'keyword', :include_in_all => false},
:origin_state => {:type => 'string', :analyzer => 'keyword', :include_in_all => false},
:origin_city => {:type => 'string', :analyzer => 'keyword', :include_in_all => false},
:dealer_name => {:type => 'string', :analyzer => 'snowball', :boost => 1.5, :include_in_all => false}
}
}
}
And queries look like:
s = Tire.search Search.index_name do
query do |query|
query.text :_all, downcased_query, :type => :phrase
end
end
Right now if you search for "New York Joe Dealer" it will match if there is a dealer_name is "Joe Dealer" or if the origin_state is "New York" but not both. Is there a way to match origin_state is "New York" and "dealer_name" is "Joe Dealer" (or either)? The idea is I want a user to enter in a fre-form query string and be able to find the best matches possible and to a user they would expect to enter New York Joe Dealer and find all cars sold at Joe Dealer in New York (or all cars in New York or at Joe Dealer ranked lower).
The correct solution would be to use the multi_match query to perform the same query against multiple fields. It's supported in Tire, see https://github.com/karmi/tire/blob/master/test/integration/match_query_test.rb.
Tire.search Search.index_name do
query do |query|
query.match :dealer_name, downcased_query
query.match :origin_state, downcased_query
end
end
You can even use the phrase type if the query would contain both the dealer and state info in correct order, such as "New York Joe Dealer".
The solution was to move to a "match" type filter, and diable :type => :phrase, so:
query do |query|
query.match :_all, downcased_query
end
There are still some issues with sorting so that time is still a big input, but this resolves many of the issues.

Resources