Module: OpencodeTheme

Includes:
HTTParty
Defined in:
lib/opencode_theme.rb,
lib/opencode_theme/cli.rb,
lib/opencode_theme/version.rb,
lib/opencode_theme/base_service.rb

Defined Under Namespace

Classes: Cli

Constant Summary collapse

NOOPParser =
Proc.new { |data, format| {} }
TIMER_RESET =
10
PERMIT_LOWER_LIMIT =
3
CONFIG_FILE =
'config.yml'
VERSION =
'1.0.6'.freeze
URL_API =
'https://opencode.tray.com.br'
@@current_api_call_count =
0

Class Method Summary collapse

Class Method Details

.api_usageObject



9
10
11
# File 'lib/opencode_theme/base_service.rb', line 9

def self.api_usage
  ''
end

.asset_listObject



60
61
62
63
64
65
66
# File 'lib/opencode_theme/base_service.rb', line 60

def self.asset_list
  response = opencode_theme.get(path, parser: NOOPParser, query: { gem_version: OpencodeTheme::VERSION })
  if response.code == 200
    assets = response.code == 200 ? parser_json(response.body)['assets'].map { |a| a['key'][1..a['key'].length] } : response.body
    assets
  end
end

.check_configObject



13
14
15
16
# File 'lib/opencode_theme/base_service.rb', line 13

def self.check_config
  response = opencode_theme.post('/api/check', query: { theme_id: config[:theme_id], gem_version: OpencodeTheme::VERSION })
  { success: response.success?, response: parser_json(response.body) }
end

.cleanObject



44
45
46
47
# File 'lib/opencode_theme/base_service.rb', line 44

def self.clean
  response = opencode_theme.post('/api/clean_cache', query: { theme_id: config[:theme_id], gem_version: OpencodeTheme::VERSION })
  { success: response.success?, response: parser_json(response.body) }
end

.configObject



34
35
36
37
38
39
40
41
42
# File 'lib/opencode_theme.rb', line 34

def self.config
  @config ||= if File.exist? CONFIG_FILE
    config = YAML.load(File.read(CONFIG_FILE))
    config
  else
    puts "#{CONFIG_FILE} não encontrado!" unless test?
    {}
  end
end

.config=(config) ⇒ Object



44
45
46
# File 'lib/opencode_theme.rb', line 44

def self.config=(config)
  @config = config
end

.delete_asset(asset) ⇒ Object



80
81
82
83
# File 'lib/opencode_theme/base_service.rb', line 80

def self.delete_asset(asset)
  response = opencode_theme.delete(path, body: { key: "/#{asset}" }, query: { key: "/#{asset}", gem_version: OpencodeTheme::VERSION })
  response
end

.delta_secondsObject



19
20
21
# File 'lib/opencode_theme.rb', line 19

def self.delta_seconds
  Time.now.to_i - @@current_timer.to_i
end

.get_asset(asset) ⇒ Object



68
69
70
71
72
# File 'lib/opencode_theme/base_service.rb', line 68

def self.get_asset(asset)
  response = opencode_theme.get(path, query: { key: "/#{asset}", gem_version: OpencodeTheme::VERSION }, parser: NOOPParser)
  asset = parser_json(response.body)
  asset
end

.ignore_filesObject



62
63
64
# File 'lib/opencode_theme.rb', line 62

def self.ignore_files
  (config[:ignore_files] || []).compact.map { |r| Regexp.new(r) }
end

.install_opencode_components(name, user = nil, origin = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/opencode_theme/base_service.rb', line 28

def self.install_opencode_components(name, user= nil, origin = nil)
  return unless name
  query_default = { gem_version: OpencodeTheme::VERSION }
  query = query_default.merge({ origin: origin, user: user })
  response = opencode_theme.post("/api/themes/#{config[:theme_id]}/components/#{name}/install", query: query)
  response
end

.install_opencode_components_notification(name, user = nil, origin = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/opencode_theme/base_service.rb', line 36

def self.install_opencode_components_notification(name, user= nil, origin = nil)
  return unless name
  query_default = { gem_version: OpencodeTheme::VERSION }
  query = query_default.merge({ origin: origin, user: user, download_component: true })
  opencode_route = "/api/themes/#{config[:theme_id]}/components/#{name}/install/notification"
  response = opencode_theme.post(opencode_route, query: query)
end

.is_binary_data?(string) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/opencode_theme.rb', line 49

def self.is_binary_data?(string)
  if string.respond_to?(:encoding)
    string.encoding == "US-ASCII"
  else
    ( string.count( "^ -~", "^\r\n" ).fdiv(string.size) > 0.3 || string.index( "\x00" ) ) unless string.empty?
  end
end

.listObject



18
19
20
21
# File 'lib/opencode_theme/base_service.rb', line 18

def self.list
  response = opencode_theme.get('/api/list', query: { gem_version: OpencodeTheme::VERSION })
  { success: response.success?, response: parser_json(response.body) }
end

.list_opencode_componentsObject



23
24
25
26
# File 'lib/opencode_theme/base_service.rb', line 23

def self.list_opencode_components
  response = opencode_theme.get('/api/themes/'+config[:theme_id]+'/components', query: { gem_version: OpencodeTheme::VERSION })
  { success: response.success?, response: parser_json(response.body) }
end

.needs_sleep?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/opencode_theme.rb', line 23

def self.needs_sleep?
  critical_permits? && !passed_api_refresh?
end

.parser_json(body) ⇒ Object



85
86
87
# File 'lib/opencode_theme/base_service.rb', line 85

def self.parser_json(body)
  JSON.parse(body)
end

.passed_api_refresh?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/opencode_theme.rb', line 15

def self.passed_api_refresh?
  delta_seconds > TIMER_RESET
end

.path(type = nil) ⇒ Object



57
58
59
# File 'lib/opencode_theme.rb', line 57

def self.path(type = nil)
  @path ||= config[:theme_id] ? "/api/themes/#{config[:theme_id]}/assets" : "/api/themes/assets"
end

.send_asset(data) ⇒ Object



74
75
76
77
78
# File 'lib/opencode_theme/base_service.rb', line 74

def self.send_asset(data)
  data[:gem_version] = OpencodeTheme::VERSION
  response = opencode_theme.put(path, body: data)
  response
end

.sleepObject



27
28
29
30
31
32
# File 'lib/opencode_theme.rb', line 27

def self.sleep
  if needs_sleep?
    Kernel.sleep(TIMER_RESET - delta_seconds)
    @current_timer = nil
  end
end

.test?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/opencode_theme.rb', line 11

def self.test?
  ENV['test']
end

.theme_delete(theme_id) ⇒ Object



49
50
51
52
# File 'lib/opencode_theme/base_service.rb', line 49

def self.theme_delete(theme_id)
  response = opencode_theme.delete("/api/themes/#{theme_id}", parser: NOOPParser, query: { gem_version: OpencodeTheme::VERSION })
  { success: response.success?, response: parser_json(response.body) }
end

.theme_new(_theme_base, theme_name) ⇒ Object



54
55
56
57
58
# File 'lib/opencode_theme/base_service.rb', line 54

def self.theme_new(_theme_base, theme_name)
  response = opencode_theme.post('/api/themes', query: { gem_version: OpencodeTheme::VERSION }, body: { theme: { theme_base: theme_name, name: theme_name, gem_version: OpencodeTheme::VERSION } }.to_json, headers: { 'Content-Type' => 'application/json'}, parser: NOOPParser)
  assets = response.code == 200 ? JSON.parse(response.body)['assets'] : {}
  { success: response.success?, assets: assets, response: parser_json(response.body) }
end

.whitelist_filesObject



66
67
68
# File 'lib/opencode_theme.rb', line 66

def self.whitelist_files
  (config[:whitelist_files] || []).compact
end