Simple example of using MongoMapper with Ruby

The MongoMapper web site is really lacking on even simple examples, especially those that don’t use Rails. So, here’s a simple example that might help someone.

From the Gemfile:

source 'https://rubygems.org'

gem 'mongo_mapper'

And then the application:

require 'mongo_mapper'


# jam it into the database "mm"
MongoMapper.database = "mm"

class App

def create_user
user = User.new(:name => 'Johnny')
puts "user created"
user.save!
end

def find_user
query = User.where(:name => 'Johnny')
user = query.first # just the first
if not user.nil?
puts user.id
end
end

def delete_user
query = User.where(:name => 'Johnny')
user = query.first # just the first

user.destroy
end
end


class User
include MongoMapper::Document
key :name, String
timestamps!
end

app = App.new()
app.create_user
app.find_user
app.delete_user

The code does a few things:

  1. Creates a new user with a single field called name.
  2. Finds the user using the where function
  3. Removes (destroys/deletes) the user

The key things to note are that the where function returns a query and not the actual results. The results are fetched on demand. This is very similar to the extension methods and LINQ in .NET as those functions build a query that executed only when the results are first requested.

The same thing is true of MongoMapper in this case. The results are not returned until the first function is called. Alternatively, all or last could have been used. all of course returning a list of results that could be iterated in a loop.

If there were no results, the result of calling first in this example would be that the user variable would be nil.

The delete_user function above has absolutely no error checking.

Blue Iris 3 and Samsung SmartCam SNH-1011N

If you’ve got a Samsung SmartCam SNH-1011N and you want to use it with Blue Iris 3, I found a reasonable currently undocumented way to make it work. The camera currently isn’t officially supported by Blue Iris, but it can be made to work with a few simple steps. I’ve sent the following information to the Blue Iris developer and ideally it will become part of the standard application.

If you’re not familiar with Blue Iris – it’s an extremely capable “DVR” for many of your IP based web cameras that you may own. I’d highly recommend it. It does not however work with “cloud-only” style IP cameras, like DropCam.

After setting up the camera normally, and updating the firmware to the latest version (at the time of this, it is 1.07_130516), Add a new camera:

image

(I’ve just right-clicked on the Camera’s display).

Then, fill out the name and the other important fields on the General tab:

image

Click the Video tab, and select Network IP and then click the Configure… button.

image

First, select the camera. As this camera isn’t currently officially supported, you’ll need to select the protocol and stream type instead:

image

Select RTSP H.264/MJPG/MPEG4 from the list. It’s generally very near the top of the list. Next, type in the host name or IP address of the camera. It’s whatever you used to set up the camera originally.

image

When you setup the SmartCam, you created a Private Key.

imageHere’s the strange part, type only the first eight (8) characters of the Private Key into the password field:

image

Yes, just the first eight characters. If you use any more than eight, the connection will fail. The camera only uses the first eight.

Finally, in the Video / Path field, type: /profile5/media.smp as shown below:

image

Hit the OK button, do any other setup you’d like, and the camera should be working!

You might want to frame limit (as I was tracing the network traffic, I saw this was the recommended size that Blue Iris switches to automatically):

image

Universal and Ultraviolet, protecting their content?

I was redeeming an Ultraviolet authorization code for a Universal movie, and needed to create an account.

image

Passwords must be between 6 and 8 characters and letters or numbers! I thought the movie industry cared about protecting their assets more than this?

It’s hard to believe in 2013 that there’s a software developer and project manager that went ahead with this idiotic password requirement.

Reading a text file (or JSON file) from a Windows 8 Runtime application

I just had need of reading a small JSON file into a Windows 8 application using the Windows Runtime.

Uri uri = new Uri("ms-appx:///assets/data.json");

var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);

using (var storageStream = await storageFile.OpenReadAsync())
{
using (Stream stream = storageStream.AsStreamForRead())
{
using (StreamReader reader = new StreamReader(stream))
{
var jsonText = reader.ReadToEnd();
var results = JsonConvert.DeserializeObject<DemoData>(jsonText);
this.DataContext = results;
}
}
}

What seemed like it should have been a common example on MSDN was not …, and after a bit of searching and reading, I believe the above code represents at least a common and safe way of loading data.

I’ve got a file called data.json stored in an Assets folder:

image

And the file’s Build Action is set to Content:

image

Change the value passed to the Uri constructor and … the file will be loaded successfully. Once loaded, I used the strongly typed generic DeserializeObject method to convert the Json data to a class called DemoData.

Fitbit Flex, dead and won’t charge?

imageMy wife and I both pre-ordered a Fitbit Flex. While it’s not a life-changing device, it’s been generally fun to wear. I’m always interested in the sleep tracking when I travel … “Awake 16x, slept 5 hours.

Last night though, my wife’s Flex had stopped working. The lights wouldn’t show up when the device was tapped and when she used the charger, it wouldn’t begin to charge. I found a few recommendations to use rubber bands to hold the Flex into the charger, cleaning the contacts, and a few more. Cleaning the contacts didn’t help. The charger worked with my Flex, so that meant the rubber band trick wouldn’t help.

I found though on the Fitbit support website this morning though a suggestion that worked! Apparently, you can restart/reset the tracker without loosing data. The steps are easy:

  1. Plug your charging cable into a USB port and insert your Flex as you normally would.
  2. Insert a paperclip into the small pinhole on the back of the charger (I’d never noticed it before, it’s small!)
  3. Press gently on the pinhole for 3-4 seconds. You’ll feel a gentle ‘click’ when you’ve depressed it far enough (although my “one-thousand, two-thousand, three-thousand” count actually got to 6). The lights flashed a few times, and it started charging a few seconds later.

I’m posting this here as my searching the night before hadn’t turned up that specific support web site page. It may be because the web page is poorly titled and applies to four of their trackers.

Hope this helps someone else.