Class: DiscourseTheme::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_theme/client.rb

Constant Summary collapse

THEME_CREATOR_REGEX =
/^https:\/\/theme-creator.discourse.org$/i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, settings, reset:) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/discourse_theme/client.rb', line 5

def initialize(dir, settings, reset:)
  @reset = reset
  @url = guess_url(settings)
  @api_key = guess_api_key(settings)

  raise "Missing site to synchronize with!" if !@url
  raise "Missing api key!" if !@api_key

  @is_theme_creator = !!(THEME_CREATOR_REGEX =~ @url)

  if !self.class.has_needed_version?(discourse_version, "2.3.0.beta1")
    Cli.info "discourse_theme is designed for Discourse 2.3.0.beta1 or above"
    Cli.info "download will not function, and syncing destination will be unpredictable"
  end
end

Class Method Details

.has_needed_version?(current, needed) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/discourse_theme/client.rb', line 22

def self.has_needed_version?(current, needed)
  current_split = current.split('.')
  needed_split = needed.split('.')

  (0..[current_split.size, needed_split.size].max).each do |idx|
    current_str = current_split[idx] || ''

    c0 = (needed_split[idx] || '').sub('beta', '').to_i
    c1 = (current_str || '').sub('beta', '').to_i

    # beta is less than stable
    return false if current_str.include?('beta') && (c0 == 0) && (c1 > 0)

    return true if c1 > c0
    return false if c0 > c1
  end

  true
end

Instance Method Details

#discourse_versionObject



99
100
101
102
103
104
# File 'lib/discourse_theme/client.rb', line 99

def discourse_version
  endpoint = "#{root}/about.json"
  response = request(Net::HTTP::Get.new(endpoint), never_404: true)
  json = JSON.parse(response.body)
  json["about"]["version"]
end

#get_raw_theme_export(id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/discourse_theme/client.rb', line 55

def get_raw_theme_export(id)
  endpoint = root +
    if @is_theme_creator
      "/user_themes/#{id}/export"
    else
      "/admin/customize/themes/#{id}/export"
    end

  response = request(Net::HTTP::Get.new endpoint)
  raise "Error downloading theme: #{response.code}" unless response.code.to_i == 200
  raise "Error downloading theme: no content disposition" unless response["content-disposition"]
  [response.body, response["content-disposition"].match(/filename=(\"?)(.+)\1/)[2]]
end

#get_themes_listObject



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

def get_themes_list
  endpoint = root +
    if @is_theme_creator
      "/user_themes.json"
    else
      "/admin/customize/themes.json"
    end

  response = request(Net::HTTP::Get.new(endpoint), never_404: true)
  json = JSON.parse(response.body)
  @is_theme_creator ? json["user_themes"] : json["themes"]
end

#is_theme_creatorObject



110
111
112
# File 'lib/discourse_theme/client.rb', line 110

def is_theme_creator
  @is_theme_creator
end

#rootObject



106
107
108
# File 'lib/discourse_theme/client.rb', line 106

def root
  @url
end

#update_theme(id, args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/discourse_theme/client.rb', line 69

def update_theme(id, args)
  endpoint = root +
    if @is_theme_creator
      "/user_themes/#{id}"
    else
      "/admin/themes/#{id}"
    end

  put = Net::HTTP::Put.new(endpoint, 'Content-Type' => 'application/json')
  put.body = args.to_json
  request(put)
end

#upload_full_theme(tgz, theme_id:, components:) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/discourse_theme/client.rb', line 82

def upload_full_theme(tgz, theme_id:, components:)
  endpoint = root +
    if @is_theme_creator
      "/user_themes/import.json"
    else
      "/admin/themes/import.json"
    end

  post = Net::HTTP::Post::Multipart.new(
    endpoint,
    "theme_id" => theme_id,
    "components" => components,
    "bundle" => UploadIO.new(tgz, "application/tar+gzip", "bundle.tar.gz")
  )
  request(post)
end