Search Social Networks for free username - Moriarty part-1
Tags: ruby, showdev, opensource, tutorial
Published: 2021-04-01 14:03:58

You can find this gem on GitHub and rubygems. Sharing is the best way to donate for my development. This is part of multiple series content about moriarty gem and it's creation, from script to gem. It's intended for beginners 

{% github decentralizuj/moriarty %}  
  
After using Sherlock for a long time (like it!), I decided to create something similar in Ruby. This give me more power to extend software with new features easily, like scrapping info from website, comments from FB, tags from photos etc... For me, ruby is much over python (because of dev happiness, and many ways to do the same thing). When I say _"over"_ python, I talk about personal experience, not about `Ruby VS Python`

---

# Start Project

---

What script should do:
 - take username
 - take website url
 - submit request
 - receive response


For _http_ request I'll go with gem _rest-client,_ and here's why. _RestClient_ response is used to scrap page `html` data with `Nokogiri::HTML` method. If we use `Net::HTTP#get` instead of `RestClient#get` we will get all positive results. But with rest-client in use, nokogiri fail if there's no page to scrap, even if response code is 200. So here I used `rescue` to set `@success` to `false`. This do not work for all sites, so I need to check scrapped data in future.

```ruby
#!/usr/bin/env ruby

require 'rest-client'
require 'nokogiri'

class Moriarty

  # initialize new object, set user and url
  # add https and '/' to the end

  def initialize( name = '', site = 'github.com', type = :https )
    @user = name.to_s
    @url  = type.to_s + '://' + site.to_s
    @url += '/' unless @url.end_with?('/')
  end


  # make request to url
  # accept optional options hash

  def go( opt = {} )
    # use initialized data or enter new
    opt[:user] ||= @user
    opt[:site] ||= @url  

    # construct url from sitename and username
    uri = opt[:site].to_s + opt[:user].to_s

    # make request with rest-client
    @response = RestClient.get uri

    # get page html with nokogiri
    @html = Nokogiri::HTML @response

    # if everything is fine, set and return true
    # otherwise, set and return false
    return @success = true
    rescue
    return @success = false
  end
end
```

Part of job is done. But we must somehow access this variables, so we need to define `attr_reader` (we will create setter methods manually).

```ruby
class Moriarty
  attr_reader :url, :user, :response, :html, :success?
  # ... ...
end
```

Now we can use Moriarty class like this:

```ruby
# This way we can check GitHub, dev, instagram, but we can't
# check linkedin etc... This is because those pages return
# data to nokogiri to scrap, even if no user

  @jim = Moriarty.new( 'moriarty', 'github.com' )
  @jim.go 

  if @jim.success ?
    puts "#{@jim.user} is registered on #{@jim.url}"
  else
    puts "Username #{@jim.user} seems to be free on #{@jim.url}"
  end
```

This post is part of multiple series. In next part I will add all other methods from gem, and in third I will create ruby gem. Last, but not least will explain CLI interface use.

---

> _With next updates I will add methods to scrap from html and check is user registered or is false-positive. If registered, data will be saved in some kind of database (I like CSV for this kind of data, or just a .txt)_

> _Next series will come soon, in next hours..._