Class: Appwrite::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/appwrite/client.rb

Constant Summary collapse

METHOD_GET =
'get'
METHOD_POST =
'post'
METHOD_PUT =
'put'
METHOD_PATCH =
'patch'
METHOD_DELETE =
'delete'
METHOD_HEAD =
'head'
METHOD_OPTIONS =
'options'
METHOD_CONNECT =
'connect'
METHOD_TRACE =
'trace'

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



19
20
21
22
23
24
25
26
# File 'lib/appwrite/client.rb', line 19

def initialize()
    @headers = {
        'content-type' => '',
        'user-agent' => RUBY_PLATFORM + ':ruby-' + RUBY_VERSION,
        'x-sdk-version' => 'appwrite:ruby:1.0.4'
    }
    @endpoint = 'https://appwrite.io/v1';
end

Instance Method Details

#add_header(key, value) ⇒ Object



58
59
60
61
62
# File 'lib/appwrite/client.rb', line 58

def add_header(key, value)
    @headers[key.downcase] = value.downcase

    return self
end

#call(method, path = '', headers = {}, params = {}) ⇒ Object



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
# File 'lib/appwrite/client.rb', line 64

def call(method, path = '', headers = {}, params = {})
    uri = URI.parse(@endpoint + path + ((method == METHOD_GET) ? '?' + encode(params) : ''))
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (uri.scheme == 'https')
    payload = ''
    
    headers = @headers.merge(headers)

    if (method != METHOD_GET)
        case headers['content-type'][0, headers['content-type'].index(';') || headers['content-type'].length]
            when 'application/json'
                payload = params.to_json
            else
                payload = encode(params)
        end
    end

    begin
        response = http.send_request(method.upcase, uri.request_uri, payload, headers)
    rescue => error
        raise 'Request Failed: '  + error.message
    end

    return JSON.parse(response.body);
end

#set_endpoint(endpoint) ⇒ Object



52
53
54
55
56
# File 'lib/appwrite/client.rb', line 52

def set_endpoint(endpoint)
    @endpoint = endpoint
    
    return self
end

#set_key(value) ⇒ Object



34
35
36
37
38
# File 'lib/appwrite/client.rb', line 34

def set_key(value)
    add_header('x-appwrite-key', value)

    return self
end

#set_locale(value) ⇒ Object



40
41
42
43
44
# File 'lib/appwrite/client.rb', line 40

def set_locale(value)
    add_header('x-appwrite-locale', value)

    return self
end

#set_mode(value) ⇒ Object



46
47
48
49
50
# File 'lib/appwrite/client.rb', line 46

def set_mode(value)
    add_header('x-appwrite-mode', value)

    return self
end

#set_project(value) ⇒ Object



28
29
30
31
32
# File 'lib/appwrite/client.rb', line 28

def set_project(value)
    add_header('x-appwrite-project', value)

    return self
end