Class: Arduino::Library::DefaultDatabase

Inherits:
Database
  • Object
show all
Defined in:
lib/arduino/library/default_database.rb

Overview

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Database

#db_list, #local_file

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Database

#search

Methods included from Utilities

#backup_previous_library, #debug, #download, #open_plain_or_gzipped, #read_file_or_url, #short_time

Constructor Details

#initializeDefaultDatabase

Returns a new instance of DefaultDatabase.



38
39
40
# File 'lib/arduino/library/default_database.rb', line 38

def initialize
  reload!
end

Class Attribute Details

.library_index_pathObject

Returns the value of attribute library_index_path.



13
14
15
# File 'lib/arduino/library/default_database.rb', line 13

def library_index_path
  @library_index_path
end

.library_index_urlObject

Returns the value of attribute library_index_url.



13
14
15
# File 'lib/arduino/library/default_database.rb', line 13

def library_index_url
  @library_index_url
end

.library_pathObject

Returns the value of attribute library_path.



13
14
15
# File 'lib/arduino/library/default_database.rb', line 13

def library_path
  @library_path
end

.url_size_cacheObject

Returns the value of attribute url_size_cache.



13
14
15
# File 'lib/arduino/library/default_database.rb', line 13

def url_size_cache
  @url_size_cache
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



36
37
38
# File 'lib/arduino/library/default_database.rb', line 36

def path
  @path
end

#urlObject

Returns the value of attribute url.



36
37
38
# File 'lib/arduino/library/default_database.rb', line 36

def url
  @url
end

Class Method Details

.assign_defaultsObject



26
27
28
29
30
31
# File 'lib/arduino/library/default_database.rb', line 26

def assign_defaults
  self.url_size_cache     ||= {}
  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
end

.instanceObject



18
19
20
# File 'lib/arduino/library/default_database.rb', line 18

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

.reload!Object



22
23
24
# File 'lib/arduino/library/default_database.rb', line 22

def reload!
  instance.reload!
end

Instance Method Details

#download_if_needed!Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/arduino/library/default_database.rb', line 55

def download_if_needed!
  if File.exist?(path)
    remote_size = get_remote_size(url)
    local_size  = File.size(path)
    debug("remote size: #{remote_size}, local size: #{local_size}")
    return if remote_size == local_size
    backup_previous_library(path)
  end
  download(url, path)
end

#get_remote_size(url) ⇒ Object



66
67
68
69
70
71
# File 'lib/arduino/library/default_database.rb', line 66

def get_remote_size(url)
  with_caching(url) do
    resp = HTTParty.head(url)
    resp['content-length'].to_i
  end
end

#reload!Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/arduino/library/default_database.rb', line 42

def reload!
  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

#with_caching(url, &_block) ⇒ Object



73
74
75
76
# File 'lib/arduino/library/default_database.rb', line 73

def with_caching(url, &_block)
  @cache ||= self.class.url_size_cache
  @cache[url] ||= yield
end