Class: Cnvrg::API

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

Constant Summary collapse

USER_AGENT =
"CnvrgCLI/#{Cnvrg::VERSION}"
ENDPOINT =

ENDPOINT = ‘localhost:3000/api

'https://cnvrg.io/api'
ENDPOINT_VERSION =
'v1'

Class Method Summary collapse

Class Method Details

.display_error(response) ⇒ Object



94
95
96
# File 'lib/cnvrg/api.rb', line 94

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

.endpoint_uriObject



89
90
91
# File 'lib/cnvrg/api.rb', line 89

def self.endpoint_uri
    "#{Cnvrg::API::ENDPOINT}/#{Cnvrg::API::ENDPOINT_VERSION}"
end

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



13
14
15
16
17
18
19
20
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
# File 'lib/cnvrg/api.rb', line 13

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 = ".cnvrg/#{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