News

triopticon.com: Bringing in the JSON

Added by Matt Platte over 1 year ago

As preparation for making the Photo Tagger project, I attempt to communicate with the server via jQuery/JSON.

It begins with attaching events to buttons (I didn't know about the <button tag!) to raise the alert. Then this stuff begins, never to completely go away. This is the biggest chunk of magic that I'm seeing right now:

      
  //jsonURL = "http://192.168.1.2:3000/feeds.json?callback=?"; // fails in Rails
  //jsonURL = "http://192.168.1.2:3000/feeds.json?callback=Bullhockey"; // works in Rails
  jsonURL = "http://192.168.1.2:3000/feeds.json"; // note: this one works in Rails

... not that any of these are the Right Answer® yet.
Takes a really long time to get JSON from the Rails into the page. Not knowing how to debug Javascript doesn't help. Not knowing what's busted doesn't help. Silent failure isn't my friend. Once I get something all the way from Rails to visible-on-the-page I then look to building a select/options control.

And building that select control took considerable time. Eventually settled on constructing HTML tags instead of using the Javascript Option object. Might come back to that sometime. Delays all along the way because of the callback magic mentioned above.

Eventually I have a chunk of Javascript/HTML and a feed_controller in Rails that more or less talk to each other. Lots of trial and error. Then more trial and error. One thing I notice is that while LiveHTTPHeaders doesn't show the delivery package, it does indicate its size. By altering the data I send to the server I get confirmation that Rails is actually returning the desired object on a successful save into the database. There should be a better way....

Sample code that kinda works:

<script type="text/javascript">
  $(document).ready(function(){
    console.log("_1 document ready");
    larry();
    $('#pik').bind('click', pikker);
    $('#rik').bind('click', rikker);

  }); // doc.ready
    /* [
    {"feed":{"title":"Walla","id":1,"description":"The long divide"}},
    {"feed":{"title":"Wander","id":2,"description":"Along the riparian road"}},
    {"feed":{"title":"Wishful","id":3,"description":"Daydreams in the meadow"}}
    ] */
  function larry(){
    var stURL = "http://192.168.20.2:3000/feeds.json?callback=?";
    console.log("stURL=" +stURL);
    $('#stutton').click(function(){
      $.getJSON(stURL, function(data){
        var htm = '<option value=""></option>';
        $.each(data, function(i,slug){
//          $("#millertime").append('&lt;option>* '+slug.feed.title+'&lt;option>');
          $("#millertime").append('.');
          htm += '<option value="'+slug.feed.id+'">'+slug.feed.title+'</option>';
        }); // .each
        $("#sally").append(htm);
      }); //getJSON stURL
    }); //stutton.click
  }; // larry
  function pikker(){ 
    console.log("Ahahahah pikker!");
    var postURL = "http://192.168.20.2:3000/feeds.json";
    var drek = $("#pikform").serialize();
    console.log(drek);
    $.post(postURL, drek, function(data){
        console.log("pikker got something back from the serva!");
      });

  }; // pikker
  function rikker(){ 
    console.log("Why hello, rikker! Yes, you may read my comics.");
    var postURL = "http://192.168.20.2:3000/feeds.json";
    var drek = $("#pikform").serialize();
    console.log(drek);
    $.ajax({
      type: "POST",
      url: postURL,
      data: drek,
      success: function(){
        console.log("okay");
      }
    });
  }; // rikker
</script>

<h3>Call and Respond</h3>
<p>This form gets posted to the Rails database, to populate the select control below.</p>
<div id="success" style="display:none;">Aha!</div>
<table><tr><td>title</td><td>description</td><td>buttons</td></tr><tr><td>
<form id="pikform">
  <input id="feed_title" type="text" name="feed[title]" /></td><td>
  <input id="feed_description" type="text" name="feed[description]" /></td><td>
</form>
<button id="pik">&nbsp; pik &nbsp;</button>
<button id="rik">&nbsp; rik &nbsp;</button>
</td></tr></table>

<h3>How To Read JSON Using JQuery</h3>
<p>From this 
<a href="http://webhole.net/2009/11/28/how-to-read-json-with-javascript/">tutorial</a> 
that describes pulling stuff from Twitter's API.  I cloned the above, but pointed 
it to my test JSON feed at jsonURL ~= "http://192.168.20.2:3000/feeds.json?callback=Bullhockey".</p>
<table><tr><td>
<button id="stutton">&nbsp; stutton button &nbsp;</button></td><td>
<div id="millertime"></div></td><td>
<select id="sally"></select></td></tr></table>
<pre> e n d</pre>

Wow, the nested <pre> tags above didn't fail!

BreadBox: After git pull in a new development suburb

Added by Matt Platte over 1 year ago

Random notes

  • Title that begin with QT will not attempt to update twitter status. This should let the development machine actually start serving pages.

Get that new OAuth thingy to working

Box Auction 808: Proxibid - the Video

Added by Matt Platte over 1 year ago

www.siliconprairienews.com/2010/09/how-it-s-done-proxibid-in-action

  • John Anderson, a senior Proxibid agent said, "Tomorrow, for the auction, I will be in the tuck with the auctioneer relaying the bids, so every time they bid, I will be the one representing their bid physically at the auction."
  • Jeff Taverner of Gunslinger Auctions said, "Our average auction before Proxibid was somewhere between 50 and 75 thousand dollars for the day. Our average, now it's more than double that."

Box Auction 808: uvccapture FTW (13 comments)

Added by Matt Platte over 1 year ago

The Logitech gang provide/link to uvccapture. It's kind of old, so I'm attaching the source files.
  • tar xv --bzip2 -f uvccapture-0.5.tar.bz2
  • cd uvccapture-0.5
  • make
  • sudo make install
    ...should be sufficient.

Box Auction 808: cats and tags (3 comments)

Added by Matt Platte almost 2 years ago

How about this: One Category, Many Tags

BreadBox: What I Did On My Holiday Break

Added by Matt Platte about 2 years ago

Hirb is terrific!

I've been wanting to build a traffic-tracker into triopticon.com just for the heck of it. Underneath this idle wish is a faint frustration with log files. I've always used an Apache Proxy to bounce requests to and fro which causes all requests in production.log to look like they're from portsmouth, while the useful traffic data is over there in the Apache log file. I'm too lazy to get awstats organized (more about that directly, in the Systems Administration project); besides, this is more fun.

The code is simple enough:

  before_filter :guest_list, :only => [:index, :show]
  def guest_list
    @visitor = Visitor.new
    @visitor.ip = request.remote_ip
    @visitor.url = request.path
    @visitor.save!
  end

But all the entries are from portsmouth! Dang. Time for Phusion Passenger/mod_rails and boy was that ever easy! I've set this up a couple of times on Edison for development, and for some odd reason figured it would be complicateder on the server. Not so.

And isn't Hirb wonderful?

Loading production environment (Rails 2.3.4)
>> require 'hirb'
=> true
>> Hirb.enable
=> true
>> Visitor.all
+----+-----------------+--------+-----+--------------------------------+--------------------------------+
| id | ip              | domain | url | created_at                     | updated_at                     |
+----+-----------------+--------+-----+--------------------------------+--------------------------------+
| 1  | 000.000.000.204 |        | /   | Tue Dec 29 14:16:22 -0600 2009 | Tue Dec 29 14:16:22 -0600 2009 |
| 2  | 000.000.000.204 |        | /   | Tue Dec 29 14:16:34 -0600 2009 | Tue Dec 29 14:16:34 -0600 2009 |
| 3  | 000.000.000.204 |        | /   | Tue Dec 29 14:16:40 -0600 2009 | Tue Dec 29 14:16:40 -0600 2009 |
| 4  | 000.000.000.204 |        | /   | Tue Dec 29 14:20:45 -0600 2009 | Tue Dec 29 14:20:45 -0600 2009 |
| 5  | 000.000.000.205 |        | /   | Tue Dec 29 14:36:00 -0600 2009 | Tue Dec 29 14:36:00 -0600 2009 |
| 6  | 000.000.000.205 |        | /   | Tue Dec 29 14:36:08 -0600 2009 | Tue Dec 29 14:36:08 -0600 2009 |
+----+-----------------+--------+-----+--------------------------------+--------------------------------+
6 rows in set
>> 

Note: 000.000.000.204 is portsmouth and 000.000.000.205 is the public IP of my internal network.

Box Auction 808: Loading external data

Added by Matt Platte about 2 years ago

Here's the result. Ignoring the first couple of runs that failed due to typos, I was pleased to see it fail due to the lack of a categories table. Rake fixed that.

Edison:~/Desktop/ebay $ ruby ebay_loader.rb 
------
37

And the code itself:

#!/usr/local/bin/ruby
#
require 'yaml'
require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :database => "box_auction_808_dev",
  :username => "root",
  :password => "")

class Category < ActiveRecord::Base
end

categories = YAML::load( File.open('ebay_categories.yml', 'r') )

categories["categories"].each do |k,v|
  @c = Category.new
  @c.name = k
  @c.save!
end

puts "------" 
@allc = Category.all
puts @allc.length

Finally, the categories which I shamelessly copied from ebay:

categories:
  Antiques:
  Art:
  Baby:
  Books:
  Business & Industrial:
  ...

Box Auction 808: Scientists say "begin the bidding at a low value"

Added by Matt Platte about 2 years ago

An article in the latest edition of Current Directions in Psychological Science reviews studies on the best starting points to increase the final price in either negotiations or auctions. In general, start high in negotiations, start low in auctions.

Mind Hacks

One day I should attempt to put this in front of the Goodwill online managers. Their minimums are way too high.

Box Auction 808: Changes to the Ancestry Gem (2 comments)

Added by Matt Platte about 2 years ago

In acts_as_tree.rb changes were made 1) add the 'validate_uuid' option (which gets set in the Thing model) and 2) the added code in the validation section:

       unless [:validate_uuid, :ancestry_column, :orphan_stra... 
      # Validate format of ancestry column value
      # original author assumed integer primary key.
      if options[:validate_uuid]
        # but I want to use UUID...
        validates_format_of ancestry_column, :with => /\A[0-9,a-f,\-]+(\/[0-9,a-f,-]+)*\Z/, :allow_nil => true
      else
        validates_format_of ancestry_column, :with => /\A[0-9]+(\/[0-9]+)*\Z/, :allow_nil => true
        # so here's my change.
      end

Box Auction 808: UUID as Primary Key (3 comments)

Added by Matt Platte about 2 years ago

Google shows a bit about setting Rails' primary key but that'll only take you part-way. To make it actually work for UUID, do something like

mysql> ALTER TABLE `things` ADD PRIMARY KEY `uuid`; 

I did that just a few minutes ago and it is looking like all that code I recently changed in my application (:id => :uuid) needs to go back to ":id". Which is kinda weird. In Rails the primary key is always called :id even when it's not :id? Magic!

1 2 3 ... 5 Next »

Also available in: Atom