paw_client 0.0.2 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/bin/console +20 -0
- data/bin/setup +8 -0
- data/lib/paw_client.rb +22 -0
- data/lib/paw_client/client.rb +162 -0
- data/lib/paw_client/configuration.rb +43 -0
- data/lib/paw_client/version.rb +3 -0
- metadata +10 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b8fe038780290db7280799dc8c0bf2c68b9a0ec
|
4
|
+
data.tar.gz: 19663cded5a18acac8bfc65fc7fad4c810a90b67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf05c867cbdd0a3adf3b9f5f8898f6ab87ba02cdeed012a5b37ff24f0456d37e00a2a739d5cf443ad99c861320a32f5d9edf2c2db26384f6518d6a200f564cd7
|
7
|
+
data.tar.gz: ee16b8a934bec95d3b555de74745ad131b446869abb27c5c0ed64caa4b15015b840c5cb22b30b21c88e7a316c71c4c2a10bdba893be085c66116cdbc956fd8e9
|
data/Gemfile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "paw_client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
|
11
|
+
config_path=File.expand_path("../../config.yml", __FILE__)
|
12
|
+
if File.exists?(config_path)
|
13
|
+
PawClient.config=PawClient::Configuration.from_yaml(config_path)
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
require "pry"
|
18
|
+
Pry.start
|
19
|
+
# require "irb"
|
20
|
+
# IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/paw_client.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "paw_client/version"
|
2
|
+
require "paw_client/configuration"
|
3
|
+
require "paw_client/client"
|
4
|
+
|
5
|
+
module PawClient
|
6
|
+
|
7
|
+
def self.config(reload=false)
|
8
|
+
if !@config||reload
|
9
|
+
@config=Configuration.new
|
10
|
+
end
|
11
|
+
@config
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.config=(value)
|
15
|
+
@config=value
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configure(reload=false)
|
19
|
+
yield(config(reload))
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'base64'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
class PawClient::Client
|
7
|
+
|
8
|
+
class ClientError < StandardError
|
9
|
+
attr_reader :response
|
10
|
+
|
11
|
+
def initialize(response)
|
12
|
+
super(message)
|
13
|
+
@response = response
|
14
|
+
message=to_json.success rescue 'Error while call plugandwork API, Unparsable response'
|
15
|
+
super(message)
|
16
|
+
end
|
17
|
+
|
18
|
+
def info
|
19
|
+
to_json.info
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_json
|
23
|
+
JSON.parse(self.response.to_s)
|
24
|
+
end
|
25
|
+
|
26
|
+
def request
|
27
|
+
self.response.request
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
attr_accessor :base_url
|
33
|
+
attr_accessor :uuid
|
34
|
+
attr_accessor :token
|
35
|
+
attr_accessor :user_token
|
36
|
+
attr_accessor :logger
|
37
|
+
|
38
|
+
def initialize(attrs={})
|
39
|
+
@base_url = attrs.fetch(:base_url, PawClient.config.base_url)
|
40
|
+
@uuid = attrs.fetch(:uuid, PawClient.config.uuid)
|
41
|
+
@token = attrs.fetch(:token, PawClient.config.token)
|
42
|
+
@user_token = attrs.fetch(:token, PawClient.config.user_token)
|
43
|
+
@logger = attrs.fetch(:logger, PawClient.config.logger)
|
44
|
+
end
|
45
|
+
|
46
|
+
def endpoint_for(path)
|
47
|
+
base_resource[path]
|
48
|
+
end
|
49
|
+
|
50
|
+
def get(path, params={})
|
51
|
+
begin
|
52
|
+
r=endpoint_for(path).get(params: params)
|
53
|
+
JSON.parse(r.body.to_s)
|
54
|
+
rescue RestClient::ExceptionWithResponse => err
|
55
|
+
raise ClientError.new(err.response)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def post(path, payload, params={})
|
60
|
+
begin
|
61
|
+
r=endpoint_for(path).post(payload,params)
|
62
|
+
JSON.parse(r.body.to_s)
|
63
|
+
rescue RestClient::ExceptionWithResponse => err
|
64
|
+
raise ClientError.new(err.response)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def patch(path, payload, params={})
|
70
|
+
begin
|
71
|
+
r=endpoint_for(path).patch(payload,params)
|
72
|
+
JSON.parse(r.body.to_s)
|
73
|
+
rescue RestClient::ExceptionWithResponse => err
|
74
|
+
raise ClientError.new(err.response)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def put(path, payload, params={})
|
79
|
+
begin
|
80
|
+
r=endpoint_for(path).put(payload,params)
|
81
|
+
JSON.parse(r.body.to_s)
|
82
|
+
rescue RestClient::ExceptionWithResponse => err
|
83
|
+
raise ClientError.new(err.response)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def delete(path, params={})
|
88
|
+
begin
|
89
|
+
r=endpoint_for(path).delete(params)
|
90
|
+
JSON.parse(r.to_s)
|
91
|
+
rescue RestClient::ExceptionWithResponse => err
|
92
|
+
raise ClientError.new(err.response)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def create_space(params = {})
|
97
|
+
j = {}
|
98
|
+
j['space']=params
|
99
|
+
res = post('spaces', j)
|
100
|
+
return res
|
101
|
+
end
|
102
|
+
|
103
|
+
def update_space(id, params = {}, patch = true)
|
104
|
+
j = {}
|
105
|
+
j['space']=params
|
106
|
+
if patch
|
107
|
+
res = patch('spaces/'+id, j)
|
108
|
+
else
|
109
|
+
res = put('spaces/'+id, j)
|
110
|
+
end
|
111
|
+
|
112
|
+
return res
|
113
|
+
end
|
114
|
+
|
115
|
+
def delete_space(id)
|
116
|
+
delete('spaces/'+id )
|
117
|
+
end
|
118
|
+
|
119
|
+
def create_doc(filepath = nil,params = {})
|
120
|
+
j = {}
|
121
|
+
j['doc']=params
|
122
|
+
j['doc']['title']||=File.basename(filepath) if filepath
|
123
|
+
res = post('docs', j)
|
124
|
+
if res && res['id'] && filepath
|
125
|
+
res = upload_file(filepath, 'id' => res['id'])
|
126
|
+
end
|
127
|
+
return res
|
128
|
+
end
|
129
|
+
|
130
|
+
def delete_doc(id)
|
131
|
+
delete('docs/'+id )
|
132
|
+
end
|
133
|
+
|
134
|
+
def upload_file(localpath, params={})
|
135
|
+
begin
|
136
|
+
request = RestClient::Request.new(
|
137
|
+
:method => :post,
|
138
|
+
:url => @base_url + "/api/d1/docs/#{params['id']}/file",
|
139
|
+
:payload => {
|
140
|
+
:multipart => true,
|
141
|
+
:file => File.new(localpath, 'rb')
|
142
|
+
},
|
143
|
+
:headers => {'Accept' => 'application/json', 'Authorization': "Bearer #{@user_token}"}
|
144
|
+
)
|
145
|
+
r = request.execute
|
146
|
+
#RestClient::Resource.new(@base_url + '/api/d1/', headers: {'Authorization': "Bearer #{@user_token}"}, lo
|
147
|
+
JSON.parse(r.to_s)
|
148
|
+
rescue RestClient::ExceptionWithResponse => err
|
149
|
+
raise ClientError.new(err.response)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def base_resource(reload=false)
|
154
|
+
if !@base_resource || reload
|
155
|
+
@base_resource=RestClient::Resource.new(@base_url + '/api/d1/', headers: {'Authorization': "Bearer #{@user_token}"}, log: self.logger)
|
156
|
+
end
|
157
|
+
@base_resource
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
class PawClient::Configuration
|
3
|
+
|
4
|
+
attr_accessor :base_url
|
5
|
+
attr_accessor :uuid
|
6
|
+
attr_accessor :token
|
7
|
+
attr_accessor :user_token
|
8
|
+
attr_accessor :test_mode
|
9
|
+
attr_accessor :logger
|
10
|
+
|
11
|
+
def logger=(value)
|
12
|
+
@logger=case value
|
13
|
+
when String
|
14
|
+
case value.downcase
|
15
|
+
when 'stdout'
|
16
|
+
Logger.new(STDOUT)
|
17
|
+
when 'stderr'
|
18
|
+
Logger.new(STDERR)
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
else
|
23
|
+
value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(attrs={})
|
28
|
+
self.base_url = attrs.fetch(:base_url, nil)
|
29
|
+
self.uuid = attrs.fetch(:uuid, nil)
|
30
|
+
self.token = attrs.fetch(:token, nil)
|
31
|
+
self.user_token = attrs.fetch(:user_token, nil)
|
32
|
+
self.logger = attrs.fetch(:logger, nil)
|
33
|
+
self.test_mode = attrs.fetch(:test_mode, false)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.from_yaml(path, env=nil)
|
37
|
+
attrs = YAML.load_file(path)
|
38
|
+
attrs = attrs[env] if env
|
39
|
+
sym_attrs=attrs.each_with_object({}) {|(k, v), memo| memo[k.to_sym] = v}
|
40
|
+
self.new(sym_attrs)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paw_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Olivier DIRRENBERGER
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -114,7 +114,14 @@ email:
|
|
114
114
|
executables: []
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
|
-
files:
|
117
|
+
files:
|
118
|
+
- Gemfile
|
119
|
+
- bin/console
|
120
|
+
- bin/setup
|
121
|
+
- lib/paw_client.rb
|
122
|
+
- lib/paw_client/client.rb
|
123
|
+
- lib/paw_client/configuration.rb
|
124
|
+
- lib/paw_client/version.rb
|
118
125
|
homepage: https://code.plugandwork.net/plugandwork/paw_client
|
119
126
|
licenses:
|
120
127
|
- MIT
|