ferto 0.0.2 → 0.0.3

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
  SHA1:
3
- metadata.gz: 76fec12269881aa711adb29ed09b8e2cbc26293a
4
- data.tar.gz: 88650a43a3c84ccdd7f7a6ae5e0a9f3c60c170b2
3
+ metadata.gz: 97a79db2d44895104a87860165f149d3b08ebe7b
4
+ data.tar.gz: 28bd5801d93445f394629766cf18d6bc2547ece3
5
5
  SHA512:
6
- metadata.gz: c210b51e0d3710ba281a8fb1ce98ac90470c234a0feb582ebfaa821a466e773508ee59d9cadf80cdc0074536e72d6e9ca47ea583e40b616b3cfb0a5e42711e6d
7
- data.tar.gz: 5ec118b21be2e3b7359cb6477d6038dce4a23a5a0ca6526e8e3e5f10086d0e3f770d113f2e9dd33c5b48e0679c30ad8441e32ea2f93f39cac5574d8106f58eb7
6
+ metadata.gz: 7c04fbf4d9943121888bb8d211a4a31d8098e891150154f7c6853e4309d55fb87f46b2c86046fca8589d981d2d7eb5827fee54487d6f8ce12109abcad6bb2690
7
+ data.tar.gz: 05d6a9d9f3f4064d7e461eb57a4702eed647e9fca734e722e21db50ab973a901335fcb0f0e4a9ef339ffe2ecbaa01c5c00a6307d25a74ce50e748a165869177c
data/README.md CHANGED
@@ -35,23 +35,49 @@ client = Ferto::Client.new(
35
35
 
36
36
  ### Downloading a file
37
37
 
38
- ```ruby
39
- client.download(aggr_id: 'bucket1',
40
- aggr_limit: '3',
41
- url: 'http://example.com/',
42
- mime_type: 'text/html',
43
- callback_url: 'http://myservice.com/downloader_callback',
44
- extra: { some_extra_info: 'info' })
38
+ [downloader](https://github.com/skroutz/downloader) supports multiple
39
+ notification backends for the download results. Currently, there are two
40
+ supported options: an HTTP and a Kafka backend. Let's see how to issue a
41
+ download request in each case:
42
+
43
+ #### HTTP-based notification backend
45
44
 
45
+ ```ruby
46
+ dl_resp = client.download(aggr_id: 'bucket1',
47
+ aggr_limit: 3,
48
+ url: 'http://example.com/',
49
+ mime_type: 'text/html',
50
+ callback_type: 'http',
51
+ callback_dst: 'http://myservice.com/downloader_callback',
52
+ extra: { some_extra_info: 'info' })
46
53
  ```
47
54
 
48
- For the semantics of those options refer to [downloader's documentation](https://github.com/skroutz/downloader#endpoints)
55
+ In order for a service to consume downloader's result, it *must* accept the HTTP
56
+ callback in the endpoint denoted by `callback_dst`.
57
+
58
+ #### Kafka-based notification backend
49
59
 
60
+ ```ruby
61
+ dl_resp = client.download(aggr_id: 'bucket1',
62
+ aggr_limit: 3,
63
+ url: 'http://example.com/',
64
+ mime_type: 'text/html',
65
+ callback_type: 'kafka',
66
+ callback_dst: 'my-kafka-topic',
67
+ extra: { some_extra_info: 'info' })
68
+ ```
50
69
 
51
- In order for a service to consume downloader's result, it must accept the HTTP
52
- callback in some endpoint passed in `callback_url`.
70
+ To consume the downloader's result, you can use your favorite Kafka library and
71
+ consume the callback message from `my-kafka-topic` (passed in `callback_dst`).
53
72
 
54
- To consume the callback, e.g. from inside a Rails controller:
73
+ If the connection with the `downloader` API was successful, the aforementioned
74
+ `dl_resp` is a
75
+ [`Ferto::Response`](https://github.com/skroutz/ferto/blob/master/lib/ferto/response.rb#L2)
76
+ object. If the client failed to connect, a
77
+ [`Ferto::ConnectionError`](https://github.com/skroutz/ferto/blob/master/lib/ferto.rb#L18)
78
+ exception is raised.
79
+
80
+ To handle the actual callback message, e.g. from inside a Rails controller:
55
81
 
56
82
  ```ruby
57
83
  class DownloaderController < ApplicationConroller
@@ -71,6 +97,12 @@ class DownloaderController < ApplicationConroller
71
97
  end
72
98
  ```
73
99
 
100
+ > For the detailed semantics of each option and the format of the callback
101
+ > payload, please, refer to the official downloader's documentation ([download
102
+ > parameters](https://github.com/skroutz/downloader#endpoints), [callback
103
+ > payload](https://github.com/skroutz/downloader/tree/kafka-backend#usage)).
104
+
74
105
  ## Contributing
75
106
 
76
- Bug reports and pull requests are welcome on GitHub at https://github.com/skroutz/ferto.
107
+ Bug reports and pull requests are welcome on GitHub at
108
+ https://github.com/skroutz/ferto.
data/lib/ferto.rb CHANGED
@@ -14,4 +14,6 @@ module Ferto
14
14
  timeout: 6,
15
15
  aggr_limit: 4
16
16
  }.freeze
17
+
18
+ class ConnectionError < StandardError; end
17
19
  end
data/lib/ferto/client.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "curl"
2
+ require "json"
3
+
1
4
  module Ferto
2
5
  class Client
3
6
  # @return [String]
@@ -46,27 +49,38 @@ module Ferto
46
49
  #
47
50
  # @example
48
51
  # downloader = Ferto::Client.new
49
- # downloader.download(
52
+ # dl_resp = downloader.download(
50
53
  # aggr_id: 'msystems',
51
54
  # aggr_limit: 3,
52
55
  # url: 'http://foo.bar/a.jpg',
53
- # callback_url: 'http://example.com/downloads/myfile',
56
+ # callback_type: 'http',
57
+ # callback_dst: 'http://example.com/downloads/myfile',
54
58
  # extra: { groupno: 'foobar' }
55
59
  # )
56
60
  #
57
- # @return [Ferto::Client::Response]
61
+ # @raise [Ferto::ConnectionError] if the client failed to connect to the
62
+ # downloader API
63
+ #
64
+ # @return [Ferto::Response]
58
65
  def download(aggr_id:, aggr_limit: @aggr_limit, url:,
59
- callback_url:, mime_type: "", extra: {})
66
+ callback_url: "", callback_dst: "",
67
+ callback_type: "", mime_type: "", extra: {})
60
68
  uri = URI::HTTP.build(
61
69
  scheme: scheme, host: host, port: port, path: path
62
70
  )
63
71
  body = build_body(
64
- aggr_id, aggr_limit, url, callback_url, mime_type, extra)
72
+ aggr_id, aggr_limit, url, callback_url, callback_type, callback_dst,
73
+ mime_type, extra
74
+ )
65
75
  # Curl.post reuses the same handler
66
- res = Curl.post(uri.to_s, body.to_json) do |handle|
67
- handle.headers = build_header(aggr_id)
68
- handle.connect_timeout = connect_timeout
69
- handle.timeout = timeout
76
+ begin
77
+ res = Curl.post(uri.to_s, body.to_json) do |handle|
78
+ handle.headers = build_header(aggr_id)
79
+ handle.connect_timeout = connect_timeout
80
+ handle.timeout = timeout
81
+ end
82
+ rescue Curl::Err::ConnectionFailedError => e
83
+ raise Ferto::ConnectionError.new(e)
70
84
  end
71
85
 
72
86
  Ferto::Response.new res
@@ -77,18 +91,25 @@ module Ferto
77
91
  def build_header(aggr_id)
78
92
  {
79
93
  'Content-Type': 'application/json',
80
- 'X-Aggr': aggr_id.to_s
94
+ 'X-Aggr': aggr_id.to_s
81
95
  }
82
96
  end
83
97
 
84
- def build_body(aggr_id, aggr_limit, url, callback_url, mime_type, extra)
98
+ def build_body(aggr_id, aggr_limit, url, callback_url, callback_type,
99
+ callback_dst, mime_type, extra)
85
100
  body = {
86
101
  aggr_id: aggr_id,
87
102
  aggr_limit: aggr_limit,
88
- url: url,
89
- callback_url: callback_url
103
+ url: url
90
104
  }
91
105
 
106
+ if callback_url.empty?
107
+ body[:callback_type] = callback_type
108
+ body[:callback_dst] = callback_dst
109
+ else
110
+ body[:callback_url] = callback_url
111
+ end
112
+
92
113
  if !mime_type.empty?
93
114
  body[:mime_type] = mime_type
94
115
  end
data/lib/ferto/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ferto
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ferto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aggelos Avgerinos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-26 00:00:00.000000000 Z
11
+ date: 2019-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curb