Sha256: 87f5ea12bf4116ace49378ed05085c850b12a260c585e0bda8404651ae61f919

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

# encoding: utf-8
module Nokaya

  # Inspired by Brett Terpstra

  class AppStore < Apple

    attr_accessor :appname

    def initialize args, options = {}
      super(args, options)
      @type = :appstore
      @appname = @workers.sanitize(args.join('_'))
      @urls = [grab_small(args), grab_big(args)].compact
      find_screenshots(args)
      @path = "#{@path}/appstore-#{@appname}-#{@workers.timed}"
      @filenames = name_files()
      remove_instance_variable(:@res)
    end

    private

    def name_files
      @urls.map.with_index do |url, index|
        "#{@type.to_s}-#{@appname}-#{'%02d' % index}-#{File.basename(url).gsub('ApplicationIcon.', '')}"
      end
    end

    def find_icon terms
      itunes_url = create_url(terms)
      begin
        @res = open(itunes_url).read
      rescue SocketError
        Logs.rec.error 'No connexion'
        @res = {}.to_json
      end
      match = @res.match(/"#{@icon_size}":"(.*?)",/)
      unless match.nil?
        return match[1]
      else
        return nil
      end
    end

    def grab_small args
      @icon_size = "artworkUrl60"
      find_icon(args)
    end

    def grab_big args
      @icon_size = "artworkUrl100"
      find_icon(args)
    end

    def find_screenshots terms
      res = JSON.parse(@res)
      unless res.empty? || res['results'].empty?
        res['results'][0]['screenshotUrls'].each {|link| @urls << link}
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nokaya-0.1.6 lib/nokaya/appstore.rb
nokaya-0.1.5 lib/nokaya/appstore.rb
nokaya-0.1.4 lib/nokaya/appstore.rb
nokaya-0.1.3 lib/nokaya/appstore.rb