Parse embedded string form JSON - ruby-on-rails-3

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

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.

Please try: email_payload['attachments'][0][3]
It appears that the problem may be the difference between :attachments (a symbol) and 'attachments' (a string).

Related

Laravel Auth::user() get attributes

when i dump( Auth::user()); that show me
User {#220 ▼
#attributes: array:10 [▼
"id" => "8"
"name" => "Vitaliy"
"email" => "dsadsa#i.ua"
"password" => "=)"
"remember_token" => null
"created_at" => "2017-10-19 13:11:21"
"updated_at" => "2017-10-19 13:11:21"
"phone" => "412412412"
"city" => "Dnipro"
"district" => "Leneinskiy"
]
#original: array:10 [▶]
....
}
In protected property we saw the info from user table
How can i get #attributes?
Or say me please how i can take all info about logged User.
Auth::user()->name
Auth::user()->email
etc...
Auth::User()->getAttributes();
This will get all the attributes of the logged in user.
simply you can use
$user = (Auth::User())->toArray();
or
use Illuminate\Support\Collection
$user = (new Collection(Auth::User()))->toArray();

Get records by hash value

I'm trying to get records from table by hash value.
Here is record example:
Activity:0x0000000709be18> {
:id => 1,
:trackable_id => 3,
:trackable_type => "User",
:owner_id => 1,
:owner_type => "User",
:key => "user.ban",
:parameters => {
:status => "new"
},
:recipient_id => nil,
:recipient_type => nil,
:created_at => Wed, 01 Jun 2016 22:19:39 UTC +00:00,
:updated_at => Wed, 01 Jun 2016 22:19:39 UTC +00:00
}
I need to get all Activities with paremeters status new (parameters[:status] == 'new').
This code works, but I need to return activerecord relation not an array.
Activity.select{|act| act.parameters == { status: 'new'}}
It is not easy to search within a serialized field, but you can use LIKE with some limitations.
Activity.where('parameters LIKE ?', '%status: new%')
This is working, but I do suggest adding a custom field, just like what public activity gem owner has mentioned:
"Besides the few fields that every activity has, you can also set
custom fields. This could be very beneficial, as parameters are a
serialized hash, which cannot be queried easily from the database."

Rails 3 execute custom sql query without a model

I need to write a standalone ruby script that is supposed to deal with database. I used code given below in rails 3
#connection = ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:database => "siteconfig_development",
:username => "root",
:password => "root123"
)
results = #connection.execute("select * from users")
results.each do |row|
puts row[0]
end
but getting error:-
`<main>': undefined method `execute' for #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00000002867548> (NoMethodError)
what i am missing here?
SOLUTION
After getting solution from denis-bu i used it following way and that worked too.
#connection = ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:database => "siteconfig_development",
:username => "root",
:password => "root123"
)
sql = "SELECT * from users"
#result = #connection.connection.execute(sql);
#result.each(:as => :hash) do |row|
puts row["email"]
end
Maybe try this:
ActiveRecord::Base.establish_connection(...)
ActiveRecord::Base.connection.execute(...)
connection = ActiveRecord::Base.connection
connection.execute("SQL query")
I'd recommend using ActiveRecord::Base.connection.exec_query instead of ActiveRecord::Base.connection.execute which returns a ActiveRecord::Result (available in rails 3.1+) which is a bit easier to work with.
Then you can access it in various the result in various ways like .rows, .each, or .to_hash
From the docs:
result = ActiveRecord::Base.connection.exec_query('SELECT id, title, body FROM posts')
result # => #<ActiveRecord::Result:0xdeadbeef>
# Get the column names of the result:
result.columns
# => ["id", "title", "body"]
# Get the record values of the result:
result.rows
# => [[1, "title_1", "body_1"],
[2, "title_2", "body_2"],
...
]
# Get an array of hashes representing the result (column => value):
result.to_hash
# => [{"id" => 1, "title" => "title_1", "body" => "body_1"},
{"id" => 2, "title" => "title_2", "body" => "body_2"},
...
]
# ActiveRecord::Result also includes Enumerable.
result.each do |row|
puts row['title'] + " " + row['body']
end
note: copied my answer from here
You could also use find_by_sql
# A simple SQL query spanning multiple tables
Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
> [#<Post:0x36bff9c #attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]
How about this :
#client = TinyTds::Client.new(
:adapter => 'mysql2',
:host => 'host',
:database => 'siteconfig_development',
:username => 'username',
:password => 'password'
sql = "SELECT * FROM users"
result = #client.execute(sql)
results.each do |row|
puts row[0]
end
You need to have TinyTds gem installed, since you didn't specify it in your question I didn't use Active Record

Finding the content type of the uploaded file in rails

I am working on ruby on rails. I am trying to do a file attachment (image/audio/video) .
So i have a common method like
byteArray = StringIO.new(File.open("path").read)
Is it possible to find the content type of the byteArray to check whether the uploaded file is a image/audio/video/pdf in ruby.
I saw this was tagged paperclip, so I will give you how we do it with paperclip:
class Attachment < ActiveRecord::Base
has_attached_file :attachment,
styles: lambda { |a| a.instance.is_image? ? {:small => "x200>", :medium => "x300>", :large => "x400>"} : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}},
:processors => lambda { |a| a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] }
def is_video?
attachment.instance.attachment_content_type =~ %r(video)
end
def is_image?
attachment.instance.attachment_content_type =~ %r(image)
end
end
If you manage to get your file into Paperclip, it basically cuts it up into content_type already. This means that if you use a lambda to determine whether the attachment content_type contains image or video
If you give me some more info on what you're trying to achieve, I can give you some refactored code to help with your issue specifically :)

Is Greyscale function of Wicked_PDF functional?

In my project I am trying to print a PDF in Greyscale with a couple of images and text from HTML using the wicked_PDF. The Gem provides a function for the same, but it doesn't seem to be working.
Here is my code:
render :pdf => "MyObject",
:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf',
:template => '/widgets/pdf/show_myObject.erb',
:page_size => 'A4',
:header => { :html => { :template => "/widgets/pdf/myObject_header.erb" }},
:footer => { :html => { :template => "/widgets/pdf/myObject_footer.erb" }, :line => true },
:margin => { :top => 0, :left => 3, :right => 3 },
:greyscale => true
I am passing the images as background. But it renders in color. Am I missing something? Why is wicked_PDF gem unable to process greyscale function as expected?
Their documentation did not have any help regarding this.
Turns out the wkhtmltopdf --grayscale option was incorrectly coded in wicked_pdf as --greyscale
(swap the 'e' for an 'a').
I've pushed a fix, and cut a new version of the gem (0.7.9) for this. Thanks for pointing it out!
:grayscale => true
Now works as intended.