class RestoreadyTheme::HttpClient

Constants

STARTER_ZIP

Attributes

client[RW]

Public Class Methods

new(api_url = nil, api_key = nil) click to toggle source
# File lib/restoready_theme/http_client.rb, line 8
def initialize(api_url = nil, api_key = nil)
  @api_key = api_key || config[:api_key]
  @client = Faraday.new(url: api_url ||= config[:api_url]) do |conn|
    conn.request :multipart
    conn.request :url_encoded

    conn.adapter :net_http
  end
end

Public Instance Methods

asset_list() click to toggle source
# File lib/restoready_theme/http_client.rb, line 22
def asset_list
  response = client.get do |req|
    req.url "#{basepath}"
    req.headers['Authorization'] = token
    req.headers['Accept'] = 'application/json'
  end

  assets = JSON.parse(response.body)["assets"].collect {|a| a['key'] }
  # Remove any .css files if a .css.liquid file exists
  assets.reject{|a| assets.include?("#{a}.liquid") }
end
basepath() click to toggle source
# File lib/restoready_theme/http_client.rb, line 117
def basepath
  @basepath = "/api/v1/themes/#{config[:theme_id]}/assets"
end
check_theme() click to toggle source
# File lib/restoready_theme/http_client.rb, line 137
def check_theme
  response = client.get do |req|
    req.url "/api/v1/tenant"
    req.headers['Authorization'] = token
    req.headers['Accept'] = 'application/json'
  end
  response
end
config() click to toggle source
# File lib/restoready_theme/http_client.rb, line 103
def config
  @config ||= if File.exist? 'config.yml'
    config = YAML.load(File.read('config.yml'))
    config
  else
    puts "config.yml does not exist!" unless test?
    {}
  end
end
config=(config) click to toggle source
# File lib/restoready_theme/http_client.rb, line 113
def config=(config)
  @config = config
end
create_asset(data) click to toggle source
# File lib/restoready_theme/http_client.rb, line 47
def create_asset(data)
  response = client.post do |req|
    req.url "#{basepath}"
    req.headers['Content-Type'] = 'application/json'
    req.headers['Accept'] = 'application/json'
    req.headers['Authorization'] = token
    req.body = {asset: data}.to_json
  end
  response
end
delete_asset(data) click to toggle source
# File lib/restoready_theme/http_client.rb, line 69
def delete_asset(data)
  response = client.delete do |req|
    req.url "#{basepath}/#{data[:id]}"
    req.headers['Accept'] = 'application/json'
    req.headers['Authorization'] = token
  end
  response
end
get_asset(key) click to toggle source
# File lib/restoready_theme/http_client.rb, line 34
def get_asset(key)
  asset = {}
  response = client.get do |req|
    req.url "#{basepath}/show_by_key?key=#{key}"
    req.headers['Authorization'] = token
    req.headers['Accept'] = 'application/json'
  end

  asset = response.status == 200 ? JSON.parse(response.body)["asset"] : {}
  asset['response'] = response
  asset
end
get_starter() click to toggle source
# File lib/restoready_theme/http_client.rb, line 78
def get_starter
  source = STARTER_ZIP
  response = client.get do |req|
    req.url "#{source}"
    req.headers['Authorization'] = token
    req.headers['Accept'] = 'application/zip'
    req.headers['Accept-Encoding'] = 'gzip'
  end
  response.status == 200 ? response.body : nil
end
ignore_files() click to toggle source
# File lib/restoready_theme/http_client.rb, line 121
def ignore_files
  (config[:ignore_files] || []).compact.map { |r| Regexp.new(r) }
end
install_starter(theme_name) click to toggle source
# File lib/restoready_theme/http_client.rb, line 89
def install_starter(theme_name)
  Dir.mktmpdir do |dir|
    File.open("#{dir}/starter-master.zip", 'wb') { |fp| fp.write(get_starter) }
    response = client.post do |req|
      req.url "/api/v1/themes"
      req.headers['Authorization'] = token
      req.body = {theme: {file: Faraday::UploadIO.new("#{dir}/starter-master.zip", 'application/zip'), name: theme_name}}
    end
    theme = response.status == 200 ? JSON.parse(response.body) : {}
    theme.merge!(response: response)
    theme
  end
end
is_binary_data?(string) click to toggle source
# File lib/restoready_theme/http_client.rb, line 129
def is_binary_data?(string)
  if string.respond_to?(:encoding)
    string.encoding == "UTF-8"
  else
    ( string.count( "^ -~", "^\r\n" ).fdiv(string.size) > 0.3 || string.index( "\x00" ) ) unless string.empty?
  end
end
is_creatable?(asset) click to toggle source
# File lib/restoready_theme/http_client.rb, line 146
def is_creatable?(asset)
  true
end
test?() click to toggle source
# File lib/restoready_theme/http_client.rb, line 18
def test?
  ENV['test']
end
update_asset(data) click to toggle source
# File lib/restoready_theme/http_client.rb, line 58
def update_asset(data)
  response = client.put do |req|
    req.url "#{basepath}/#{data[:id]}"
    req.headers['Content-Type'] = 'application/json'
    req.headers['Accept'] = 'application/json'
    req.headers['Authorization'] = token
    req.body = {asset: data}.to_json
  end
  response
end
whitelist_files() click to toggle source
# File lib/restoready_theme/http_client.rb, line 125
def whitelist_files
  (config[:whitelist_files] || []).compact
end

Private Instance Methods

token() click to toggle source
# File lib/restoready_theme/http_client.rb, line 152
def token
  "Token token=\"#{@api_key}\""
end