Class: DiscourseTheme::Client
- Inherits:
-
Object
- Object
- DiscourseTheme::Client
- Defined in:
- lib/discourse_theme/client.rb
Constant Summary collapse
- THEME_CREATOR_REGEX =
%r{^https://(theme-creator\.discourse\.org|discourse\.theme-creator\.io)$}i
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #discourse_version ⇒ Object
- #get_raw_theme_export(id) ⇒ Object
- #get_themes_list ⇒ Object
-
#initialize(dir, settings, reset:) ⇒ Client
constructor
A new instance of Client.
- #is_theme_creator ⇒ Object
- #root ⇒ Object
- #update_theme(id, args) ⇒ Object
- #upload_full_theme(tgz, theme_id:, components:, skip_migrations: false) ⇒ Object
Constructor Details
#initialize(dir, settings, reset:) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/discourse_theme/client.rb', line 9 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") UI.info "discourse_theme is designed for Discourse 2.3.0.beta1 or above" UI.info "download will not function, and syncing destination will be unpredictable" end end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
7 8 9 |
# File 'lib/discourse_theme/client.rb', line 7 def url @url end |
Class Method Details
.has_needed_version?(current, needed) ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/discourse_theme/client.rb', line 26 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_version ⇒ Object
110 111 112 113 114 115 |
# File 'lib/discourse_theme/client.rb', line 110 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/discourse_theme/client.rb', line 62 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_list ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/discourse_theme/client.rb', line 46 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_creator ⇒ Object
126 127 128 |
# File 'lib/discourse_theme/client.rb', line 126 def is_theme_creator @is_theme_creator end |
#root ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/discourse_theme/client.rb', line 117 def root parsed = URI.parse(@url) # we must strip the username/password so it does not # confuse AWS albs parsed.user = nil parsed.password = nil parsed.to_s end |
#update_theme(id, args) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/discourse_theme/client.rb', line 79 def update_theme(id, args) endpoint = root + (@is_theme_creator ? "/user_themes/#{id}" : "/admin/themes/#{id}") 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:, skip_migrations: false) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/discourse_theme/client.rb', line 87 def upload_full_theme(tgz, theme_id:, components:, skip_migrations: false) endpoint = root + ( if @is_theme_creator "/user_themes/import.json" else "/admin/themes/import.json" end ) params = { "theme_id" => theme_id, "components" => components, "bundle" => UploadIO.new(tgz, "application/tar+gzip", "bundle.tar.gz"), } params["skip_migrations"] = true if skip_migrations post = Net::HTTP::Post::Multipart.new(endpoint, params) request(post) end |