Module: Remocon::Request

Defined in:
lib/remocon/command/lib/request.rb

Class Method Summary collapse

Class Method Details

.build_client(config) ⇒ Object



80
81
82
83
84
85
# File 'lib/remocon/command/lib/request.rb', line 80

def self.build_client(config)
  uri = URI.parse(config.endpoint)
  client = Net::HTTP.new(uri.host, uri.port)
  client.use_ssl = true
  return client, uri
end

.fetch_etag(config) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/remocon/command/lib/request.rb', line 63

def self.fetch_etag(config)
  # remote config api doesn't support head request so we need to use GET instead.

  client, uri = Request.build_client(config)

  headers = {
      "Authorization" => "Bearer #{config.token}",
      "Content-Type" => "application/json; UTF8",
      "Content-Encoding" => "gzip",
  }

  request = Net::HTTP::Get.new(uri.request_uri, headers)
  response = client.request(request)

  response.kind_of?(Net::HTTPOK) && response.header["etag"]
end

.pull(config) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/remocon/command/lib/request.rb', line 55

def self.pull(config)
  raw_json, etag = open(config.endpoint, "Authorization" => "Bearer #{config.token}") do |io|
    [io.read, io.meta["etag"]]
  end

  [raw_json, etag]
end

.push(config) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/remocon/command/lib/request.rb', line 5

def self.push(config)
  raise "etag should be specified. If you want to ignore this error, then please add --force option" unless config.etag

  client, uri = Request.build_client(config)

  headers = {
      "Authorization" => "Bearer #{config.token}",
      "Content-Type" => "application/json; UTF8",
      "If-Match" => config.etag,
  }

  request = Net::HTTP::Put.new(uri.request_uri, headers)
  request.body = +""
  request.body << File.read(config.config_json_file_path).delete("\r\n")

  response = client.request(request)

  response_body = begin
    json_str = response.try(:read_body)
    (json_str ? JSON.parse(json_str) : {}).with_indifferent_access
  end

  return response, response_body
end

.validate(config, config_temp_file) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/remocon/command/lib/request.rb', line 30

def self.validate(config, config_temp_file)
  raise "etag should be specified. If you want to ignore this error, then please add --force option" unless config.etag

  client, uri = Request.build_client(config)

  headers = {
      "Authorization" => "Bearer #{config.token}",
      "Content-Type" => "application/json; UTF8",
      "If-Match" => config.etag,
  }

  request = Net::HTTP::Put.new("#{uri.request_uri}?validate_only=true", headers)
  request.body = +""
  request.body << config_temp_file.read.delete("\r\n")

  response = client.request(request)

  response_body = begin
    json_str = response.try(:read_body)
    (json_str ? JSON.parse(json_str) : {}).with_indifferent_access
  end

  return response, response_body
end