Class: DiscourseTheme::Client
- Inherits:
-
Object
- Object
- DiscourseTheme::Client
- Defined in:
- lib/discourse_theme/client.rb
Constant Summary collapse
- THEME_CREATOR_REGEX =
/^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:) ⇒ Object
Constructor Details
#initialize(dir, settings, reset:) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/discourse_theme/client.rb', line 8 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.
6 7 8 |
# File 'lib/discourse_theme/client.rb', line 6 def url @url end |
Class Method Details
.has_needed_version?(current, needed) ⇒ Boolean
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/discourse_theme/client.rb', line 25 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
102 103 104 105 106 107 |
# File 'lib/discourse_theme/client.rb', line 102 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
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/discourse_theme/client.rb', line 58 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
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/discourse_theme/client.rb', line 45 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
118 119 120 |
# File 'lib/discourse_theme/client.rb', line 118 def is_theme_creator @is_theme_creator end |
#root ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/discourse_theme/client.rb', line 109 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
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/discourse_theme/client.rb', line 72 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/discourse_theme/client.rb', line 85 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 |