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

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
20
# 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)

  parts = discourse_version.split(".").map { |s| s.sub('beta', '').to_i }
  if parts[0] < 2 || parts[1] < 2 || parts[2] < 0 || (!parts[3].nil? && parts[3] < 10)
    Cli.info "discourse_theme is designed for Discourse 2.2.0.beta10 or above"
    Cli.info "download will not function, and syncing destination will be unpredictable"
  end
end

Instance Method Details

#discourse_versionObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/discourse_theme/client.rb', line 77

def discourse_version
  endpoint = root +
    if @is_theme_creator
      "/about.json"
    else
      "/about.json?api_key=#{@api_key}"
    end

  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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/discourse_theme/client.rb', line 35

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

  response = request(Net::HTTP::Get.new endpoint)
  raise "Error downloading theme: #{response.code}" unless response.code.to_i == 200
  response.body
end

#get_themes_listObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/discourse_theme/client.rb', line 22

def get_themes_list
  endpoint = root +
    if @is_theme_creator
      "/user_themes.json"
    else
      "/admin/customize/themes.json?api_key=#{@api_key}"
    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

#update_theme(id, args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/discourse_theme/client.rb', line 48

def update_theme(id, args)
  endpoint = root +
    if @is_theme_creator
      "/user_themes/#{id}"
    else
      "/admin/themes/#{id}?api_key=#{@api_key}"
    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:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/discourse_theme/client.rb', line 61

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

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