mobiorm_client 1.0.3

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/mobiorm +50 -0
  3. data/lib/mobiorm.rb +97 -0
  4. metadata +74 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6ebf5d367c104afa9dd726f627e7a3e2bd28d34
4
+ data.tar.gz: ddeec00a78c44373393376989f3069f5ae38f6ec
5
+ SHA512:
6
+ metadata.gz: f0ebdca506bc75032cd1daa9f5ea90de88097853af5a90729ab77cded41aa8d5a771c6706e918924f410f38c9c393ffb3597cd2663df14d5a589ae3205f3db17
7
+ data.tar.gz: eeeec8d16711c5c18e1c53032c3d482a1af71f74d36ab64bf4b8b8c496d99cf0f454d9e534732f47d41e1109c8084d2cf10b06811f532ba869abc753b3c7b4cb
data/bin/mobiorm ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mobiorm'
4
+ ARGV.each do |arg|
5
+ if arg =~ /^mode=(vertabelo|xml)$/
6
+ @mode = "#{$1}"
7
+ elsif arg =~ /^output=(.+)$/
8
+ @dest = "#{$1}"
9
+ elsif arg =~ /^file=(.+)$/
10
+ @xml = "#{$1}"
11
+ elsif arg =~ /^classPrefix=(.*)$/
12
+ @classPrefix = "#{$1}"
13
+ elsif arg =~ /^propertyPrefix=(.*)$/
14
+ @propertyPrefix = "#{$1}"
15
+ elsif arg =~ /^token=(.+)$/
16
+ @token = "#{$1}"
17
+ elsif arg =~ /^model=(.+)$/
18
+ @id = "#{$1}"
19
+ elsif arg =~ /^tag=(.+)$/
20
+ @tag = "#{$1}"
21
+ else
22
+ next
23
+ end
24
+ end
25
+
26
+ puts "\"mode\" argument is required and has to be either \"xml\" or \"vertabelo\"." unless @mode
27
+ @dest = "." unless @dest
28
+
29
+ if @dest
30
+ puts "output argument contains invalid path: " + @dest + "." unless File.exists? @dest
31
+ end
32
+
33
+ if @mode == "xml"
34
+ if not @xml
35
+ puts "For \"xml\" mode, \"file\" argument is required and it has to be a path to the Vertabelo XML file."
36
+ elsif not File.exists? @xml
37
+ puts "File given in \"file\" argument does not exist: " + @xml + "."
38
+ else
39
+ Mobiorm.getOrmFromFile(@dest, @xml, @classPrefix, @propertyPrefix)
40
+ end
41
+ elsif @mode == "vertabelo"
42
+ if not @token
43
+ puts "For \"vertabelo\" mode, \"token\" argument is required and it has to be your API token."
44
+ elsif not @id
45
+ puts "For \"vertabelo\" mode, \"model\" argument is required and it has to be your model identifier."
46
+ else
47
+ Mobiorm.getOrmFromVertabelo(@dest, @token, @id, @tag, @classPrefix, @propertyPrefix);
48
+ end
49
+ end
50
+
data/lib/mobiorm.rb ADDED
@@ -0,0 +1,97 @@
1
+
2
+ require "rest-client"
3
+ require "tempfile"
4
+ require "zipruby"
5
+ require "json"
6
+ require "base64"
7
+
8
+ class Mobiorm
9
+
10
+ MOBIORM_VERSION = "1.0"
11
+ HTTP_SERVICE = "https://api.mobiorm.com/" + MOBIORM_VERSION + "/generate/objectivec"
12
+ VERTABELO_API_XML = "https://my.vertabelo.com/api/xml/"
13
+
14
+ def self.downloadModel(token, id, tag)
15
+ RestClient::Resource.new(VERTABELO_API_XML + id + (tag ? "/" + tag : ""), token, "") {
16
+ |response, request, result, &block|
17
+ case response.code
18
+ when 200
19
+ response
20
+ else
21
+ abort "Could not download you model, HTTP request ended with code: " + response.code.to_s + "."
22
+ end
23
+ }
24
+ end
25
+
26
+ def self.generateOrm(file, classPrefix, propertyPrefix)
27
+ parameters = Hash.new
28
+ parameters['vertabeloXML'] = file
29
+
30
+ if propertyPrefix
31
+ parameters['propertyPrefix'] = propertyPrefix
32
+ end
33
+
34
+ if classPrefix
35
+ parameters['classPrefix'] = classPrefix
36
+ end
37
+
38
+ RestClient.post(HTTP_SERVICE, parameters) {
39
+ |response, request, result, &block|
40
+ case response.code
41
+ when 200
42
+ response
43
+ else
44
+ abort "Generation unsuccessful, HTTP request ended with code: " + response.code.to_s + "."
45
+ end
46
+ }
47
+ end
48
+
49
+ def self.saveOrm(response, path)
50
+ Zip::Archive.open_buffer(Base64.decode64(response)) do |archive|
51
+ archive.each do |entry|
52
+ if not entry.directory?
53
+ open(path + File.basename(entry.name), "wb") do |f|
54
+ f << entry.read
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def self.getOrm(file, path, classPrefix, propertyPrefix)
62
+ response = JSON.parse(generateOrm(file, classPrefix, propertyPrefix))
63
+ if response["status"] == "OK"
64
+ response["result"]["errors"].each do |error|
65
+ puts "ERROR: " + error
66
+ end
67
+ response["result"]["warnings"].each do |warning|
68
+ puts "WARNING: " + warning
69
+ end
70
+ self.saveOrm(response["result"]["content"], path)
71
+ puts
72
+ puts "Generation successful, files saved in: " + path
73
+ puts
74
+ elsif response["status"] == "ERROR"
75
+ abort "Error during ORM generation: " + response["result"]["error"]
76
+ else
77
+ abort "Error during ORM generation"
78
+ end
79
+ end
80
+
81
+ def self.getOrmFromVertabelo(dest, token, id, tag, classPrefix, propertyPrefix)
82
+ path = "" + dest + "/"
83
+ resource = self.downloadModel(token, id, tag)
84
+ Tempfile.open("temp.xml") do |f|
85
+ f.write(resource.get)
86
+ f.rewind
87
+ self.getOrm(f, path, classPrefix, propertyPrefix)
88
+ end
89
+ end
90
+
91
+ def self.getOrmFromFile(dest, xml, classPrefix, propertyPrefix)
92
+ path = "" + dest + "/"
93
+ File.open(xml) do |f|
94
+ self.getOrm(f, path, classPrefix, propertyPrefix)
95
+ end
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mobiorm_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Pawel Poskrobko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zipruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Generates ORM for Vertabelo XML
42
+ email: contact@vertabelo.com
43
+ executables:
44
+ - mobiorm
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/mobiorm.rb
49
+ - bin/mobiorm
50
+ homepage: http://www.mobiorm.com
51
+ licenses:
52
+ - Apache License 2.0
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.0.14
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Mobi ORM - OR Mapping for Objective-C
74
+ test_files: []