Class: RestoreadyTheme::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/restoready_theme/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url = nil) ⇒ HttpClient

Returns a new instance of HttpClient.



6
7
8
9
# File 'lib/restoready_theme/http_client.rb', line 6

def initialize(api_url = nil)
  # api_url = base_url.nil? ? config[:api_url] : base_url
  @client = ::Faraday.new(url: api_url ||= config[:api_url])
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/restoready_theme/http_client.rb', line 4

def client
  @client
end

Instance Method Details

#asset_listObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/restoready_theme/http_client.rb', line 15

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

#basepathObject



85
86
87
# File 'lib/restoready_theme/http_client.rb', line 85

def basepath
  @basepath = "/api/v1/themes/#{config[:theme_id]}/assets"
end

#check_themeObject



105
106
107
108
109
110
111
112
# File 'lib/restoready_theme/http_client.rb', line 105

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

#configObject



71
72
73
74
75
76
77
78
79
# File 'lib/restoready_theme/http_client.rb', line 71

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) ⇒ Object



81
82
83
# File 'lib/restoready_theme/http_client.rb', line 81

def config=(config)
  @config = config
end

#create_asset(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/restoready_theme/http_client.rb', line 40

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) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/restoready_theme/http_client.rb', line 62

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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/restoready_theme/http_client.rb', line 27

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

#ignore_filesObject



89
90
91
# File 'lib/restoready_theme/http_client.rb', line 89

def ignore_files
  (config[:ignore_files] || []).compact.map { |r| Regexp.new(r) }
end

#is_binary_data?(string) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
# File 'lib/restoready_theme/http_client.rb', line 97

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) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/restoready_theme/http_client.rb', line 114

def is_creatable?(asset)
  true
end

#test?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/restoready_theme/http_client.rb', line 11

def test?
  ENV['test']
end

#update_asset(data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/restoready_theme/http_client.rb', line 51

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_filesObject



93
94
95
# File 'lib/restoready_theme/http_client.rb', line 93

def whitelist_files
  (config[:whitelist_files] || []).compact
end