Sha256: ef0592ba040f0970ea15f4f8f543ec8e68f35f8f81f43a796e3678c4631e4af2

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

require 'httparty'
require 'open-uri'
require_relative 'database'

module Arduino
  module Library
    # This class represents a single entry into the library-index.json file,
    # in other words — a `library.properties` file.

    class DefaultDatabase < Database

      class << self
        attr_accessor :library_index_path,
                      :library_path,
                      :library_index_url

        def instance
          @default ||= self.send(:new)
        end
      end

      self.library_index_path ||= DEFAULT_ARDUINO_LIBRARY_INDEX_PATH
      self.library_index_url  ||= DEFAULT_ARDUINO_LIBRARY_INDEX_URL
      self.library_path       ||= DEFAULT_ARDUINO_LIBRARY_PATH

      attr_accessor :url, :path

      def initialize
        setup
      end

      def setup
        self.url  = self.class.library_index_url
        self.path = self.class.library_index_path

        FileUtils.mkpath(File.dirname(path))

        download_if_needed!

        self.local_file = open_plain_or_gzipped(path)

        load_json
      end

      def download_if_needed!
        if File.exist?(path)
          resp        = HTTParty.head(url)
          remote_size = resp['content-length'].to_i
          local_size  = File.size(path)
          debug("remote: #{remote_size}, local #{local_size}")
          return if remote_size == local_size
          backup_previous_library(path)
        end

        download(url, path)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
arduino-library-0.4.1 lib/arduino/library/default_database.rb
arduino-library-0.4.0 lib/arduino/library/default_database.rb
arduino-library-0.3.2 lib/arduino/library/default_database.rb