Class: TRMNLP::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/trmnlp/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ APIClient

Returns a new instance of APIClient.



10
11
12
# File 'lib/trmnlp/api_client.rb', line 10

def initialize(config)
  @config = config
end

Instance Method Details

#delete_plugin_setting(id) ⇒ Object

Raises:



72
73
74
75
76
77
78
# File 'lib/trmnlp/api_client.rb', line 72

def delete_plugin_setting(id)
  response = conn.delete("plugin_settings/#{id}")

  raise Error, "failed to delete plugin setting: #{response.status} #{response.body}" unless response.status == 204

  true
end

#get_meObject

Raises:



14
15
16
17
18
19
20
# File 'lib/trmnlp/api_client.rb', line 14

def get_me
  response = conn.get('me')

  raise Error, "failed to fetch user info: #{response.status} #{response.body}" unless response.status == 200

  JSON.parse(response.body)['data']
end

#get_plugin_setting_archive(id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/trmnlp/api_client.rb', line 30

def get_plugin_setting_archive(id)
  response = conn.get("plugin_settings/#{id}/archive")

  unless response.status == 200
    raise Error, "failed to download plugin settings archive: #{response.status} #{response.body}"
  end

  temp_file = Tempfile.new(["plugin_settings_#{id}", '.zip'])
  temp_file.binmode
  temp_file.write(response.body)
  temp_file.rewind

  # return the temp file IO
  temp_file
end

#get_plugin_settingsObject

Raises:



22
23
24
25
26
27
28
# File 'lib/trmnlp/api_client.rb', line 22

def get_plugin_settings
  response = conn.get('plugin_settings')

  raise Error, "failed to list plugin settings: #{response.status} #{response.body}" unless response.status == 200

  JSON.parse(response.body)['data']
end

#post_plugin_setting(params) ⇒ Object

Raises:



64
65
66
67
68
69
70
# File 'lib/trmnlp/api_client.rb', line 64

def post_plugin_setting(params)
  response = conn.post('plugin_settings', params.to_json, content_type: 'application/json')

  raise Error, "failed to create plugin setting: #{response.status} #{response.body}" unless response.status == 200

  JSON.parse(response.body)
end

#post_plugin_setting_archive(id, path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/trmnlp/api_client.rb', line 46

def post_plugin_setting_archive(id, path)
  filepart = Faraday::Multipart::FilePart.new(path, 'application/zip')

  payload = {
    file: filepart
  }

  response = conn.post("plugin_settings/#{id}/archive", payload)

  filepart.close

  unless response.status == 200
    raise Error, "failed to upload plugin settings archive: #{response.status} #{response.body}"
  end

  JSON.parse(response.body)
end