I'm trying to use active_model_serializers to convert an object to JSON. As per this question, I created a file called config/initializers/serializers.rb with the following code:
puts 'Running serializers.rb.'
ActiveModelSerializers.config.adapter = :json_api
I see Running serializers.rb. when resetting the database, but not when running the Rails console. In the console, when I type UserSerializer.new(User.first).as_json, I get {:id=>1, :name=>"Regular User", :created_at=>Thu, 07 Jul 2016 06:03:46 UTC +00:00, :lists=>[{:id=>1, :title=>"Before Wednesday"}]}, which is clearly not JSON. How do I get the as_json method to actually return JSON?
UserSerializer.new(User.first).to_json
http://apidock.com/rails/Hash/to_json
Related
I'm sending moment object in request, but unfortunately the date is changing. For example when I choosed some date and I write to console, the date is ok, but in the request time is few hour early what is change the date.
console: Fri Oct 01 2021 00:00:00 GMT+0200 - ok,
In sent json: "2021-09-30T22:00:00.000Z" - wrong
Why it happens and how to fix it.
I'm trying to solve an issue.
I must get images from a Microsoft webservice. I'm using HTTParty in my project, but when I get the data the result is:
"\x89PNG\r\n\x1A\n\x00\x00\x00\rIHDR\x00\x00\x01,\x00\x00\x01,\b\x06\x00\x00\x00y}\x8Eu\x00\x00\x00\x01sRGB\x00\xAE\xCE\x1C\xE9\x00\x00\x00\x04gAMA\x00\x00\xB1\x8F\v\xFCa\x05\x00\x00\x00\tpHYs\x00\x00\x0E\xC3\x00\x00\x0E\xC3\x01\xC7o\xA8d\x00\x00\xEBAIDATx^\xEC\xFD\x87wTW\xB6-\x0E\xBF\x7F\xE07\xBE\xF1\xEE\xBB\xB7\xFBvn\xB7\xDDng\xB7sN`\xB0\xC1\xE4\x9Cs\x8E6\xD1\xD8\xC6&\xE7dr\xCE\x19\x91\x84\x90\x90\x04\x02\x84#9\xE7\x9C\x11( \x913\xEB\x9Bs\xEF\xDA\xAA\xA3B\x98T\x02A\x9F\xEDq,!U\x9D*\xEDS5k\xAD\xB9\xE6\x9A\xEB\xFF\x9C9sF.]\xBAd\x1F\xF6\x1E\xD8\xAF\x01\xFB5P\xAB_\x03\xC4\xAA\xFFs\xF5\xEAU\xB1\x97\xBD\x03\xF6\x0E\xD8;P\xDBw\x80X\xF5\x7F\x18]\xD9\xEB\xF1\xEE\xC0\xAD[\xB7?\xFE\xCD\x9B7\xE4\xE6\xCD\x9B8\xF8\x15\xC7\xAD\er\xEB\xD6\xCD{|\xA2\xB7\x84\xFF\xD9\xCB\xDE\x81\xA7i\a\x88U6`\xD5\x82+z\xFD\xFAU\xA9\xA8(\x93\x82\x82\x1CIK\x8B\x97\xB8\xB80\x89\x8A\x0E\x96\xE8\x98\x10\x89\x89\r\x97X\x1Cqq\x11\x12\x1F\x1F)I\xC91\x92\x9E\x9E(9\xB9ir\xEAT\xBE\x94W\x94\xC8\xE5\xCB\x17\xEE\x03\xCCj\xC1\x1Fl?\x05{\a\x1E`\al\xC0z\x80Ms\xE7]\xAE\\\xB9D\xAEB`\x82", #response=#<Net::HTTPOK 200 OK readbody=true>, #headers={"cache-control"=>["private"], "content-type"=>["image/jpeg"], "server"=>["Microsoft-IIS/7.5"], "x-aspnet-version"=>["4.0.30319"], "x-powered-by"=>["ASP.NET"], "date"=>["Wed, 23 Dec 2015 13:38:31 GMT"], "connection"=>["close"], "content-length"=>["60332"]}
I try to pass this data to Carrierwave using remote_model_url but of course is not working. Any idea?
Use Tempfile:
response = HTTParty.get('http://i.imgur.com/2xYuR0B.jpg')
ext = '.' + response.content_type.split('/').last
tmp = Tempfile.new(['img', ext])
tmp.binmode
tmp.write(response.body)
Now you can pass tmp to Carrierwave, for example:
u = User.new
u.avatar = tmp
u.save!
u.avatar.url # => "/uploads/tmp/1450886065-1021-6429/img20151223-1021-1w0uu5b.jpeg"
Already been through RFC 4469.
Just wanted to know how exactly I can use the CATENATE command.
I also referred the example given in the RFC. But couldn't really execute it against the server.
Any help will be appreciated.
I know this is old issue, but since I was looking this myself, and noticed this I thought I'd share what I found.
So. The simple examples:
s SELECT INBOX
a APPEND INBOX (\Seen) CATENATE (TEXT {53+}
Date: Tue, 03 Jan 2017 22:39:40 +0200
Hello, world.
)
This will work with modern system. You can also use
s SELECT INBOX
a APPEND INBOX (\Seen) CATENATE (TEXT {53}
Date: Tue, 03 Jan 2017 22:39:40 +0200
Hello, world.
)
The thing about catenate is that it can also combine input from other emails. You can do this with URL.
a APPEND INBOX CATENATE (URL "/INBOX;UIDVALIDITY=1483364905/;UID=2/;SECTION=HEADER" TEXT {8}
Hello..
)
a OK [APPENDUID 1483364905 4] Append completed.
FETCH 4:4 (BODY[])
Date: Tue, 03 Jan 2017 22:39:40 +0200
Hello..
)
a OK Fetch completed.
And we have reused headers from mail with UID 2 in INBOX. UIDVALIDITY can be acquired by saying s STATUS INBOX.
The examples in the IETF are bit spooky, but they show how to use mime multipart as input.
I'm facing trouble decoding messages fetched through ruby's Net::IMAP, here's what I've tried in console
imap = Net::IMAP.new(‘imap.gmail.com’,993,true)
imap.login(“my#gmail.com”, “mypassword”)
imap.select(‘INBOX’)
msgs = []
imap.uid_search(["NOT", "DELETED"]).each do |uid|
msgs << imap.uid_fetch(uid, ['RFC822']).first.attr['RFC822'].to_s
end
Now when I'm doing msgs.first I'm getting this
"Return-Path: <my#gmail.com>\r\n
Received: from 192.168.10.191:3000 ([111.93.167.67])\r\n
by mx.google.com with ESMTPSA id kz4sm35619700pbc.39.1969.12.31.16.00.00\r\n
(version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);\r\n
Thu, 05 Sep 2013 06:18:24 -0700 (PDT)\r\n
Date: Thu, 05 Sep 2013 18:48:18 +0530\r\n
From: my#gmail.com\r\n
Reply-To: please-change-me-at-config-initializers-devise#example.com\r\n
To: admin#example.com\r\n
Message-ID: <5228849ab79c5_4b57133029c665c4#rajde1.mail>\r\n
Subject: Confirmation instructions\r\n
Mime-Version: 1.0\r\n
Content-Type: text/html;\r\n
charset=UTF-8\r\n
Content-Transfer-Encoding: 7bit\r\n\r\n
<p>Welcome admin#example.com!</p>\r\n\r\n<p>You can confirm your account email through the link below:</p>\r\n\r\n<p>Confirm my account</p>\r\n"
How to decode this, please help. I'm new to this feature.
When you retrieve RFC822 you get the whole thing. If you retrieve BODY.PEEK[HEADER.FIELDS (Subject)] you'll get just the subject. (There's also an ENVELOPE which can be quite handy, it contains Subject, From and a few more, neatly parsed for you.) Fetching the body (I assume that's what you mean by content) is a little more tricky, you need to first retrieve the bodystructure, then retrieve the parts you want, often BODY.PEEK[1] or BODY.PEEK[1.2] or somesuch.
Try using something like this:
body1 = imap.fetch(uid, "RFC822")[0].attr["RFC822"]
mail = Mail.new(body1)
subject = mail.subject
to = mail.to
like that you could take all the value.
I am creating an array of hashes and passing it into a controller via json. Here is the data that I am sending and some server output right before it:
Started POST "/api/user.json" for 127.0.0.1 at Wed Jun 27 18:52:32 -0400 2012
Processing by Api::UsersController#create as JSON
Parameters: {"accept"=>"json", "content_type"=>"json", "users"=>"[{\"email\":\"kirsten_gronberg#yahoo.com\"},{\"email\":\"dtsheahan#msn.com\"},{\"email\":\"clayton19361#hotmail.com\"},{\"email\":\"kumar.alka92#yahoo.com\"},{\"email\":\"j.eromes.n.d.c#gmail.com\\r\"},{\"email\":\"0\"},{\"email\":\"christine#colormepink.com\"},{\"email\":\"janabouc#gmail.com\"},{\"email\":\"carin#inkfoundry.com\"},{\"email\":\"jon.neiditz#nelsonmullins.com\"}]"}
here is my controller code:
params["users"].each { |user_params|
User.create!(user_params)
}
and here is the error:
Completed 500 Internal Server Error in 2ms
NoMethodError (undefined method `keys' for #<String:0x10662d660>):
app/controllers/api/users_controller.rb:6:in `create'
app/controllers/api/users_controller.rb:5:in `each'
app/controllers/api/users_controller.rb:5:in `create'
Would anyone have any idea what might be happening wrong for me here?
Thanks!
When you receive params[:users], you are in fact receiving a string and not a hash. This string just happens to be encoded in JSON (i.e. in a style carrying the likeness of a Ruby hash.) You must convert this string into your hash through a method such as hash = JSON.parse(params[:users]).