Class: Cnvrg::API

Inherits:
Object
  • Object
show all
Defined in:
lib/cnvrg/api.rb

Constant Summary collapse

USER_AGENT =
"CnvrgCLI/#{Cnvrg::VERSION}"
ENDPOINT_VERSION =
'v1'

Class Method Summary collapse

Class Method Details

.display_error(response) ⇒ Object



103
104
105
# File 'lib/cnvrg/api.rb', line 103

def self.display_error(response)
    "Oops, an error occurred! Reason: #{response['message']}"
end

.endpoint_uriObject



97
98
99
100
# File 'lib/cnvrg/api.rb', line 97

def self.endpoint_uri
    api = get_api()
    return "#{api}/#{Cnvrg::API::ENDPOINT_VERSION}"
end

.get_apiObject



12
13
14
15
16
17
18
19
20
# File 'lib/cnvrg/api.rb', line 12

def self.get_api
    home_dir = File.expand_path('~')
    config = YAML.load_file(home_dir+"/.cnvrg/config.yml")
    if config.empty? or config.to_h[:api].nil?
        return "https://cnvrg.io/api"
    else
      return config.to_h[:api]
    end
end

.request(resource, method = 'GET', data = {}, parse_request = true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cnvrg/api.rb', line 21

def self.request(resource, method = 'GET', data = {}, parse_request = true)
    begin
        n = Netrc.read
    rescue => e
        puts e.message
    end

    # Make sure there is an entry for the Acquia API before generating the
    # requests.
    if n['cnvrg.io'].nil?
        puts 'You\'re not logged in'
        puts 'Please log in via `cnvrg login`'
        return
    end

    @user, @pass = n[Cnvrg::Helpers.netrc_domain]

    conn = Faraday.new
    conn.headers['Auth-Token'] = @pass
    conn.headers['User-Agent'] = "#{Cnvrg::API::USER_AGENT}"

    case method
    when 'GET'
        response = conn.get "#{endpoint_uri}/#{resource}"

        if parse_request == true
            JSON.parse(response.body)
        else
            response
        end
    when 'POST'
        response = conn.post "#{endpoint_uri}/#{resource}", data

        if parse_request == true
            JSON.parse(response.body)
        else
            response
        end
    when 'POST_FILE'
        conn = Faraday.new do |fr|
            fr.headers['Auth-Token'] = @pass
            fr.headers['User-Agent'] = "#{Cnvrg::API::USER_AGENT}"
            fr.headers["Content-Type"] = "multipart/form-data"
            
            fr.request :multipart
            fr.request :url_encoded
            fr.adapter :net_http
        end
        

        # what if windows?
        # data[:file] = Faraday::UploadIO.new(data[:absolute_path], content_type)
        file_base = File.basename(data[:relative_path])
        temp_path = File.expand_path('~')+"/.cnvrg/tmp/#{file_base}"
        FileUtils.touch(temp_path)
        data[:file] = Faraday::UploadIO.new("#{temp_path}", "plain/text")
        response = conn.post "#{endpoint_uri}/#{resource}", data
        FileUtils.rm(temp_path)

        if parse_request == true
            JSON.parse(response.body)
        else
            response
        end
    when 'DELETE'
        response = conn.delete "#{endpoint_uri}/#{resource}", data

        if parse_request == true
            JSON.parse(response.body)
        else
            response
        end
    else
    end
end