Class: PawClient::Client

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

Defined Under Namespace

Classes: ClientError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Client

Returns a new instance of Client.



38
39
40
41
42
43
44
# File 'lib/paw_client/client.rb', line 38

def initialize(attrs={})
  @base_url = attrs.fetch(:base_url, PawClient.config.base_url)
  @uuid     = attrs.fetch(:uuid, PawClient.config.uuid)
  @token = attrs.fetch(:token, PawClient.config.token)
  @user_token = attrs.fetch(:token, PawClient.config.user_token)
  @logger   = attrs.fetch(:logger, PawClient.config.logger)
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



32
33
34
# File 'lib/paw_client/client.rb', line 32

def base_url
  @base_url
end

#loggerObject

Returns the value of attribute logger.



36
37
38
# File 'lib/paw_client/client.rb', line 36

def logger
  @logger
end

#tokenObject

Returns the value of attribute token.



34
35
36
# File 'lib/paw_client/client.rb', line 34

def token
  @token
end

#user_tokenObject

Returns the value of attribute user_token.



35
36
37
# File 'lib/paw_client/client.rb', line 35

def user_token
  @user_token
end

#uuidObject

Returns the value of attribute uuid.



33
34
35
# File 'lib/paw_client/client.rb', line 33

def uuid
  @uuid
end

Instance Method Details

#base_resource(reload = false) ⇒ Object



153
154
155
156
157
158
# File 'lib/paw_client/client.rb', line 153

def base_resource(reload=false)
  if !@base_resource || reload
    @base_resource=RestClient::Resource.new(@base_url + '/api/d1/', headers: {'Authorization': "Bearer #{@user_token}"}, log: self.logger)
  end
  @base_resource
end

#create_doc(filepath = nil, params = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/paw_client/client.rb', line 119

def create_doc(filepath = nil,params = {})
  j = {}
  j['doc']=params
  j['doc']['title']||=File.basename(filepath) if filepath
  res = post('docs', j)
  if res && res['id'] && filepath
    res = upload_file(filepath, 'id' => res['id'])
  end
  return res
end

#create_space(params = {}) ⇒ Object



96
97
98
99
100
101
# File 'lib/paw_client/client.rb', line 96

def create_space(params = {})
  j = {}
  j['space']=params
  res = post('spaces', j)
  return res
end

#delete(path, params = {}) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/paw_client/client.rb', line 87

def delete(path, params={})
  begin
    r=endpoint_for(path).delete(params)
    JSON.parse(r.to_s)
  rescue RestClient::ExceptionWithResponse => err
    raise ClientError.new(err.response)
  end
end

#delete_doc(id) ⇒ Object



130
131
132
# File 'lib/paw_client/client.rb', line 130

def delete_doc(id)
  delete('docs/'+id )
end

#delete_space(id) ⇒ Object



115
116
117
# File 'lib/paw_client/client.rb', line 115

def delete_space(id)
  delete('spaces/'+id )
end

#endpoint_for(path) ⇒ Object



46
47
48
# File 'lib/paw_client/client.rb', line 46

def endpoint_for(path)
  base_resource[path]
end

#get(path, params = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/paw_client/client.rb', line 50

def get(path, params={})
  begin
    r=endpoint_for(path).get(params: params)
    JSON.parse(r.body.to_s)
  rescue RestClient::ExceptionWithResponse => err
    raise ClientError.new(err.response)
  end
end

#patch(path, payload, params = {}) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/paw_client/client.rb', line 69

def patch(path, payload, params={})
  begin
    r=endpoint_for(path).patch(payload,params)
    JSON.parse(r.body.to_s)
  rescue RestClient::ExceptionWithResponse => err
    raise ClientError.new(err.response)
  end
end

#post(path, payload, params = {}) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/paw_client/client.rb', line 59

def post(path, payload, params={})
  begin
    r=endpoint_for(path).post(payload,params)
    JSON.parse(r.body.to_s)
  rescue RestClient::ExceptionWithResponse => err
    raise ClientError.new(err.response)
  end
end

#put(path, payload, params = {}) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/paw_client/client.rb', line 78

def put(path, payload, params={})
  begin
    r=endpoint_for(path).put(payload,params)
    JSON.parse(r.body.to_s)
  rescue RestClient::ExceptionWithResponse => err
    raise ClientError.new(err.response)
  end
end

#update_space(id, params = {}, patch = true) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/paw_client/client.rb', line 103

def update_space(id, params = {}, patch = true)
  j = {}
  j['space']=params
  if patch
    res = patch('spaces/'+id, j)
  else
    res = put('spaces/'+id, j)
  end

  return res
end

#upload_file(localpath, params = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/paw_client/client.rb', line 134

def upload_file(localpath, params={})
  begin
    request = RestClient::Request.new(
          :method => :post,
          :url => @base_url + "/api/d1/docs/#{params['id']}/file",
          :payload => {
            :multipart => true,
            :file => File.new(localpath, 'rb')
          },
          :headers => {'Accept' => 'application/json', 'Authorization': "Bearer #{@user_token}"}
        )
    r = request.execute
    #RestClient::Resource.new(@base_url + '/api/d1/', headers: {'Authorization': "Bearer #{@user_token}"}, lo
    JSON.parse(r.to_s)
  rescue RestClient::ExceptionWithResponse => err
    raise ClientError.new(err.response)
  end
end