Rails Viewpoint Send-message attachments - ruby-on-rails

I've incorporated viewpoint gem to send emails using Microsoft exchange services. I don't have any issues of sending plain html email. I am not able to send a email with document attached. Someone please help me in this.
please find the sample below
endpoint = "http:///.asmx"
ep_user = ""
ep_password = ""
viewclient = Viewpoint::EWSClient.new ep_user, ep_password
view_client.send_message (:subject => message.subject, :body => message.body, :body_type => "HTML")
-Raj
Solution for my problem updated on 04/27/2016
I tweaked my code in such a way to make it workable
mail(:from=>"", :to =>"", :subject => "", :doc_path => 'public/images/1.doc')
endpoint = "http:///.asmx"
ep_user = ""
ep_password = ""
viewclient = Viewpoint::EWSClient.new ep_user, ep_password
data_file = message[:doc_path].value
data = [File.open(data_file), "r"]
view_client.send_message (:subject => message.subject, :body => message.body, :body_type => "HTML", :file_attachments => data)

The send_message options hash accepts a file_attachments option as explained in the gem's code. This option should be of type Array<File>. So I guess your code would look like:
...
array_of_files = [File.join(Rails.root, 'whatever_directory', 'whatever_file.ext')]
view_client.send_message (:subject => message.subject, :body => message.body, :body_type => "HTML", :file_attachments => array_of_files)
Update:
Seems gem is broken for the case when you try to send a message with files (I think messages are just kept as drafts and not sent, only files are). So I've updated the gem to fix this case, let me know if it works correctly. Import gem from my repo like this on your Gemfile:
gem 'viewpoint', :git => 'https://github.com/durnin/Viewpoint.git'
And try the above code again. (remember to do bundle install after updating Gemfile)

Related

updating a column Parse Rest API

i am trying to update a Users information for ex. Phone, email etc.
i looked at this: https://parse.com/docs/rest/guide#users-updating-users
so i wrote this in my controller:
#response = HTTParty.put('https://api.parse.com/1/users/',
:headers => {"X-Parse-Application-Id" => "APIKEY",
"X-Parse-REST-API-Key" => "APIKEY",
"X-Parse-Session-Token" => session[:session_token],
"Content-Type" => "application/json"},
:data => {"phoneNumber" => "9994432"})
I return #response in a view and get back this:
{"error"=>"requested resource was not found"}
I was thinking maybe its because im not passing the user's objectid in the url?
Well, now that you have played with creating HTTP requests to API manually, it's time to switch to some library/gem for interactions with Parse. Hopefully, people who built the library that you will find, already have dealt with many routine tasks (like formatting your JSON properly – the error you are investigating right now), and have good documentation for many cases.
I suggest parse-ruby-client.
Add gem 'parse-ruby-client', github: 'adelevie/parse-ruby-client' to Gemfile (it's better to use master version, not the current Rubygems version, because they are saying that there are some useful changes which are not yet pushed to Rubygems), then run bundle install as usual, and you are good to go.
Object saving is as easy as
game_score = client.object("GameScore")
game_score["score"] = 1337
game_score["playerName"] = "Sean Plott"
game_score["cheatMode"] = false
result = game_score.save
puts result
according to their documentation.
UPD. Answering original question. You can use a function to provide object id dynamically:
def update_user(object_id)
#response = HTTParty.put("https://api.parse.com/1/users/#{object_id}",
:headers => {"X-Parse-Application-Id" => "APIKEY",
"X-Parse-REST-API-Key" => "APIKEY",
"X-Parse-Session-Token" => session[:session_token],
"Content-Type" => "application/json"},
:data => {"phoneNumber" => "9994432"})
end
My solution is force the variables to int and string
response = HTTParty.put("https://api.parse.com/1/users/#{object_id}",
:headers => {
"X-Parse-Application-Id" => ENV['PARSE_APP_ID'].to_s,
"X-Parse-REST-API-Key" => ENV['PARSE_API_KEY'].to_s,
"X-Parse-Session-Token" => token.to_s,
"Content-Type" => "application/json"},
:body => {"h_optimum" => optimum.to_i,
"h_moderate" => moderate.to_i,
"h_appalling" => appalling.to_i}
)

Ruby: POST data to a url

In a rails app I want to be able to post some information to another website inside a method without visiting the website. I'm fairly new to the topic so act as if I’m 5 years old.
My idea so far:
def create_button
button = {
:name => 'test',
:type => 'buy_now',
:callback_url => 'http://www.example.com/my_custom_button_callback',
:description => 'sample description',
:include_email => true
}
"https://thesite.com/api/v1" + button.to_query
# post logic here
end
Ruby ships with the Net::HTTP library for making HTTP requests. There are a lot of gems that wrap HTTP as well, my favorite is Faraday. Look into those libraries, it is pretty straightforward how to make a POST request with them.
There are a lots of gems that do this for free but if you only want to use the standard library, try something like this:
require "net/http"
require 'net/https'
require "uri"
uri = URI.parse("https://thesite.com/api/v1")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path)
# I'm unsure if symbols will work
button = {
"name" => 'test',
"type" => 'buy_now',
"callback_url" => 'http://www.example.com/my_custom_button_callback',
"description" => 'sample description',
"include_email" => true
}
req.set_form_data(button)
res = https.request(req)
Try: Net::HTTP
uri = URI.parse("https://thesite.com/api/v1")
button = {
:name => 'test',
:type => 'buy_now',
:callback_url => 'http://www.example.com/my_custom_button_callback',
:description => 'sample description',
:include_email => true
}
res = Net::HTTP.post_form(uri,button)
res.code
res.body # response from api

attaching an email message to an email rails 2.3.5 upgrading to rails 3.017

In rails 2.3.5 we were able to attach emails as attachments to other emails which were multipart emails using the following code:
recipients to
from from
subject subject
content_type "multipart/mixed"
part "text/html" do |p|
p.body = render_message("rampup_notification.text.html.erb", :mailbody => body)
end
part "text/plain" do |p|
p.body = render_message("rampup_notification.text.plain.erb", :mailbody => body)
end
email = enrollment_application.email
if email != nil && email.raw_email != nil
attachment :content_type => "message/rfc822", :filename => "icann.eml", :body => email.raw_email, :transfer_encoding => '7bit'
end
This was very temperamental to get to work with outlook, exchange etc along with other mailers.
How do I do this in rails 3?
I see: http://www.rubydoc.info/docs/rails/3.0.0/ActionMailer/Base:attachments
encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
:encoding => 'SpecialEncoding',
:content => encoded_content }
But I dont understand how to use this, is SpecialEncode a class I need to write that does 7bit encoding?
thanks
Joel
So it turns out rails 3 actionmailer is pretty smart, this seems to work fine:
#mailbody = body
attachments["icann.eml"] = {:content => email.raw_email }
mail(:to => to, :from => from, :subject => subject)

Pre-filling first and last name in Paypal setExpressCheckout using ActiveMerchant

I'm trying to get Paypal SetExpressCheckout operation to add first and last name for billing. I'm using ActiveMerchant. I'm seeing the address field pre-populated (street, state, city,zip-code) but nothing else.
#### gateway ######
gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(:login => 'login',:password => 'pass',:signature => 'sig')
### options ######
#options = Hash.new
#options.merge!(:ip => '127.0.0.1')
#options.merge!(:return_url => '127.0.0.1')
#options.merge!(:return_url => 'http://www.google.com')
#options.merge!(:cancel_return_url => 'http://www.google.com')
#options.merge!(:name => 'name')
#options.merge!(:description => 'description')
#options.merge!(:max_amount => 5000)
#options.merge!(:solution_type => 'Sole')
#options.merge!(:no_shipping => 1)
#options.merge!(:address_override => 1)
### build address
#address = Hash.new
#address.merge!(:name => "Joe User")
#address.merge!(:address1 => "111 ABCD EFG")
#address.merge!(:address2 => nil)
#address.merge!(:city => "Fremont")
#address.merge!(:state => "CA")
#address.merge!(:country => "US")
#address.merge!(:phone => "408-111-2222")
#options.merge!(:address => #address)
setup_response = gateway.setup_purchase(5000, #options)
redirect_to gateway.redirect_url_for(setup_response.token)
On the resultant page, I'm not seeing the name pre-filled for billing.
What am I doing wrong?
Thanks
I had the same issue as you did. After some research I came to the conclusion that this is a bug in ActiveMerchant. Please see the issue that I filed. It includes an explanation of how I patched my code to make phone number and names work:
https://github.com/Shopify/active_merchant/issues/161

Ruby IMAP library doesn't decode mail subject

I have got mail message with following subject in my Gmail accout:
"400, значение, значение"
Here is the code I use to grab mail:
imap = Net::IMAP.new('imap.gmail.com', 993, true, nil, false)
imap.login(LOGIN, PASSWORD)
imap.select("INBOX")
messages = imap.search(['ALL']).map do |message_id|
msg =imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
result = {:mailbox => msg.from[0].mailbox, :host => msg.from[0].host, :subject => msg.subject, :created_at => msg.date}
imap.store(message_id, "+FLAGS", [:Deleted])
result
end
imap.expunge()
imap.logout
In msg.subject i've got following value "=?KOI8-R?B?MTAwLCDixc7ayc4sIDMwMDAgzMnU0s/X?="
It seems that IMAP haven't decoded it. Should I do I manually or IMAP library could it for me?
Mail::Encodings is really helpful here:
require 'mail'
test = "zwei plus =?ISO-8859-15?Q?zw=F6lf_ist_vierzehn?="
puts Mail::Encodings.value_decode(test)
returns
zwei plus zwölf ist vierzehn
How about using NKF?
require 'nkf'
...
result = {... :subject => NKF.nkf("-mw", msg.subject), ...}
-mw means MIME decode and utf-8 output

Resources