Rails Viewpoint发送消息附件

时间:2016-04-26 19:19:33

标签: ruby-on-rails ruby actionmailer exchangewebservices sendmessage

我已合并使用Microsoft Exchange服务发送电子邮件的观点gem。我没有发送普通HTML电子邮件的任何问题。我无法发送附有文件的电子邮件。有人请帮助我。

请找到下面的示例

    // scalastyle:off
    package org.apache.spark.mllib.regression

    import org.apache.spark.mllib.linalg.{Vector, Vectors}
    import org.apache.spark.mllib.util.LinearDataGenerator
    import org.apache.spark.streaming.dstream.DStream
    import org.apache.spark.streaming.{StreamingContext, TestSuiteBase}
    import org.apache.spark.streaming.TestSuiteBase
    import org.scalatest.BeforeAndAfter

    class StreamingLinearRegressionHypeOpt  extends TestSuiteBase with BeforeAndAfter {

      // use longer wait time to ensure job completion
      override def maxWaitTimeMillis: Int = 20000

      var ssc: StreamingContext = _

      override def afterFunction() {
        super.afterFunction()
        if (ssc != null) {
          ssc.stop()
        }
      }

      def calculateMSE(output: Seq[Seq[(Double, Double)]], n: Int): Double = {
        val mse = output
          .map {
            case seqOfPairs: Seq[(Double, Double)] =>
              val err = seqOfPairs.map(p => math.abs(p._1 - p._2)).sum
              err*err
          }.sum / n
        mse
      }

      def calculateRMSE(output: Seq[Seq[(Double, Double)]], n: Int): Double = {
          val mse = output
            .map {
              case seqOfPairs: Seq[(Double, Double)] =>
                val err = seqOfPairs.map(p => math.abs(p._1 - p._2)).sum
                err*err
            }.sum / n
          math.sqrt(mse)
      }

      def dummyStringStreamSplit(datastream: Stream[String]) =
        datastream.flatMap(txt => txt.split(" "))


      test("Test 1") {
        // create model initialized with zero weights
        val model = new StreamingLinearRegressionWithSGD()
          .setInitialWeights(Vectors.dense(0.0, 0.0))
          .setStepSize(0.2)
          .setNumIterations(25)

        // generate sequence of simulated data for testing
        val numBatches = 10
        val nPoints = 100
        val inputData = (0 until numBatches).map { i =>
          LinearDataGenerator.generateLinearInput(0.0, Array(10.0, 10.0), nPoints, 42 * (i + 1))
        }

        // Without hyper-parameters optimization
        withStreamingContext(setupStreams(inputData, (inputDStream: DStream[LabeledPoint]) => {
          model.trainOn(inputDStream)
          model.predictOnValues(inputDStream.map(x => (x.label, x.features)))
        })) { ssc =>
          val output: Seq[Seq[(Double, Double)]] = runStreams(ssc, numBatches, numBatches)

          val rmse = calculateRMSE(output, nPoints)
          println(s"RMSE = $rmse")
        }


        // With hyper-parameters optimization
        val gridParams = Map(
          "initialWeights" -> List(Vectors.dense(0.0, 0.0), Vectors.dense(10.0, 10.0)),
          "stepSize" -> List(0.1, 0.2, 0.3),
          "numIterations" -> List(25, 50)
        )

        val gridEvals = for (initialWeights <- gridParams("initialWeights");
                             stepSize <- gridParams("stepSize");
                             numIterations <- gridParams("numIterations")) yield {
          val lr = new StreamingLinearRegressionWithSGD()
                      .setInitialWeights(initialWeights.asInstanceOf[Vector])
                      .setStepSize(stepSize.asInstanceOf[Double])
                      .setNumIterations(numIterations.asInstanceOf[Int])


          withStreamingContext(setupStreams(inputData, (inputDStream: DStream[LabeledPoint]) => {
            lr.trainOn(inputDStream)
            lr.predictOnValues(inputDStream.map(x => (x.label, x.features)))
          })) { ssc =>
            val output: Seq[Seq[(Double, Double)]] = runStreams(ssc, numBatches, numBatches)
            val cvRMSE = calculateRMSE(output, nPoints)
            println(s"RMSE = $cvRMSE")
            (initialWeights, stepSize, numIterations, cvRMSE)
          }
        }

        // Save the evaluations for further visualization
        // val gridEvalsRDD = sc.parallelize(gridEvals)
        //      gridEvalsRDD.coalesce(1)
        //        .map(e => "%.3f\t%.3f\t%d\t%.3f".format(e._1, e._2, e._3, e._4))
        //        .saveAsTextFile("data/mllib/streaming")

      }

}
// scalastyle:on

-Raj

我的问题解决方案已于2016年4月27日更新

我调整了我的代码以使其可行

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")

1 个答案:

答案 0 :(得分:1)

send_message选项哈希接受file_attachments选项,如gem code中所述。此选项应为Array<File>类型。所以我想你的代码看起来像是:

...
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)

<强>更新

当你尝试发送带有文件的消息时,似乎gem已经破坏了(我认为消息只是保留为草稿而不是发送,只有文件)。所以我已经更新了宝石来修复这个案例,让我知道它是否正常工作。在 Gemfile

上从我的repo中导入gem
gem 'viewpoint', :git => 'https://github.com/durnin/Viewpoint.git'

再次尝试上面的代码。 (记得在更新 Gemfile 后)bundle install