kril 0.2.1 → 0.2.2

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: 771729deb1ee390248c3ba8e3087da45db562714
4
- data.tar.gz: 55741902dd6394973cf0a8556fe93c513862d924
3
+ metadata.gz: 76311c3631f04d8825304e54631a10d2f854cc0f
4
+ data.tar.gz: dd0fe954dabe659bb5ecac6c04aa269d2a7991c5
5
5
  SHA512:
6
- metadata.gz: 8284333752e8f4707d59ae35ed070a0ed7862b4b3ba655f45370efdba3bc4389b273ffaf67d350d80531aab80d2fa29c3aacd88d5883bb3df83acfbfd61ccb56
7
- data.tar.gz: 8311a73109b2b6a08a2b96aba76d48244388f5d79daa4a1ddb0f366ef477b70768d94edac7e149d56011183b5cd025e16bb2825b52cd9dd56cf303a201a1e65d
6
+ metadata.gz: 54cc317fe57dfb796a0f87f0f88f7440e472048a010f159199f6285263604413b3aa5de054425425d0ddb0c473adb8a5f7d7b85b6943b45a225aa545b73010da
7
+ data.tar.gz: 51c201f5a3175d716431459f459637bb296405986e931584958b93394d215f204cd29c15653102dbf0ed8264b701a0987db4fb42131e2649727b12886f737305
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kril (0.2.1)
4
+ kril (0.2.2)
5
5
  avro_turf (~> 0.8.0)
6
6
  clamp (~> 1.2, >= 1.2.1)
7
7
  ruby-kafka (~> 0.5.3)
@@ -17,7 +17,12 @@ GEM
17
17
  clamp (1.2.1)
18
18
  diff-lcs (1.3)
19
19
  excon (0.61.0)
20
+ httparty (0.13.7)
21
+ json (~> 1.8)
22
+ multi_xml (>= 0.5.2)
23
+ json (1.8.6)
20
24
  multi_json (1.13.1)
25
+ multi_xml (0.6.0)
21
26
  rake (10.5.0)
22
27
  rspec (3.7.0)
23
28
  rspec-core (~> 3.7.0)
@@ -43,6 +48,7 @@ PLATFORMS
43
48
 
44
49
  DEPENDENCIES
45
50
  bundler (~> 1.16)
51
+ httparty (~> 0.13.7)
46
52
  kril!
47
53
  rake (~> 10.0)
48
54
  rspec (~> 3.7)
data/README.md CHANGED
@@ -93,7 +93,8 @@ To see what schemas are saved in the repository:
93
93
  $ kril --list-schemas
94
94
  ```
95
95
  ```bash
96
- human, another_schema
96
+ human
97
+ another_schema
97
98
  ```
98
99
  ## Contributing
99
100
 
data/bin/kril CHANGED
@@ -25,6 +25,7 @@ Clamp do
25
25
  option %w[-g --schema-registry], '', 'address of schema registry', default: 'http://localhost:8081'
26
26
  option %w[-p --schemas-path], '', 'directory of Avro schemas', default: File.join($gem_root, 'schemas').to_s do |path|
27
27
  raise ArgumentError, "Schema path: #{path} is not a directory" unless File.directory?(path)
28
+ path
28
29
  end
29
30
  option %w[-v --verbose], :flag, 'print logs, warnings' do
30
31
  $log.level = Logger::DEBUG
@@ -77,8 +78,8 @@ Clamp do
77
78
  print_error(e.message)
78
79
  end
79
80
  if list_schemas?
80
- schemas = File.join($gem_root, 'schemas', '**', '*.avsc')
81
- puts Dir.glob(schemas).map { |file| File.basename(file, '.avsc') }.join(', ')
81
+ schemas = File.join(schemas_path, '**', '*.avsc')
82
+ Dir.glob(schemas).map { |file| puts File.basename(file, '.avsc') }
82
83
  end
83
84
  end
84
85
 
@@ -86,15 +87,15 @@ Clamp do
86
87
 
87
88
  def produce_record(kafka, avro, store)
88
89
  producer = Kril::Producer.new(kafka: kafka, avro: avro)
89
- names = schema_name(store)
90
+ name, namespace, fullname = schema_name(store)
90
91
  record_as_json = JSON.parse(record)
91
- infered_topic = topic || names[:schema_name]
92
+ topic ||= fullname
92
93
  producer.send(record: record_as_json,
93
- schema_name: names[:schema_name],
94
- namespace: names[:namespace],
95
- topic: infered_topic,
94
+ schema_name: name,
95
+ namespace: namespace,
96
+ topic: topic,
96
97
  syncronous: synchronous?)
97
- print_record(infered_topic, record_as_json)
98
+ print_record(topic, record_as_json)
98
99
  end
99
100
 
100
101
  def consume_records(kafka, avro)
@@ -110,7 +111,8 @@ Clamp do
110
111
 
111
112
  def schema_name(schema_store)
112
113
  handler = Kril::SchemaHandler.new(schema_store: schema_store, schemas_path: schemas_path)
113
- handler.process(schema || topic)
114
+ schema_object = handler.process(schema || topic)
115
+ [schema_object.name, schema_object.namespace, schema_object.fullname]
114
116
  end
115
117
 
116
118
  def print_record(topic, record)
data/kril.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency 'clamp', '~> 1.2', '>= 1.2.1'
27
27
  spec.add_dependency 'ruby-kafka', '~> 0.5.3'
28
28
 
29
+ spec.add_development_dependency 'httparty', '~> 0.13.7'
29
30
  spec.add_development_dependency 'bundler', '~> 1.16'
30
31
  spec.add_development_dependency 'rake', '~> 10.0'
31
32
  spec.add_development_dependency 'rspec', '~> 3.7'
@@ -12,7 +12,7 @@ module Kril
12
12
  def self.extract(source_dir:, output_dir:)
13
13
  find_java_files(source_dir) do |file|
14
14
  schema = parse_avro_java_class(file)
15
- write_avsc(schema, output_dir) if schema
15
+ SchemaHandler.new(schemas_path: output_dir).process(schema)
16
16
  end
17
17
  nil
18
18
  end
@@ -30,14 +30,6 @@ module Kril
30
30
  Dir.chdir(old_dir)
31
31
  end
32
32
 
33
- def write_avsc(contents, directory)
34
- FileUtils.mkdir_p(directory) unless File.directory?(directory)
35
- path = File.join(directory, "#{contents['name']}.avsc")
36
- File.open(path, 'w') do |file|
37
- file.write(JSON.pretty_generate(contents))
38
- end
39
- end
40
-
41
33
  def dejavafy(java_string)
42
34
  java_string.split('","').join.gsub(/\\?\\"/, '"')
43
35
  end
@@ -45,7 +37,7 @@ module Kril
45
37
  def parse_avro_java_class(file)
46
38
  result = file.each_line do |line|
47
39
  extraction = line[/SCHEMA.*parse\("(.*)"\);/, 1]
48
- break JSON.parse(dejavafy(extraction)) if extraction
40
+ break dejavafy(extraction) if extraction
49
41
  end
50
42
  result.is_a?(File) ? nil : result
51
43
  end
@@ -15,28 +15,27 @@ module Kril
15
15
  # Handles input to reference or create schema.
16
16
  #
17
17
  # input_string - schema name, schema file, or schema contents [String]
18
- # returns - schema name, namespace [Hash]
18
+ # returns - stored schema [Avro::Schema]
19
19
  def process(input_string)
20
- if File.exist?(input_string)
21
- copy_schema_to_store(input_string)
22
- elsif schema?(input_string)
23
- save_schema(input_string)
24
- else
25
- name, namespace = separate_fullname(input_string)
26
- namespace = nil if namespace.empty?
27
- schema = @schema_store.find(name, namespace)
28
- {
29
- schema_name: schema&.name,
30
- namespace: schema&.namespace
31
- }
32
- end
20
+ name, namespace =
21
+ if File.exist?(input_string)
22
+ copy_schema_to_store(input_string)
23
+ elsif schema?(input_string)
24
+ save_schema(input_string)
25
+ else
26
+ separate_fullname(input_string)
27
+ end
28
+ @schema_store.find(name, namespace)
33
29
  end
34
30
 
35
31
  private
36
32
 
37
33
  def separate_fullname(fullname)
38
34
  arr = fullname.split('.')
39
- [arr.pop, arr.join('.')]
35
+ name = arr.pop
36
+ namespace = arr.join('.')
37
+ namespace = nil if namespace.empty?
38
+ [name, namespace]
40
39
  end
41
40
 
42
41
  def schema?(input)
@@ -45,38 +44,25 @@ module Kril
45
44
  false
46
45
  end
47
46
 
47
+ def build_path(name, namespace)
48
+ base = namespace ? File.join(@schemas_path, namespace.split('.')) : @schemas_path
49
+ File.join(base, "#{name}.avsc")
50
+ end
51
+
48
52
  def save_schema(schema)
49
53
  schema = JSON.parse(schema)
50
- schema_name = schema['name']
51
- namespace = schema['namespace']
52
- if namespace
53
- path = File.join(@schemas_path, namespace.split('.'))
54
- FileUtils.mkdir_p(path)
55
- end
56
- File.open(File.join(path || @schemas_path, "#{schema_name}.avsc"), 'w') do |file|
54
+ path = build_path(schema['name'], schema['namespace'])
55
+ FileUtils.mkdir_p(File.dirname(path))
56
+ File.open(path, 'w') do |file|
57
57
  file.write(JSON.pretty_generate(schema))
58
58
  end
59
- {
60
- schema_name: schema_name,
61
- namespace: namespace
62
- }
59
+ [schema['name'], schema['namespace']]
63
60
  end
64
61
 
65
62
  def copy_schema_to_store(path)
66
63
  schema = File.read(path)
67
64
  raise ArgumentError, "Not a valid schema: #{path}" unless schema?(schema)
68
- json = JSON.parse(schema)
69
- schema_name = json['name']
70
- namespace = json['namespace']
71
- if namespace
72
- schema_path = File.join(@schemas_path, namespace.split('.'))
73
- FileUtils.mkdir_p(schema_path)
74
- end
75
- FileUtils.copy_file(path, File.join(schema_path || @schemas_path, "#{schema_name}.avsc"))
76
- {
77
- schema_name: schema_name,
78
- namespace: namespace
79
- }
65
+ save_schema(schema)
80
66
  end
81
67
  end
82
68
  end
data/lib/kril/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kril
4
- VERSION = '0.2.1'
4
+ VERSION = '0.2.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kril
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chad Bowman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-18 00:00:00.000000000 Z
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro_turf
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: 0.5.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: httparty
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 0.13.7
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 0.13.7
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: bundler
63
77
  requirement: !ruby/object:Gem::Requirement