Class: RestoreadyTheme::HttpClient

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

Constant Summary collapse

STARTER_ZIP =
"https://codeload.github.com/restoready/starter/zip/master"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url = nil, api_key = nil) ⇒ HttpClient

Returns a new instance of HttpClient.



8
9
10
11
12
13
14
15
16
# 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

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



22
23
24
25
26
27
28
29
30
31
32
# 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

#basepathObject



117
118
119
# File 'lib/restoready_theme/http_client.rb', line 117

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

#check_themeObject



137
138
139
140
141
142
143
144
# 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

#configObject



103
104
105
106
107
108
109
110
111
# 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) ⇒ Object



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

def config=(config)
  @config = config
end

#create_asset(data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



69
70
71
72
73
74
75
76
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# 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_starterObject



78
79
80
81
82
83
84
85
86
87
# 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_filesObject



121
122
123
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
# 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) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/restoready_theme/http_client.rb', line 146

def is_creatable?(asset)
  true
end

#test?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/restoready_theme/http_client.rb', line 18

def test?
  ENV['test']
end

#update_asset(data) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# 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_filesObject



125
126
127
# File 'lib/restoready_theme/http_client.rb', line 125

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