0

There is this rails app which I created to accept SNS topic notification data. In summery SNS notification is generated on completion of a elastic transcoder job. So the rails app simply accept the incoming post request from AWS SNS service and All I need to parse this data. What doesn't work for me is, I'm trying to retrieve the Message => jobId but I can't figure out how. Appreciate any help.

def sns_transcode_completed

      amz_message_type = request.headers['x-amz-sns-message-type']
      amz_sns_topic = request.headers['x-amz-sns-topic-arn']

      if !amz_sns_topic.nil? &&
        amz_sns_topic.to_s.downcase == 'arn:aws:sns:us-east-1:XXXXXXXXXX:MyApp_transcode_completed'

        request_body = JSON.parse(request.body.read, {:symbolize_names => true})

        notification = Hashie::Mash.new(request_body)


        if amz_message_type.to_s.downcase == 'subscriptionconfirmation'
          subscribe_url = request_body['SubscribeURL']
          if !subscribe_url.to_s.empty? && !subscribe_url.nil?
            subscribe_confirm = HTTParty.get subscribe_url

            puts subscribe_confirm
            puts subscribe_url
          end


        end

        if amz_message_type.to_s.downcase == 'notification'

          puts "--------------------------"
          puts notification.inspect # See output 1
          puts "--------------------------"
          puts notification.MessageId # This works I can get the MessageId 
          puts "--------------------------"
          puts notification.Message => # From here I need to get the jobId, but it comes as a String? See output 2
          puts "--------------------------"



        end

      end
      render :nothing => true, :status => 200, :content_type => 'text/html'


    end

Output 1

          #<Hashie::Mash Message="{\n \"state\" : \"COMPLETED\",\n \"version\" : \"2012-09-25\",\n \"jobId\" : \"1440122777052-XXXXXX\",\n \"pipelineId\" :
   \"1432361831290-XXXXX\",\n \"input\" : {\n \"key\" : \"web-b796ab20-297c-0133-4ccf-378cf690e3b1.mp3\"\n },\n \"outputKeyPrefix\" :
   \"hlsv4/246-21632840-29d7-0133-1d8e-XXXXXXXX/\",\n \"outputs\" : [ {\n \"id\" : \"1\",\n \"presetId\" : \"1351620000001-200071\",\n \"key\" : \"hls_64k\",\n
   \"segmentDuration\" : 10.0,\n \"status\" : \"Complete\",\n \"duration\" : 60\n } ],\n \"playlists\" : [ {\n \"name\" : \"index\",\n \"format\" : \"HLSv4\",\n
   \"outputKeys\" : [ \"hls_64k\" ],\n \"status\" : \"Complete\"\n } ]\n}" MessageId="2d26f6d0-1715-5edb-af39-b8809eff521f" Signature="XXXXXX" SignatureVersion="1"
   SigningCertURL="https://sns.us-east-1.amazonaws.com/SimpleNotificationService-d6d679a1d18e95c2f9ffcf11fXXXXXXX.pem" Subject="Amazon Elastic Transcoder has
   finished transcoding job 1440122777052-XXXXXX." Timestamp="2015-08-21T02:06:34.523Z" TopicArn="arn:aws:sns:us-east-1:005550634774:MyApp_transcode_completed"
   Type="Notification"
   UnsubscribeURL="https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:005550634774:MyApp_transcode_completed:8c0e48fe-c34
   9-4185- bb94-XXXXXXXXXX">

Output 2

 {
  "state": "COMPLETED",
  "version": "2012-09-25",
  "jobId": "1440122777052-XXXXX",
  "pipelineId": "1432361831290-XXXXXX",
  "input": {
    "key": "web-b796ab20-297c-0133-4ccf-378cf690e3b1.mp3"
  },
  "outputKeyPrefix": "hlsv4/246-21632840-29d7-0133-1d8e-35ebbdecd855/",
  "outputs": [
    {
      "id": "1",
      "presetId": "1351620000001-200071",
      "key": "hls_64k",
      "segmentDuration": 10,
      "status": "Complete",
      "duration": 60
    }
  ],
  "playlists": [
    {
      "name": "index",
      "format": "HLSv4",
      "outputKeys": [
        "hls_64k"
      ],
      "status": "Complete"
    }
  ]
}
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
randika
  • 1,519
  • 3
  • 18
  • 39
  • `notification.Message` is a hash which includes key `"jobId"`. So you should be able to access the job ID with `notification.Message["jobId"]`. – steve klein Aug 21 '15 at 03:02
  • @steveklein I thought the same but when I do this `puts "Result is #{notification.Message["jobId"]}"` the output becomes `Result is jobId` - It seems `notification.Message` is a string ? – randika Aug 21 '15 at 04:57
  • Sorry, @randika but How can I capture the email send by aws SNS? my elastic transcoder works and send me an email with status and informations about the job completed, but How can I get this email in ruby? and show it in console? thanks so much – bsiilvia May 02 '17 at 14:17

2 Answers2

1

I was having the same issue and I ended up ditching Hashie.(It's really not needed aside from being able to use dot notation in this case.) I did still have to double parse (due to the string returned when you dig out the message). Once I grabbed that, the object I wanted to get at was in an Array so I used the dig method to pull out the object and then access the data I needed.

def sns_endpoint
notification = JSON.parse(request.raw_post, {:symbolize_names => true})
case notification[:Type]
  when "SubscriptionConfirmation"
    confirm(notification[:TopicArn], notification[:Token])
  when "Notification"
    message_data = JSON.parse(notification[:Message])
    thing_you_are_trying_to_get = message_data["Records"].dig(0)["s3"]...etc...(access the data you need here.)
     else
    Rails.logger.error "Unknown notification type #{notification[:Type]}"
end

render body: nil

end

angeleah
  • 11
  • 1
0

I got this working parsing the notification.Message value again. I wish if there is a better way than this.

parsed_message = JSON.parse(notification.Message, {:symbolize_names => true})

      puts parsed_message.inspect

      puts "--------------------------"

      puts parsed_message[:jobId]
randika
  • 1,519
  • 3
  • 18
  • 39