<commit_before>require 'faraday_middleware'
require 'faraday/rashify'

class ITunes

  attr_accessor :limit, :adapter

  def method_missing(name, *args)
    methods = %q{movie podcast music music_video audiobook short_film tv_show all}

    if methods.include?(name.to_s)
      camelcase = name.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
      camelcase[0] = camelcase[0].chr.downcase

      @limit = args[1].delete(:limit) unless args[1].nil? # will return nil if there is no limit

      request(args.first, camelcase)
    else
      super(name, *args)
    end
  end

  def initialize(limit=nil)
    @limit = limit
    @adapter = Faraday.default_adapter
    @base_uri = 'http://ax.phobos.apple.com.edgesuite.net'
  end

  # So you don't have to create an instance if you don't need to
  def self.music(terms, opts=nil)
    self.new.music(terms, opts)
  end

  def self.podcast(terms, opts=nil)
    self.new.podcast(terms, opts)
  end

  def self.movie(terms, opts=nil)
    self.new.movie(terms, opts)
  end

  def self.music_video(terms, opts=nil)
    self.new.music_video(terms, opts)
  end

  def self.audiobook(terms, opts=nil)
    self.new.audiobook(terms, opts)
  end

  def self.short_film(terms, opts=nil)
    self.new.short_film(terms, opts)
  end

  def self.tv_show(terms, opts=nil)
    self.new.tv_show(terms, opts)
  end

  def self.all(terms, opts=nil)
    self.new.all(terms, opts)
  end

  private
    def request(term, media='all')
      raise ArgumentError, 'you need to search for something, provide a term.' if term.nil?

      #
      query = { :term => term, :media => media }
      query.merge!({ :limit => @limit }) unless @limit.nil? or @limit == 0

      url = '/WebObjects/MZStoreServices.woa/wa/wsSearch'

      response = connection.get do |req|
        req.url url, query
      end
      response.body
    end

    def connection
      @conn ||= Faraday::Connection.new(:url => @base_uri) do |builder|
        builder.adapter(@adapter)
        builder.use Faraday::Response::ParseJson
        builder.use Faraday::Response::Rashify
      end
    end
end<commit_msg>simple refactor of search methods<commit_after>require 'faraday_middleware'
require 'faraday/rashify'

class ITunes

  attr_accessor :limit, :adapter

  def method_missing(name, *args)
    methods = %q{movie podcast music music_video audiobook short_film tv_show all}

    if methods.include?(name.to_s)
      camelcase = name.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
      camelcase[0] = camelcase[0].chr.downcase

      @limit = args[1].delete(:limit) unless args[1].nil? # will return nil if there is no limit

      search(args.first, camelcase)
    else
      super(name, *args)
    end
  end

  def initialize(limit=nil)
    @limit = limit
    @adapter = Faraday.default_adapter
    @base_uri = 'http://ax.phobos.apple.com.edgesuite.net'
  end

  # So you don't have to create an instance if you don't need to
  def self.music(terms, opts=nil)
    self.new.music(terms, opts)
  end

  def self.podcast(terms, opts=nil)
    self.new.podcast(terms, opts)
  end

  def self.movie(terms, opts=nil)
    self.new.movie(terms, opts)
  end

  def self.music_video(terms, opts=nil)
    self.new.music_video(terms, opts)
  end

  def self.audiobook(terms, opts=nil)
    self.new.audiobook(terms, opts)
  end

  def self.short_film(terms, opts=nil)
    self.new.short_film(terms, opts)
  end

  def self.tv_show(terms, opts=nil)
    self.new.tv_show(terms, opts)
  end

  def self.all(terms, opts=nil)
    self.new.all(terms, opts)
  end


  private
    def search(term, media='all')
      raise ArgumentError, 'you need to search for something, provide a term.' if term.nil?

      params = { :term => term, :media => media }
      params.merge!({ :limit => @limit }) unless @limit.nil? or @limit == 0

      request('Search', params)
    end

    def request(request_type, params)
      url = '/WebObjects/MZStoreServices.woa/wa/ws' + request_type

      response = connection.get do |req|
        req.url url, params
      end
      response.body
    end

    def connection
      @conn ||= Faraday::Connection.new(:url => @base_uri) do |builder|
        builder.adapter(@adapter)
        builder.use Faraday::Response::ParseJson
        builder.use Faraday::Response::Rashify
      end
    end
end
{'fraction_non_alphanumeric': 0.13085399449035812, 'fraction_numerical': 0.0032139577594123047, 'mean_word_length': 3.004595588235294, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '52723', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1489, 'n_words': 362}