klaro-client 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe44df96a08d9b009c290b8d3e50cc31d479762d1d83ec9c6e288ce1f7c3e1eb
4
- data.tar.gz: '06291eca7e8ff30a8d5e5b88ac0ccf8bb62420eed30e8c75bba8c883d675874e'
3
+ metadata.gz: 9612e0a404f699dce37fa9d937c7c5b79758834f058aa125074107f9b5a70468
4
+ data.tar.gz: cb0fe33f9ecf1a43839d162738b5d29e36072179e03c1cf05c44185de85d353d
5
5
  SHA512:
6
- metadata.gz: 394155c941e99cce11ce9193f8bde47a305d6d8b02b17328a65b0bc106f1c7e014af15f317073da2b5bdd16f983a0add1dcdcbb36af88948a7bae1b9d95ccec0
7
- data.tar.gz: 8644b0567fe9725216e8a120e99d0e64c77082cc4b00194b4c9bcc80427cfed083913e8c496804fd02fdf07ce2d7db51b98cf15a85e7adc6ef460d93317e243d
6
+ metadata.gz: fedd2867b05272d285640f822b26cd45b732fa75665525699b51abd5e1da04b2c8f644b177d3547e4e39ca37b1d4d42e626d87193504631f3b8731e01881e830
7
+ data.tar.gz: 046d46ade148f02cac554f74df9cdb45d80f74172941720eec79af395e62fbd357a5969a6c69c71c3224ff45601cbf5767c9b5903dee3e279081d24ec846c809
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1
1
+ 0.3.0
@@ -0,0 +1,10 @@
1
+ module Klaro
2
+ class Client
3
+ class Error < StandardError
4
+ class NoSuchBoardFound < Error
5
+ end
6
+ class AuthenticationFailed < Error
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,70 @@
1
+ module Klaro
2
+ class Client
3
+ class RequestHandler
4
+
5
+ attr_reader :base_url, :token
6
+
7
+ def initialize(url)
8
+ @base_url = url
9
+ end
10
+
11
+ def authenticate(user, password)
12
+ @token = get_token Http
13
+ .headers({
14
+ 'Content-Type' => 'application/json'
15
+ })
16
+ .post("#{base_url}/api/auth/tokens/", payload(user, password))
17
+ end
18
+
19
+ def get(endpoint, raw = false)
20
+ url = "#{base_url}#{endpoint}"
21
+ info("GET `#{url}`")
22
+ response = http.get(url, ssl_context: http_ctx)
23
+ raise Error::NoSuchBoardFound if response.status >= 300 || response.status <200
24
+
25
+ raw ? response.to_s : JSON.parse(response.to_s)
26
+ end
27
+
28
+ def post(endpoint, body)
29
+ url = "#{base_url}#{endpoint}"
30
+ info("POST `#{url}`")
31
+ http.post(url, body.merge(ssl_context: http_ctx))
32
+ end
33
+
34
+ private
35
+ def get_token(response)
36
+ raise Error::AuthenticationFailed if response.status >= 300 || response.status <200
37
+
38
+ response = JSON.parse(response.body)
39
+ "#{response['token_type']} #{response['access_token']}"
40
+ end
41
+
42
+ def payload(user, password)
43
+ { json: {
44
+ grant_type: "client_credentials",
45
+ client_id: user,
46
+ client_secret: password
47
+ },
48
+ ssl_context: http_ctx
49
+ }
50
+ end
51
+
52
+ def http_ctx
53
+ OpenSSL::SSL::SSLContext.new
54
+ end
55
+
56
+ def http
57
+ Http.headers(
58
+ {
59
+ Authorization: @token
60
+ }
61
+ )
62
+ end
63
+
64
+ def info(msg)
65
+ puts msg
66
+ end
67
+ alias error info
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,55 @@
1
+ module Klaro
2
+ class Client
3
+ class Story < Resource
4
+
5
+ def download_and_relocate_attachments(root_path, target_folder, client)
6
+ as = self.attachments.map do |attachment|
7
+ url = attachment["url"]
8
+ url += "?n=" + URI.encode_www_form_component(attachment["filename"]) unless url =~ /\?n=/
9
+ path = handle_image(url, root_path, target_folder, client)
10
+ attachment.merge("url" => path)
11
+ end
12
+ self.class.new(self.to_h.merge(
13
+ attachments: as
14
+ ))
15
+ rescue => ex
16
+ puts ex.message
17
+ puts ex.backtrace.join("\n")
18
+ raise
19
+ end
20
+
21
+ def download_and_relocate_images(root_path, target_folder, client)
22
+ spec = self.specification.gsub(%r{!\[([^\]]*)\]\(([^\)]*)\)}) do
23
+ m = Regexp.last_match
24
+ label, url = m[1], m[2]
25
+ image_relative_path = handle_image(url, root_path, target_folder, client)
26
+ "![#{label}](#{image_relative_path})"
27
+ end
28
+ self.class.new(self.to_h.merge(
29
+ specification: spec
30
+ ))
31
+ end
32
+
33
+ private
34
+
35
+ def handle_image(url, root_path, folder, client)
36
+ image_path = get_image_path(url, folder)
37
+ download_image(url, image_path, client) unless image_path.exists?
38
+ '/' + image_path.relative_to(root_path).to_s
39
+ end
40
+
41
+ def get_image_path(url, folder)
42
+ file_name = URI.parse(url).query.gsub('n=', '')
43
+ file_name = URI.decode_www_form_component(file_name)
44
+ folder/"#{self['card-kind']}/#{identifier}/#{file_name}"
45
+ end
46
+
47
+ def download_image(url, target_file, client)
48
+ target_file.parent.mkdir_p unless target_file.parent.exists?
49
+ img = client.request.get("#{url}", true)
50
+ target_file.write(img)
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,7 @@
1
+ module Klaro
2
+ class Client
3
+ class Resource < OpenStruct
4
+ end # class Resource
5
+ end # class Client
6
+ end # module Klaro
7
+ require_relative 'resource/story'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klaro-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - enspirit
@@ -90,6 +90,10 @@ files:
90
90
  - Rakefile
91
91
  - VERSION
92
92
  - lib/klaro/client.rb
93
+ - lib/klaro/client/errors.rb
94
+ - lib/klaro/client/request_handler.rb
95
+ - lib/klaro/client/resource.rb
96
+ - lib/klaro/client/resource/story.rb
93
97
  homepage:
94
98
  licenses:
95
99
  - MIT