0

I have a Rails 3 application and I am using the following code to parse a JSON post:

email_payload = JSON.parse(params[:payload])
Result.create(:from => email_payload['from'], :from_name => email_payload['from_name'], :from_address => email_payload['from_address'], :date => email_payload['date'], :html_body => email_payload['html_body'], :priority => email_payload['priority'], :spam_status => email_payload['spam_status'], :subject => email_payload['subject'])

The JSON post data is as follows:

payload{ "address_identifier": null, "attachments": [ [ "twitter.png", "image/png", 3029, "http://api.deliverhq.com/api/incoming/attachment/7gdd71wo75/5772412/0" ] ], "cc": null, "date": "Thu, 25 Oct 2012 22:04:20 +0100"

I am trying to work out how to parse the URL, in this case http://api.deliverhq.com/api/incoming/attachment/7gdd71wo75/5772412/0, and then enter the URL into the database.

:url => email_payload['attachments']

won't work because there is multiple values within attachment and:

email_attachments_payload = JSON.parse(params[:payload][:attachments])
:url => email_attachments_payload['URL']

won't work because the URL doesn't have an identifier. For this particular application there should only ever be one attachment, therefore selecting .first may be an option if that's possible.

Any pointers would be appreciated!

UPDATE:

Adding:

email_payload[:attachments][0][4]

results in the following exception error:

NoMethodError: undefined method []' for nil:NilClass

UPDATE 2

def receive_fax
    if request.get?
        render :text => "hello"
    elsif request.post?   
      email_payload = JSON.parse(params[:payload])     
      Fax.create(:from => email_payload['from'], :from_name => email_payload['from_name'], :from_address => email_payload['from_address'], :date => email_payload['date'], :html_body => email_payload['html_body'], :priority => email_payload['priority'], :spam_status => email_payload['spam_status'], :subject => email_payload['subject'], :fax => email_payload[:attachments][0][3])
      render :text => "success"
    else
      render :text => "error"
    end
  end
6
  • You seem to be switching between attachment and attachments, is it singular or plural? Commented Oct 27, 2012 at 22:06
  • email_payload[:attachments] is a 1-element array with a 4-element array in it, the last of which is a URL. What you want is to get that URL? Can't you just use email_payload[:attachments][0][4]? If not then you have to be more specific about the constraints. Commented Oct 28, 2012 at 0:34
  • Yeah doing that throws an exception. TypeError: can't convert Symbol into Integer Commented Oct 28, 2012 at 10:02
  • Sorry - the actual error is NoMethodError: undefined method []' for nil:NilClass` Commented Oct 28, 2012 at 10:11
  • Sorry that should be email_payload[:attachments][0][3]. It works fine in the console. Commented Oct 28, 2012 at 14:03

2 Answers 2

2

In irb:

require 'json'

email_payload = JSON.parse('{ "address_identifier": null, "attachments": [ [ "twitter.png", "image/png", 3029, "http://api.deliverhq.com/api/incoming/attachment/7gdd71wo75/5772412/0" ] ], "cc": null, "date": "Thu, 25 Oct 2012 22:04:20 +0100" }')
#=> {"address_identifier"=>nil, "attachments"=>[["twitter.png", "image/png", 3029, "http://api.deliverhq.com/api/incoming/attachment/7gdd71wo75/5772412/0"]], "cc"=>nil, "date"=>"Thu, 25 Oct 2012 22:04:20 +0100"}
email_payload['attachments'][0][3]
#=> "http://api.deliverhq.com/api/incoming/attachment/7gdd71wo75/5772412/0"

If that doesn't work then there's something else going on that you haven't described above.

UPDATE: in the hash returned from JSON.parse, the attachments key should be a string ('attachments'), not a symbol (:attachments). Updated my answer above.

Sign up to request clarification or add additional context in comments.

2 Comments

Is the way I'm parsing the payload causing an error? I've updated my question with the full code.
Very sorry, I miscopied my code from the console. It should be email_payload['attachments'][0][3] - i.e. 'attachments' (a string) not :attachments (a symbol).
1

Please try: email_payload['attachments'][0][3]

It appears that the problem may be the difference between :attachments (a symbol) and 'attachments' (a string).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.