aries 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/aries.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "active_support/core_ext/object/try"
2
+ require "active_support/core_ext/string/inflections"
3
+ require "active_support/core_ext/module/delegation"
4
+ require 'json'
5
+ require 'json_schema'
6
+ require 'erubis'
7
+ require 'thor'
8
+ require 'addressable/template'
9
+
10
+ require "aries/version"
11
+ require "aries/error"
12
+ require "aries/schema"
13
+ require "aries/resource"
14
+ require "aries/link"
15
+ require "aries/param"
16
+ require "aries/param_type"
17
+ require "aries/generator"
18
+ require "aries/cli"
19
+
20
+ require "aries/presenters/schema_swift"
21
+ require "aries/presenters/resource_swift"
22
+ require "aries/presenters/link_swift"
23
+ require "aries/presenters/param_swift"
24
+ require "aries/presenters/param_type_swift"
25
+
26
+ module Aries
27
+
28
+ end
data/lib/aries/cli.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Aries
2
+
3
+ class Cli < ::Thor
4
+
5
+ option :output, aliases: "-o"
6
+ option :name, aliases: "-n"
7
+ option :url, aliases: "-u"
8
+ desc "generate SCHEMA_FILE_PATH", "generate api client from schem schema"
9
+ def generate schema_file
10
+ Aries::Generator.generate(schema_file, {}.tap { |opt|
11
+ opt[:output_path] = options[:output] if options[:output]
12
+ opt[:class_name] = options[:name] if options[:name]
13
+ opt[:base_url] = options[:url] if options[:url]
14
+ })
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,3 @@
1
+ module Aries
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,94 @@
1
+ module Aries
2
+
3
+ class Generator
4
+
5
+ def self.generate schema_path, options
6
+ new(schema_path, options).exec
7
+ end
8
+
9
+ #
10
+ # @param file_path [String] json schema file path
11
+ #
12
+ def initialize schema_path, options ={}
13
+ @schema_path = schema_path
14
+ @output_path = options[:output_path] || Dir.pwd
15
+ @class_name = options[:class_name] || "AriesApi"
16
+ @language = options[:language] || "swift"
17
+ @base_url = options[:base_url]
18
+ end
19
+
20
+ # @return [Aries::Presenters::SchemaSwift]
21
+ def schema
22
+ case @language
23
+ when "swift"
24
+ Aries::Presenters::SchemaSwift.new original_schema
25
+ end
26
+ end
27
+
28
+ # @return [Aries::Schema]
29
+ def original_schema
30
+ Schema.new JSON.parse(File.read(@schema_path))
31
+ end
32
+
33
+ # @return [String]
34
+ def class_name
35
+ @class_name.camelcase
36
+ end
37
+
38
+ # @return [String]
39
+ def output_path
40
+ @output_path
41
+ end
42
+
43
+ # @return [String]
44
+ def extension
45
+ case @language
46
+ when "swift" then "swift"
47
+ when "java" then "java"
48
+ end
49
+ end
50
+
51
+ # @return [String]
52
+ def file_name
53
+ class_name + "." + extension
54
+ end
55
+
56
+ # @return [String]
57
+ def base_url
58
+ @base_url || schema.base_url
59
+ end
60
+
61
+ # @return [Array<Aries::Presenters::ResourceSwift>]
62
+ def resources
63
+ schema.resources
64
+ end
65
+
66
+ # @return [String]
67
+ def template_path
68
+ File.expand_path("../templates/client.#{@language}.erb", __FILE__)
69
+ end
70
+
71
+ # @return [String]
72
+ def file_path
73
+ output_path + "/" + file_name
74
+ end
75
+
76
+ # @return [ERB]
77
+ def template
78
+ Erubis::Eruby.new File.read(template_path), trim: true
79
+ end
80
+
81
+ def timestamp
82
+ Time.now.strftime('%Y/%m/%d')
83
+ end
84
+
85
+ # @return [TrueClass, FalseClass]
86
+ def exec
87
+ return puts "File already exists" if File.exist?(file_path)
88
+ File.write file_path, template.result(binding)
89
+ true
90
+ end
91
+
92
+ end
93
+
94
+ end
data/lib/aries/link.rb ADDED
@@ -0,0 +1,105 @@
1
+ module Aries
2
+ class Link
3
+
4
+ URL_REGEXP = /\{\(#([%\/a-zA-Z0-9_-]*)\)\}/
5
+
6
+ attr_reader :parent
7
+
8
+ # @param link [JsonSchema::Schema::Link]
9
+ def initialize link, parent = nil
10
+ @link = link
11
+ @parent = parent
12
+ end
13
+
14
+ # @return [Array<Aries::Param>] Required request parameter
15
+ def required_params
16
+ if @link.schema && @link.schema.required && params
17
+ params.select{|param| @link.schema.required.include? param.name } + url_params
18
+ else
19
+ url_params
20
+ end
21
+ end
22
+
23
+ # @return [Array<Aries::Param>] Optional requrest parameter
24
+ def optional_params
25
+ params - required_params
26
+ end
27
+
28
+ # @return [Array<Aries::Param>]
29
+ def params
30
+ (prop_params + url_params).uniq
31
+ end
32
+
33
+ # @return [Array<Aries::Param]
34
+ def prop_params
35
+ @prop_params ||= if @link.schema && @link.schema.properties
36
+ @link.schema.properties.map do |name, definition|
37
+ Param.new name, definition, self, type: :prop
38
+ end
39
+ else
40
+ []
41
+ end
42
+ end
43
+
44
+ # @return [Array<Aries::Param>]
45
+ def url_params
46
+ @url_params ||= href.scan(URL_REGEXP).flatten.map do |param|
47
+ Param.new param.split('/').last, root_schema.property_by(param), self, type: :url
48
+ end
49
+ end
50
+
51
+ # @return [TrueClass, FalseClass]
52
+ def has_params?
53
+ params.size > 0
54
+ end
55
+
56
+ # @return [String] api client's request method name
57
+ # @example
58
+ # method_name #=> "list"
59
+ def method_name
60
+ title.split.join('_').underscore
61
+ end
62
+
63
+ # @return [String] http request method
64
+ def http_method
65
+ @link.method.to_s.upcase
66
+ end
67
+
68
+ # @return [String] requrest encode type URL encode or JSON encode used in client
69
+ def encode
70
+ case http_method
71
+ when "GET"
72
+ "URL"
73
+ else
74
+ "JSON"
75
+ end
76
+ end
77
+
78
+ # @return [String] convert uri template to {resource_name}_{action_name} template
79
+ def pretty_href
80
+ href.gsub(URL_REGEXP){|n| "{" + n.scan(/definitions\/([a-zA-Z0-9\-\_]+)/)
81
+ .map{|v| v[0].split('-').join('_') }.join('_').camelize(:lower) + "}" }
82
+ end
83
+
84
+ # @return [String] url decoded href string
85
+ def href
86
+ URI.decode @link.href
87
+ end
88
+
89
+ # @return [String]
90
+ def title
91
+ @link.title
92
+ end
93
+
94
+ def inspect
95
+ return "#<Link title=#{title}>"
96
+ end
97
+
98
+
99
+ # @return [Aries::Schema] Root schema of link
100
+ def root_schema
101
+ @parent.respond_to?(:parent) ? @parent.root_schema : @parent
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,75 @@
1
+ module Aries
2
+
3
+ class Param
4
+
5
+ attr_reader :parent
6
+
7
+ def initialize name, definition, parent = nil, options = {}
8
+ @name = name
9
+ @definition = definition
10
+ @parent = parent
11
+ @param_type = options[:type]
12
+ end
13
+
14
+ def name
15
+ @name
16
+ end
17
+
18
+ # @return [String] name of param
19
+ def pretty_name
20
+ if @param_type == :url && has_resource?
21
+ "#{@parent.parent.name}_#{name}"
22
+ else
23
+ name
24
+ end
25
+ end
26
+
27
+ # @return [Array<Aries::Param>]
28
+ def params
29
+ @definition.properties.map do |name, definition|
30
+ Aries::Param.new name, definition, self, type: @param_type
31
+ end
32
+ end
33
+
34
+ # @return [TrueClass, FalseClass] whether contains null or not
35
+ def optional?
36
+ @definition.type.detect{|type| type == "null" } != nil
37
+ end
38
+
39
+ # @return [TrueClass, FalseClass] whether contains child parameters
40
+ def has_children?
41
+ @definition.type.detect{|type| type == "object" } != nil
42
+ end
43
+
44
+ # @return [Array<Aries::Type>]
45
+ def type
46
+ Aries::ParamType.new @definition.type.detect{|type| type != "null"}, self
47
+ end
48
+
49
+ def inspect
50
+ return "#<Aries::Param name=#{name} >"
51
+ end
52
+
53
+ private
54
+
55
+ def param_tree
56
+ trees = [self]
57
+ pr = parent
58
+ while pr.kind_of?(Param)
59
+ trees.unshift pr
60
+ pr = pr.parent
61
+ end
62
+ trees
63
+ end
64
+
65
+ def has_resource?
66
+ if parent.kind_of?(Param)
67
+ parent.has_resource?
68
+ else
69
+ parent.respond_to?(:parent) && parent.parent.kind_of?(Resource)
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,18 @@
1
+ module Aries
2
+
3
+ class ParamType
4
+
5
+ ALLOWED_TYPES = %w{any array boolean integer number null object string}
6
+
7
+ def initialize type, parent = nil
8
+ @type = type
9
+ @parent = parent
10
+ end
11
+
12
+ def type
13
+ @type
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,55 @@
1
+ module Aries
2
+
3
+ module Presenters
4
+
5
+ class LinkSwift
6
+
7
+ delegate :http_method, :href, :pretty_href, :encode, :has_params?, to: :original
8
+
9
+ # @param link [Aries::Link]
10
+ def initialize link
11
+ @link = link
12
+ end
13
+
14
+ # @return [String]
15
+ def method_name
16
+ original.method_name.camelcase
17
+ end
18
+
19
+ [:required_params, :optional_params, :params,
20
+ :prop_params, :url_params].each do |m|
21
+ define_method(m) do
22
+ original.send(m).map{|v| ParamSwift.new v }
23
+ end
24
+ end
25
+
26
+ # @return [String] params string for template
27
+ def params_for_method
28
+ if params.size > 0
29
+ (required_params.map{|v| "#{v.name}: #{v.param_class}" } +
30
+ optional_params.map{|v| "#{v.name}: #{v.param_class}? = nil"}).join(', ')
31
+ else
32
+ ""
33
+ end
34
+ end
35
+
36
+ # @return [String] param keys string for template
37
+ def params_keys
38
+ if params.size > 0
39
+ params.map{|v| v.name}.join(',')
40
+ else
41
+ ""
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def original
48
+ @link
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,41 @@
1
+ module Aries
2
+
3
+ module Presenters
4
+
5
+ class ParamSwift
6
+
7
+ delegate :has_children?, to: :original
8
+
9
+ # @param [Aries::Param]
10
+ def initialize param
11
+ @param = param
12
+ end
13
+
14
+ def name
15
+ original.pretty_name.camelize(:lower)
16
+ end
17
+
18
+ def param_class
19
+ type.param_class
20
+ end
21
+
22
+ def type
23
+ ParamTypeSwift.new original.type
24
+ end
25
+
26
+ def params
27
+ original.params.map{|v| ParamSwift.new v }
28
+ end
29
+
30
+ private
31
+
32
+ # @return [Aries::Param]
33
+ def original
34
+ @param
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end