{ Josh Rendek }

<3 Go & Kubernetes · honeypots · homelab · leadership

Dec 25, 2010 · 1 min

Rails: Implementing fine grained ACLs while staying DRY

There don’t seem to be any ‘dynamic’ ACL modules for rails ( CanCan is kind of there, but not quite ) – I want to be able to modify permissions on the fly, preferably from an administration page.

This is done simply with a Role table and a few methods in application_controller and your User model. This allows you to easily check the serialized Role hash {“foo” => [“edit”, “update”]} by calling current_user.can?(“foo”, “edit”) and you’ll know if they can edit the foo object.

read more

Dec 24, 2010 · 1 min

Rails: Polymorphic routes, models, and forms

I’m currently working on a project where I’m trying to make everything as extensible as possible. I have a Ticket model and I want everything to be ticketable (thats my polymorphic association).

The problem is that I want the URLs to be sensible and still have my polymorphic attribute loaded automatically without having to do to much hacking / un-dry code, so I wanted the URLs to be: /users/1/tickets/new.

This needs to be applied to any arbitrary number of models that has_many :tickets, :as => :ticketable .

read more

Oct 29, 2010 · 1 min

Mac: It just works! Unless you have a mac book pro with a slot load dvd drive

Yesterday I was trying to install Ubuntu 10.10 (occasionally I give linux another try for a desktop) on my Macbook Pro – and boy did I run into issues. First, when I was reformatting my mac, the disc kept ejecting – so finally when I got it to load and stay in, the formatter would error out. After repeating this several dozen times with various CD/DVD’s and different burns of Ubuntu, I ran to Walmart and grabbed a Targus external DVD reader. After my mac kept ejecting cd’s every time I stuck them in, this managed to fix the problem. Everything worked first time, even discs with a few scratches/dust on them.

read more

Oct 16, 2010 · 1 min

Starcraft 2: Freezing when loading map

Starcraft 2 was freezing when I was loading maps ( after I accidentally forcequit it the other night :| ) -after trying the Blizzard Repair tool and uninstalling/reinstalling several times, a simple reboot was all that was needed to fix it. Hope this helps someone else!

read more

Oct 3, 2010 · 2 min

Ruby and Ebay: Get list prices

I’ll eventually be turning my ruby ebay work into a gem, but for now here are little snippets:

Please note that this code automagically takes care of things like 4x SOMETHING – it’ll take the current list price and divide by four, if you don’t want that functionality, remove the divider. You also need rest-open-uri and hpricot for gems.

To get a list of prices:

 1def self.get_search_results(query)
 2            # API request variables
 3            endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';  # URL to call
 4            version = '1.0.0';  # API version supported by your application
 5            appid = 'YOUR APP ID';  # Replace with your own AppID
 6            globalid = 'EBAY-US';  # Global ID of the eBay site you want to search (e.g., EBAY-DE)
 7            safequery = URI.encode(query);  # Make the query URL-friendly
 8
 9            # Construct the findItemsByKeywords HTTP GET call
10            apicall = "#{endpoint}?";
11            apicall += "OPERATION-NAME=findItemsByKeywords";
12            apicall += "&SERVICE-VERSION=#{version}";
13            apicall += "&SECURITY-APPNAME=#{appid}";
14            apicall += "&GLOBAL-ID=#{globalid}";
15            apicall += "&keywords=#{safequery}";
16            apicall += "&paginationInput.entriesPerPage=25";
17
18            res = ""
19
20            open( apicall ).each { |s| res << s }
21            prices = []
22
23
24            doc = Hpricot.parse(res)
25            (doc/:item).each do |x|
26
27                divider = 1
28                get_count = (x/:title).inner_html.scan(/[0-9]/).first
29                p "#{(x/:title).inner_html}"
30                if !get_count.nil?
31                    divider = get_count.to_i
32                end
33                prices << (x/:sellingstatus/:currentprice).inner_html.to_f/divider
34            end
35
36            prices
37end

read more

Sep 15, 2010 · 4 min

Ruby on Rails, NGINX, and Passenger: Setting up the ultimate development environment

My office (underneath the desks) is a mess of cables. I have a dual monitor setup for my mac pro, usually having documentation (or a video) on the second screen, and coding going on in my main 28" HANNS-G. I love developing rails applications on my mac because of the tools I have available to me and how easy it is to access them. However I wanted to keep running applications on linux (since thats where they end up going) and doing some other specific linux things.

read more

Sep 10, 2010 · 1 min

Mac: I'm an IDE whore

I don’t know if I’ve posted about how many IDE’s I use but I’m always switching between TextMate/RubyMine and several others. I will say that I have been sticking more with the Java variant IDEs though since I got my Mac Pro with an SSD…. it makes uber-slow Java apps actually run at a near native speed.

read more

Sep 8, 2010 · 1 min

Ruby: random stat

I have a program that takes a block of texts and then counts the most popular occurrences of words in that text. Processed a 680,354 character string in about 30 minutes on 2 Core i7s and an SSD. Probably could be optimized a bit I think.

Snippet:

def get_keywords_for_content(content, n)
    content = content.gsub(/<\/?[^>]*>/, "")
    words = content.split(/ /)
    # clean up the matches
    words.collect! { |x|
      begin
        x.downcase.match(/[a-zA-Z0-9]+/)[0].chomp
      rescue
        #print "Failed on: #{x} - #{e}\n"
      end
    }
    words.compact!

    occurrences = []
    for w in words
      #p "#{w}: " + self.count_occurrences(w, content).to_s
      occurrences << [count_occurrences(w, content), w] if !@@exclude.include?(w)
    end

    keywords = []

    counter = 0
    for o in occurrences.uniq.sort.reverse
      if counter == n then
        break
      end

      #p "#{o[0]} => #{o[1]}"
      keywords << o[1]

      counter+=1
    end


    return keywords
  end

read more