Phoenix app crashing on Heroku CI - heroku

We are attempting to port our CI from Travis to Heroku. Tests all pass both locally and on Travis, but on Heroku we are getting some erlang errors that are difficult to debug.
All of the errors seem to arise from tests that make use of the Elixir Mock module whilst mocking an AWS S3 request.
Here is an example test:
test "put_object failure function" do
with_mock ExAws, [request!: fn(_) -> %{status_code: 500} end] do
res = S3.put_object("unique.png", "binary")
assert res == {:error, "error uploading to S3"}
end
end
And the put object is simply:
def put_object(unique, image_binary) do
bucket = System.get_env("BUCKET_NAME")
res = ExAws.S3.put_object(bucket, unique, image_binary)
|> ExAws.request!
case res do
%{status_code: 200} ->
{:ok, image_url(unique, bucket)}
_ ->
{:error, "error uploading to S3"}
end
end
So I'm not too sure what is going wrong.
The errors from Heroku CI are here:
8) test put_object failure function (Engine.S3Test)
test/controllers/s3_test.exs:34
** (EXIT from #PID<0.1043.0>)
{:compile_forms, {:error, [{[], [{:none, :compile, {:crash, :lint_module, {:badarg, [{:erlang, :atom_to_list, [[Application]], []},
{:erl_lint, :is_latin1_name, 1, [file: 'erl_lint.erl', line: 3050]}, {:erl_lint, :check_module_name, 3, [file: 'erl_lint.erl', line: 3043]},
{:erl_lint, :behaviour_callbacks, 3, [file: 'erl_lint.erl', line: 968]}, {:erl_lint, :all_behaviour_callbacks, 3, [file: 'erl_lint.erl', line: 929]},
{:erl_lint, :behaviour_check, 2, [file: 'erl_lint.erl', line: 916]}, {:erl_lint, :post_traversal_check, 2, [file: 'erl_lint.erl', line: 888]}, {:erl_lint, :module, 3, [file: 'erl_lint.erl', line: 534]}, {:compile, :lint_module, 2, [file: 'compile.erl', line: 1109]}, {:compile, :"-internal_comp/5-anonymous-1-", 3, [file: 'compile.erl', line: 342]}, {:compile, :fold_comp, 4, [file: 'compile.erl', line: 369]}, {:compile, :internal_comp, 5, [file: 'compile.erl', line: 353]},
{:compile, :"-do_compile/2-anonymous-0-", 2, [file: 'compile.erl', line: 177]}, {:compile, :"-do_compile/2-anonymous-1-", 1, [file: 'compile.erl', line: 190]}]}}}]}], []}}

That sorted it thanks to #Dogbert's answer here
Update mock version to 0.3.1 - {:mock, "~> 0.3.1", only: :test}

Related

Elixir - Read postgres result

I need to read the raw Postgres output and iterate over the row value.
ww = %Postgrex.Result{columns: ["id", "tracking_link"], command: :select,
connection_id: 22311, num_rows: 14,
rows: [[2, "http://StarChef.com?id=1"],
[3, "http://CaptainAmerica:TWSLiveWP.com?id=2"]]}
ww[:rows] - not giving output
You need to use . to access fields of a struct. The bracket syntax only works for maps which are not structs or structs which implement the Access behavior.
iex(1)> ww = %Postgrex.Result{columns: ["id", "tracking_link"], command: :select,
...(1)> connection_id: 22311, num_rows: 14,
...(1)> rows: [[2, "http://StarChef.com?id=1"],
...(1)> [3, "http://CaptainAmerica:TWSLiveWP.com?id=2"]]}
%Postgrex.Result{columns: ["id", "tracking_link"], command: :select,
connection_id: 22311, num_rows: 14,
rows: [[2, "http://StarChef.com?id=1"],
[3, "http://CaptainAmerica:TWSLiveWP.com?id=2"]]}
iex(2)> ww.rows
[[2, "http://StarChef.com?id=1"],
[3, "http://CaptainAmerica:TWSLiveWP.com?id=2"]]

Why doesn't this example ruby code print the result?

I'm wondering why the code below doesn't print the movies that have more than 3 stars.
movie_ratings = {
memento: 3,
primer: 3.5,
the_matrix: 5,
truman_show: 4,
red_dawn: 1.5,
skyfall: 4,
alex_cross: 2,
uhf: 1,
lion_king: 3.5
}
def good_movies
puts movie_ratings.select {|movies, ratings| ratings > 3}
end
good_movies
Local variable movie_ratings is not accessible in the method good_movies. Several approaches are available here:
Pass ratings as a parameter
movie_ratings = {
memento: 3,
primer: 3.5,
the_matrix: 5,
truman_show: 4,
red_dawn: 1.5,
skyfall: 4,
alex_cross: 2,
uhf: 1,
lion_king: 3.5
}
def good_movies(ratings)
puts ratings.select {|movies, ratings| ratings > 3}
end
good_movies(movie_ratings)
Make ratings instance variable (instead of local variable)
#movie_ratings = {
memento: 3,
primer: 3.5,
the_matrix: 5,
truman_show: 4,
red_dawn: 1.5,
skyfall: 4,
alex_cross: 2,
uhf: 1,
lion_king: 3.5
}
def good_movies
puts #movie_ratings.select {|movies, ratings| ratings > 3}
end
good_movies
The variable movie_ratings is not in the scope of the method. you should pass it as a parameter:
movie_ratings = {
memento: 3,
primer: 3.5,
the_matrix: 5,
truman_show: 4,
red_dawn: 1.5,
skyfall: 4,
alex_cross: 2,
uhf: 1,
lion_king: 3.5
}
def good_movies movie_ratings
puts movie_ratings.select {|movies, ratings| ratings > 3}
end
good_movies movie_ratings
# {:primer=>3.5, :the_matrix=>5, :truman_show=>4, :skyfall=>4, :lion_king=>3.5}
Because local variables cannot take scope into a method block. Change it to another type of variable, for example an instance variable, and it will work.
#movie_ratings = {
memento: 3,
primer: 3.5,
the_matrix: 5,
truman_show: 4,
red_dawn: 1.5,
skyfall: 4,
alex_cross: 2,
uhf: 1,
lion_king: 3.5
}
def good_movies
puts #movie_ratings.select {|movies, ratings| ratings > 3}
end
good_movies
You should either pass movie_ratings to the method good_movies like this :
def good_movies(movie_ratings)
puts movie_ratings.select {|movies, ratings| ratings > 3}
end
or make movie_ratings an instance variable like this:
#movie_ratings
and then inside the method use: puts #movie_ratings.select {|movies, ratings| ratings > 3}
If you're getting the following error:
NameError: undefined local variable or method `movie_ratings' for main:Object
The reason is the method good_movies creates its own local scope, and it doesn't understand what movie_ratings is. You need to pass it to the method so that the method will understand what it is.
def good_movies(movies)
return movies.select { |m, r| r > 3 }
end

What is `&method` called? How to pass multiple arguments in it? [duplicate]

You're probably familiar with the following Ruby shorthand (a is an array):
a.map(&:method)
For example, try the following in irb:
>> a=[:a, 'a', 1, 1.0]
=> [:a, "a", 1, 1.0]
>> a.map(&:class)
=> [Symbol, String, Fixnum, Float]
The syntax a.map(&:class) is a shorthand for a.map {|x| x.class}.
Read more about this syntax in "What does map(&:name) mean in Ruby?".
Through the syntax &:class, you're making a method call class for each array element.
My question is: can you supply arguments to the method call? And if so, how?
For example, how do you convert the following syntax
a = [1,3,5,7,9]
a.map {|x| x + 2}
to the &: syntax?
I'm not suggesting that the &: syntax is better.
I'm merely interested in the mechanics of using the &: syntax with arguments.
I assume you know that + is a method on Integer class. You can try the following in irb:
>> a=1
=> 1
>> a+(1)
=> 2
>> a.send(:+, 1)
=> 2
You can create a simple patch on Symbol like this:
class Symbol
def with(*args, &block)
->(caller, *rest) { caller.send(self, *rest, *args, &block) }
end
end
Which will enable you to do not only this:
a = [1,3,5,7,9]
a.map(&:+.with(2))
# => [3, 5, 7, 9, 11]
But also a lot of other cool stuff, like passing multiple parameters:
arr = ["abc", "babc", "great", "fruit"]
arr.map(&:center.with(20, '*'))
# => ["********abc*********", "********babc********", "*******great********", "*******fruit********"]
arr.map(&:[].with(1, 3))
# => ["bc", "abc", "rea", "rui"]
arr.map(&:[].with(/a(.*)/))
# => ["abc", "abc", "at", nil]
arr.map(&:[].with(/a(.*)/, 1))
# => ["bc", "bc", "t", nil]
And even work with inject, which passes two arguments to the block:
%w(abecd ab cd).inject(&:gsub.with('cde'))
# => "cdeeecde"
Or something super cool as passing [shorthand] blocks to the shorthand block:
[['0', '1'], ['2', '3']].map(&:map.with(&:to_i))
# => [[0, 1], [2, 3]]
[%w(a b), %w(c d)].map(&:inject.with(&:+))
# => ["ab", "cd"]
[(1..5), (6..10)].map(&:map.with(&:*.with(2)))
# => [[2, 4, 6, 8, 10], [12, 14, 16, 18, 20]]
Here is a conversation I had with #ArupRakshit explaining it further:
Can you supply arguments to the map(&:method) syntax in Ruby?
As #amcaplan suggested in the comment below, you could create a shorter syntax, if you rename the with method to call. In this case, ruby has a built in shortcut for this special method .().
So you could use the above like this:
class Symbol
def call(*args, &block)
->(caller, *rest) { caller.send(self, *rest, *args, &block) }
end
end
a = [1,3,5,7,9]
a.map(&:+.(2))
# => [3, 5, 7, 9, 11]
[(1..5), (6..10)].map(&:map.(&:*.(2)))
# => [[2, 4, 6, 8, 10], [12, 14, 16, 18, 20]]
Here is a version using Refinements (which is less hacky than globally monkey patching Symbol):
module AmpWithArguments
refine Symbol do
def call(*args, &block)
->(caller, *rest) { caller.send(self, *rest, *args, &block) }
end
end
end
using AmpWithArguments
a = [1,3,5,7,9]
a.map(&:+.(2))
# => [3, 5, 7, 9, 11]
[(1..5), (6..10)].map(&:map.(&:*.(2)))
# => [[2, 4, 6, 8, 10], [12, 14, 16, 18, 20]]
For your example can be done a.map(&2.method(:+)).
Arup-iMac:$ pry
[1] pry(main)> a = [1,3,5,7,9]
=> [1, 3, 5, 7, 9]
[2] pry(main)> a.map(&2.method(:+))
=> [3, 5, 7, 9, 11]
[3] pry(main)>
Here is how it works :-
[3] pry(main)> 2.method(:+)
=> #<Method: Fixnum#+>
[4] pry(main)> 2.method(:+).to_proc
=> #<Proc:0x000001030cb990 (lambda)>
[5] pry(main)> 2.method(:+).to_proc.call(1)
=> 3
2.method(:+) gives a Method object. Then &, on 2.method(:+), actually a call #to_proc method, which is making it a Proc object. Then follow What do you call the &: operator in Ruby?.
As the post you linked to confirms, a.map(&:class) is not a shorthand for a.map {|x| x.class} but for a.map(&:class.to_proc).
This means that to_proc is called on whatever follows the & operator.
So you could give it directly a Proc instead:
a.map(&(Proc.new {|x| x+2}))
I know that most probably this defeats the purpose of your question but I can't see any other way around it - it's not that you specify which method to be called, you just pass it something that responds to to_proc.
There is another native option for enumerables which is pretty only for two arguments in my opinion. the class Enumerable has the method with_object which then returns another Enumerable.
So you can call the & operator for a method with each item and the object as arguments.
Example:
a = [1,3,5,7,9]
a.to_enum.with_object(2).map(&:+) # => [3, 5, 7, 9, 11]
In the case you want more arguments you should repeat the proccess but it's ugly in my opinion:
a = [1,3,5,7,9]
a.to_enum.with_object(2).map(&:+).to_enum.with_object(5).map(&:+) # => [8, 10, 12, 14, 16]
Short answer: No.
Following #rkon's answer, you could also do this:
a = [1,3,5,7,9]
a.map &->(_) { _ + 2 } # => [3, 5, 7, 9, 11]
if all your method needs as argument is an element from the array, this is probably the simplest way to do it:
def double(x)
x * 2
end
[1, 2, 3].map(&method(:double))
=> [2, 4, 6]
Instead of patching core classes yourself, as in the accepted answer, it's shorter and cleaner to use the functionality of the Facets gem:
require 'facets'
a = [1,3,5,7,9]
a.map &:+.(2)
I'm surprised no one mentioned using curry yet, which has been in Ruby since Ruby 2.2.9. Here's how it can be done in the way OP wants using the standard Ruby library:
[1,3,5,7,9].map(&:+.to_proc.curry(2).call(11))
# => [12, 14, 16, 18, 20]
You need to supply an arity to curry that matches the call, though. This is because the interpreter doesn't know which object the + method refers to yet. This also means you can only use this when all the objects in map have the same arity. But that's probably not an issue if you're trying to use it this way.
I'm not sure about the Symbol#with already posted, I simplified it quite a bit and it works well:
class Symbol
def with(*args, &block)
lambda { |object| object.public_send(self, *args, &block) }
end
end
(also uses public_send instead of send to prevent calling private methods, also caller is already used by ruby so this was confusing)

Can you supply arguments to the map(&:method) syntax in Ruby?

You're probably familiar with the following Ruby shorthand (a is an array):
a.map(&:method)
For example, try the following in irb:
>> a=[:a, 'a', 1, 1.0]
=> [:a, "a", 1, 1.0]
>> a.map(&:class)
=> [Symbol, String, Fixnum, Float]
The syntax a.map(&:class) is a shorthand for a.map {|x| x.class}.
Read more about this syntax in "What does map(&:name) mean in Ruby?".
Through the syntax &:class, you're making a method call class for each array element.
My question is: can you supply arguments to the method call? And if so, how?
For example, how do you convert the following syntax
a = [1,3,5,7,9]
a.map {|x| x + 2}
to the &: syntax?
I'm not suggesting that the &: syntax is better.
I'm merely interested in the mechanics of using the &: syntax with arguments.
I assume you know that + is a method on Integer class. You can try the following in irb:
>> a=1
=> 1
>> a+(1)
=> 2
>> a.send(:+, 1)
=> 2
You can create a simple patch on Symbol like this:
class Symbol
def with(*args, &block)
->(caller, *rest) { caller.send(self, *rest, *args, &block) }
end
end
Which will enable you to do not only this:
a = [1,3,5,7,9]
a.map(&:+.with(2))
# => [3, 5, 7, 9, 11]
But also a lot of other cool stuff, like passing multiple parameters:
arr = ["abc", "babc", "great", "fruit"]
arr.map(&:center.with(20, '*'))
# => ["********abc*********", "********babc********", "*******great********", "*******fruit********"]
arr.map(&:[].with(1, 3))
# => ["bc", "abc", "rea", "rui"]
arr.map(&:[].with(/a(.*)/))
# => ["abc", "abc", "at", nil]
arr.map(&:[].with(/a(.*)/, 1))
# => ["bc", "bc", "t", nil]
And even work with inject, which passes two arguments to the block:
%w(abecd ab cd).inject(&:gsub.with('cde'))
# => "cdeeecde"
Or something super cool as passing [shorthand] blocks to the shorthand block:
[['0', '1'], ['2', '3']].map(&:map.with(&:to_i))
# => [[0, 1], [2, 3]]
[%w(a b), %w(c d)].map(&:inject.with(&:+))
# => ["ab", "cd"]
[(1..5), (6..10)].map(&:map.with(&:*.with(2)))
# => [[2, 4, 6, 8, 10], [12, 14, 16, 18, 20]]
Here is a conversation I had with #ArupRakshit explaining it further:
Can you supply arguments to the map(&:method) syntax in Ruby?
As #amcaplan suggested in the comment below, you could create a shorter syntax, if you rename the with method to call. In this case, ruby has a built in shortcut for this special method .().
So you could use the above like this:
class Symbol
def call(*args, &block)
->(caller, *rest) { caller.send(self, *rest, *args, &block) }
end
end
a = [1,3,5,7,9]
a.map(&:+.(2))
# => [3, 5, 7, 9, 11]
[(1..5), (6..10)].map(&:map.(&:*.(2)))
# => [[2, 4, 6, 8, 10], [12, 14, 16, 18, 20]]
Here is a version using Refinements (which is less hacky than globally monkey patching Symbol):
module AmpWithArguments
refine Symbol do
def call(*args, &block)
->(caller, *rest) { caller.send(self, *rest, *args, &block) }
end
end
end
using AmpWithArguments
a = [1,3,5,7,9]
a.map(&:+.(2))
# => [3, 5, 7, 9, 11]
[(1..5), (6..10)].map(&:map.(&:*.(2)))
# => [[2, 4, 6, 8, 10], [12, 14, 16, 18, 20]]
For your example can be done a.map(&2.method(:+)).
Arup-iMac:$ pry
[1] pry(main)> a = [1,3,5,7,9]
=> [1, 3, 5, 7, 9]
[2] pry(main)> a.map(&2.method(:+))
=> [3, 5, 7, 9, 11]
[3] pry(main)>
Here is how it works :-
[3] pry(main)> 2.method(:+)
=> #<Method: Fixnum#+>
[4] pry(main)> 2.method(:+).to_proc
=> #<Proc:0x000001030cb990 (lambda)>
[5] pry(main)> 2.method(:+).to_proc.call(1)
=> 3
2.method(:+) gives a Method object. Then &, on 2.method(:+), actually a call #to_proc method, which is making it a Proc object. Then follow What do you call the &: operator in Ruby?.
As the post you linked to confirms, a.map(&:class) is not a shorthand for a.map {|x| x.class} but for a.map(&:class.to_proc).
This means that to_proc is called on whatever follows the & operator.
So you could give it directly a Proc instead:
a.map(&(Proc.new {|x| x+2}))
I know that most probably this defeats the purpose of your question but I can't see any other way around it - it's not that you specify which method to be called, you just pass it something that responds to to_proc.
There is another native option for enumerables which is pretty only for two arguments in my opinion. the class Enumerable has the method with_object which then returns another Enumerable.
So you can call the & operator for a method with each item and the object as arguments.
Example:
a = [1,3,5,7,9]
a.to_enum.with_object(2).map(&:+) # => [3, 5, 7, 9, 11]
In the case you want more arguments you should repeat the proccess but it's ugly in my opinion:
a = [1,3,5,7,9]
a.to_enum.with_object(2).map(&:+).to_enum.with_object(5).map(&:+) # => [8, 10, 12, 14, 16]
Short answer: No.
Following #rkon's answer, you could also do this:
a = [1,3,5,7,9]
a.map &->(_) { _ + 2 } # => [3, 5, 7, 9, 11]
if all your method needs as argument is an element from the array, this is probably the simplest way to do it:
def double(x)
x * 2
end
[1, 2, 3].map(&method(:double))
=> [2, 4, 6]
Instead of patching core classes yourself, as in the accepted answer, it's shorter and cleaner to use the functionality of the Facets gem:
require 'facets'
a = [1,3,5,7,9]
a.map &:+.(2)
I'm surprised no one mentioned using curry yet, which has been in Ruby since Ruby 2.2.9. Here's how it can be done in the way OP wants using the standard Ruby library:
[1,3,5,7,9].map(&:+.to_proc.curry(2).call(11))
# => [12, 14, 16, 18, 20]
You need to supply an arity to curry that matches the call, though. This is because the interpreter doesn't know which object the + method refers to yet. This also means you can only use this when all the objects in map have the same arity. But that's probably not an issue if you're trying to use it this way.
I'm not sure about the Symbol#with already posted, I simplified it quite a bit and it works well:
class Symbol
def with(*args, &block)
lambda { |object| object.public_send(self, *args, &block) }
end
end
(also uses public_send instead of send to prevent calling private methods, also caller is already used by ruby so this was confusing)

weird Socket.getaddrinfo result for localhost

I have a problem with a ruby application connecting to a data base. After some hours of poking around, my problem boiled down to Socket.getaddrinfo 'localhost', nil returns unexpected addresses:
ruby -e "require \"socket\"; puts Socket.getaddrinfo('localhost', nil).inspect"
[["AF_INET", nil, "10.10.1.37", "10.10.1.37", 2, 2, 17],
["AF_INET", nil, "10.10.1.37", "10.10.1.37", 2, 1, 6],
["AF_INET", nil, "fe80:0:0:0:5e96:9dff:fe94:fce3%5", "fe80:0:0:0:5e96:9dff:fe94:fce3%5", 2, 2, 17],
["AF_INET", nil, "fe80:0:0:0:5e96:9dff:fe94:fce3%5", "fe80:0:0:0:5e96:9dff:fe94:fce3%5", 2, 1, 6]]
10.10.1.37 is the address of the network adapter.
I'm using rvm and this strange behavior just happens, when I use a jruby. If I use a regular ruby version, the result is what I expect ('127.0.0.1' for example).
According to wikipedia, localhost should refer to the local loop back interface. What could cause this behavior, it this a Java thingy?
JRuby versions I've tried are: 1.7.2, 1.7.4 and 1.7.9
The more expected output (when using a regular ruby-1.9.3) is:
ruby -e "require \"socket\"; puts Socket.getaddrinfo('localhost', nil).inspect"
[["AF_INET6", 0, "::1", "::1", 30, 2, 17],
["AF_INET6", 0, "::1", "::1", 30, 1, 6],
["AF_INET", 0, "127.0.0.1", "127.0.0.1", 2, 2, 17],
["AF_INET", 0, "127.0.0.1", "127.0.0.1", 2, 1, 6],
["AF_INET6", 0, "fe80::1%lo0", "fe80::1%lo0", 30, 2, 17],
["AF_INET6", 0, "fe80::1%lo0", "fe80::1%lo0", 30, 1, 6]]
This looks like a bug in jruby. They try to maintain compatibility with MRI as much as possible, and when that doesn't happen, it is considered a bug unless otherwise noted. You should open an issue with them over at Github
EDIT
I was not able to reproduce this on Linux running jruby 1.7.10 or on OS X using 1.7.8, but it still seems like a bug to me.

Resources