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



111
112
113
114
115
116
# File 'lib/paw_client/client.rb', line 111

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



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

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

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



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

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



88
89
90
# File 'lib/paw_client/client.rb', line 88

def delete_doc(id)
  delete('docs/'+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)
    JSON.parse(r.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.to_s)
  rescue RestClient::ExceptionWithResponse => err
    raise ClientError.new(err.response)
  end
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/paw_client/client.rb', line 92

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