Technology and Software

FactoryGirl and Paperclip: testing content types

I used Paparclip to add a picture to a model, something I did for years. This time I also added a validation for content types, and this might be a first time for me (I don’t want to grep all the models of all the past projects). The validation is

validates_attachment :picture,
content_type: { content_type: ["image/jpg","image/png"] }

Now I want to test it. I was loading real image files in the objects created with FactoryGirl. This is the code

picture  { File.open("#{Rails.root}/#{%x[ls test-images/*jpg].split("\n").sample}") }

Note that I’m using %x[].sample to randomly pick an image from a directory, but that’s not important.

The code above doesn’t set a mime type and the validation fails. I had to google quite a lot to find the right hints (some solutions have been obsoleted by newer versions of Paperclip and maybe other parts of the toolchain). The solution is

Rack::Test::UploadedFile.new("#{Rails.root}/#{%x[ls test-images/*jpg].split("\n").sample}"), "image/jpg")

which loads the image and sets it’s content type.

Standard
Technology and Software

Espruino, first impact

I received my Espruino yesterday and this is the tale of my first impact with it as a pure software developer with no harware skills to talk about.
Conclusions first: it’s going to be fun and I’ll learn many things. But how long it will take? Read on.
Let’s define the baseline. What do I know about electronics? I know how to change a light bulb, how to connect wires to a plug, a socket, a switch. I know that resistors are for heaters or for making light in tungsten lamps. I know that capacitors are to store energy. I know that solenoids are to make transformers or antennas. That’s it.
So, I got the Espruino, unpacked it, wondered at all those wires in the package and the other little boxes (the Espruino board, a relay, two servos, a temperature sensor, lot of LEDs). There is a printed piece of paper with the url to the Quick Start page. I went there and watched the video. Then I watched this one on YouTube which is even simpler. I suggest you watch it too to understand what going on next.
I’m using Ubuntu Linux and I didn’t have the minicom program used in the example but Ubuntu tells me how to install it. First problem solved.
I connected the Espruino to my netbook with my smartphone data cable which has the same connector. Second problem solved.
I type in the commands in the video and switch the LEDs on and of and make them blink at different speeds. Great!
Then I get to 2:55. “For that you need a battery” and I obviusly have many batteries at home, AA or AAA formats, 1.5 V. Yes I also know about V and A, I’m such genius! :-) Unfortunately that means that I also know that plugging in the wrong battery could burn my Espruino. Which battery are they using? They don’t tell it but probably the specs of the Espruino will help me. Furthermore I don’t have any connector that would fit into that socket and I only have a vague idea about where to buy one. Ok, let’s take a note.
A few seconds later into the video they plug a servo into the board. There are two servos in the box I received. I get one and check it. I realize that I can’t plug it anywhere into the board. It’s got a female 3 pin plug and my board has only holes. But the board in the video is different. It’s got a lot of male pins and I realize now that at 0:20 they said “pinheads we soldered on it”. Oh… soldered. It means a need a solder. Guess what? I don’t have one and I have a vague idea of how to use it. I did it a couple of times in my life on objects much larger than these ones. Oh, I think I’ll also need some tin or lead. Let’s take another note. That’s a new skill to learn.
Luckily the box contains many pinheads. I’ll have to cut them into the sizes of the video but I can probably make it. I think they’ve been using the ones that look like the ones in the picture on this page. Ok, I don’t have the battery and I don’t have the solderer, move on to the next demo.
I have no light sensors in my box (I think) but I have a temperature sensor. I know because it looks exactly like the one in this page and in this one (and that’s my relay!)
Ok, I still don’t have a way to plug it into the board (no solderer!) but let’s pretend I can. How do I plug the temperature sensor? Oh, I need a 4.7 k resistor. I know what it is and maybe it’s one of the resistors in the box but there is nothing written on them. I guess there might be a color code and I’ll ask on the forum. Let’s take a note.
But the real question is why do I need a resistor to make the temperature sensor work? You know, I’m a software developer so my first thought is that the sensor has a bug and we must fix it. Couldn’t they sell it so that it works out of the box? OK, I’m pretty sure that the real answer will be that the sensor is perfectly fine and I need the resistor for some reason that I can’t understand given my almost zero knowledge of electronics. I even found page with a picture of the sensor plus the resistor – it looks like the ones in my box. Let’s move on.
Looking at the relay I see some good news: I know how to connect wires to it, it’s like what’s inside a plug :-)
So, let’s recap: no battery, no idea about the voltage, no connector for the battery, no solderer, no idea about why I need resistors but a guess about which one to use. All of that has hinted me again about why hardware is hard and confirmed my old choice to turn to software. But learning things is great so I’ll google around and find solutions. And I’ll stop by an electronics store and buy some tools.

Standard