tumblr / tumblr_client

A Ruby Wrapper for the Tumblr v2 API
Apache License 2.0
224 stars 86 forks source link

Tumblrize search param to allow for more search results #29

Closed lizrush closed 10 years ago

lizrush commented 10 years ago

So, even though tumblr doesn't allow for fuzzy search, it would be nice to have the ability to have the client "tumblrize" search params for possible matches. What I mean by this is:

search_term = "liz m rush"
client.blog_info(search_term)

returns a Bad URI error.
This is simple enough to delete or sub out the whitespaces, however because tumblr is, well... tumblr, it is hard to predict whether a blog uses dashes or not. It would be awesome to have a client.tumblr_search type method that would return an array of blog_info for (in the case of above example) not only "liz-m-rush.tumblr.com" but also "lizmrush.tumblr.com" and other potential smushed together/dash combos where whitespace might be entered, like so:

search_term = "liz m rush"
client.tumblr_search(search_term)

would return (in the case that they exist):

[{"blog"=>{"title"=>"1", "name"=>"lizmrush",...},
{"blog"=>{"title"=>"2", "name"=>"liz-mrush",...}, 
{"blog"=>{"title"=>"3", "name"=>"lizm-rush",...}, 
{"blog"=>{"title"=>"4", "name"=>"liz-m-rush",...}] 
seejohnrun commented 10 years ago

Heya - I do like the idea, but I don't think it's generic enough of a problem to make it's way into this library. But for fun here's a sample implementation:

# Compute possible dasherized variants
def tumblrize(str)
  idxs = (0...str.length).select { |i| str[i] == '-' }
  possibilities = (0..idxs.count + 1).flat_map { |s| idxs.combination(s).to_a }
  possibilities.map do |possibility|
    str.chars.reject.with_index { |c, idx| possibility.include?(idx) }.join
  end
end

# Find the blogs with a given str (dasherized to the max)
def find_blogs(client, str)
  tumblrize(str).map do |blog|
    client.blog_info(blog) rescue nil
  end.compact
end

# Usage
str = 'liz-m-rush'
tumblrize(str) # => ["liz-m-rush", "lizm-rush", "liz-mrush", "lizmrush"]
find_blogs(client, str) # <array of blogs that exist>