glossyapp 0.1.0 → 0.1.1

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: 9385eb3444315ca2e4796807fd9f0501786dfbca
4
- data.tar.gz: 6ee6a956cccd03f7c369908ecce76fa0bff3cc9c
3
+ metadata.gz: 9024b71eaebac9a9af5bc96065ff4aa095889a9c
4
+ data.tar.gz: 12a8e2b28966f02a4e46c82d1a706f1c8633ea32
5
5
  SHA512:
6
- metadata.gz: 043f28fd2776a9263a2b5d2dfb22f7af4ff44954b4c4e2f2c42ff9b20d42c9deafccafad04faed36b998d9a23654df02a845ba4aa996175335e2f86f283f6a72
7
- data.tar.gz: 9dacca0bf513576b9c12bcfa0b9c440b324e5c7dda397541c6879748e5455f1c88c0ae2ab9131ea1c643afbfe46b20da11619f13b1a79d667ea6d5972c6308bb
6
+ metadata.gz: 3fa59df686edd02a1be223d2bf1503ea7e59303e37e42302505bbd965f5ed21bf41140301b6bd4c48f214e74cce2030a292c47f5512adaad5823a931c08bfa43
7
+ data.tar.gz: a4052f063e3e82da5a2a424bda075b14c9f81c03a34b5e8d15c85db97d3a40470c90d44984a891a0f60f2564bf4fe0161316b0fa9a70737d456fe71e9b874a29
data/glossyapp.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'glossyapp/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "glossyapp"
8
- spec.version = Glossyapp::VERSION
8
+ spec.version = GlossyApp::VERSION
9
9
  spec.authors = ["Philipp Schmid"]
10
10
  spec.email = ["schmidp@schmidp.com"]
11
11
 
@@ -21,4 +21,8 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.8"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+ spec.add_dependency "json", "~> 1.8"
26
+ spec.add_dependency "patron", "~> 0.4"
27
+ spec.add_dependency "nokogiri", "~> 1.6"
24
28
  end
@@ -0,0 +1,71 @@
1
+ module GlossyApp
2
+
3
+ class Session
4
+
5
+ def create_file_asset(file_path, asset_folder_id)
6
+ # won't work otherwise...
7
+ setup_session
8
+
9
+ response = @glossy_api.post_multipart("/asset_folders/#{asset_folder_id}/asset_files", {}, { 'asset_file[asset_file]' => file_path })
10
+
11
+ if [200, 201].include?(response.status)
12
+ asset_file = JSON.parse(response.body)
13
+ return asset_file['id']
14
+ else
15
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
16
+ end
17
+
18
+ return nil
19
+ end
20
+
21
+ def delete_file_asset(asset_folder_id, asset_file_id)
22
+ response = @glossy_api.delete("/asset_folders/#{asset_folder_id}/asset_files/#{asset_file_id}")
23
+ if [200, 201].include?(response.status)
24
+ return true
25
+ else
26
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
27
+ end
28
+ end
29
+
30
+ def create_assets_folder(name, parent_id = 'root')
31
+ data = {
32
+ 'asset_folder' => {
33
+ 'parent_id' => parent_id,
34
+ 'name' => name
35
+ }
36
+ }
37
+
38
+ response = @glossy_api.post('/asset_folders', data.to_json, {"Content-Type" => "application/json"})
39
+ if [200, 201].include?(response.status)
40
+ asset_folder = JSON.parse(response.body)
41
+ return asset_folder['id']
42
+ else
43
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
44
+ end
45
+ end
46
+
47
+ def delete_assets_folder(assets_folder_id)
48
+ response = @glossy_api.delete("/asset_folders/#{assets_folder_id}")
49
+ if [200, 201].include?(response.status)
50
+ return true
51
+ else
52
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
53
+ end
54
+ end
55
+
56
+ def asset_folder_with_name_exists?(asset_folder_name, parent_folder_id='root')
57
+ response = @glossy_api.get "/asset_folders/#{parent_folder_id}"
58
+ if [200].include?(response.status)
59
+ root = JSON.parse(response.body)
60
+ root['children'].each do |folder|
61
+ return folder['id'] if folder['name'] == asset_folder_name.to_s
62
+ end
63
+ return false
64
+ else
65
+ return false
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+ end
@@ -0,0 +1,108 @@
1
+ module GlossyApp
2
+
3
+ class Session
4
+
5
+ def get_document_with_external_id(external_id)
6
+ response = @glossy_api.get "/documents"
7
+ if [200].include?(response.status)
8
+ json = JSON.parse(response.body)
9
+
10
+ document_id = nil
11
+ json['documents'].each do |document|
12
+ if document['external_id'].to_s == external_id.to_s
13
+ document_id = document['id']
14
+ break
15
+ end
16
+ end
17
+
18
+ if document_id
19
+ return get_document(document_id)
20
+ else
21
+ return nil
22
+ end
23
+ else
24
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
25
+ end
26
+
27
+ end
28
+
29
+ def get_document(document_id)
30
+ response = @glossy_api.get "/documents/#{document_id}"
31
+ if [200].include?(response.status)
32
+ return JSON.parse(response.body)
33
+ else
34
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
35
+ end
36
+ end
37
+
38
+ def create_document(publication_id, title, language = "en", external_id = nil)
39
+ post_data = {
40
+ 'document' => {
41
+ 'publication_id' => publication_id,
42
+ 'title' => title,
43
+ 'external_id' => external_id,
44
+ 'language' => language
45
+ }
46
+ }
47
+
48
+ response = @glossy_api.post("/documents", post_data.to_json, {"Content-Type" => "application/json"})
49
+ if [200, 201].include?(response.status)
50
+ document = JSON.parse(response.body)
51
+
52
+ document_id = document['id']
53
+ return document_id
54
+
55
+ else
56
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
57
+ end
58
+ end
59
+
60
+ def delete_document(document_id)
61
+ response = @glossy_api.delete("/documents/#{document_id}")
62
+ if [200, 201].include?(response.status)
63
+ return true
64
+ else
65
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
66
+ end
67
+ end
68
+
69
+ def document_exists?(document_id)
70
+ response = @glossy_api.get "/documents/#{document_id}"
71
+ if [200].include?(response.status)
72
+ doc = JSON.parse(response.body)
73
+ return document_id.to_s == doc['id'].to_s
74
+ else
75
+ return false
76
+ end
77
+ end
78
+
79
+ def publish_document(document_id)
80
+ response = @glossy_api.post("/documents/#{document_id}/publish", {})
81
+ if [200, 201].include?(response.status)
82
+ return true
83
+ else
84
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
85
+ end
86
+ end
87
+
88
+ def unpublish_document(document_id)
89
+ response = @glossy_api.post("/documents/#{document_id}/unpublish", {})
90
+ if [200, 201].include?(response.status)
91
+ return true
92
+ else
93
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
94
+ end
95
+ end
96
+
97
+ def import_asset_file_into_document(document_id, asset_file_id)
98
+ response = @glossy_api.post("/documents/#{document_id}/import", {:asset_file_id => asset_file_id}.to_json, {"Content-Type" => "application/json"})
99
+ if [200, 201].include?(response.status)
100
+ return true
101
+ else
102
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
103
+ end
104
+ end
105
+
106
+ end
107
+
108
+ end
@@ -0,0 +1,11 @@
1
+ module GlossyApp
2
+
3
+ class Session
4
+
5
+ def bla
6
+ puts "bla"
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,16 @@
1
+ module GlossyApp
2
+
3
+ class Session
4
+
5
+ def get_pages(document_id)
6
+ response = @glossy_api.get "/documents/#{document_id}/pages"
7
+ if [200].include?(response.status)
8
+ return JSON.parse(response.body)
9
+ else
10
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,11 @@
1
+ module GlossyApp
2
+
3
+ class Session
4
+
5
+ def bla
6
+ puts "bla"
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,61 @@
1
+ require 'patron'
2
+ require 'JSON'
3
+
4
+ require "glossyapp/session/documents"
5
+ require "glossyapp/session/publications"
6
+ require "glossyapp/session/pages"
7
+ require "glossyapp/session/page_elements"
8
+ require "glossyapp/session/assets"
9
+
10
+
11
+ module GlossyApp
12
+
13
+ # You find the rest of the methods in glossyapp/session/*
14
+ class Session
15
+
16
+ def initialize(api_key, base_url = 'http://api.glossyapp.com/v1', proxy = nil)
17
+
18
+ @api_key = api_key
19
+ @base_url = base_url
20
+ @proxy = proxy
21
+
22
+ setup_session
23
+ end
24
+
25
+ def ping
26
+ response = @glossy_api.get "/ping"
27
+ if [200].include?(response.status)
28
+ return true
29
+ else
30
+ return false
31
+ end
32
+ end
33
+
34
+ # FIXME: remove before publishing
35
+ def self.local_dev
36
+ GlossyApp::Session.new("1rbsyZyuqyYeUxzsAkpJ", "http://localhost:3000/api/v1")#, 'http://localhost:8888')
37
+ end
38
+
39
+ protected
40
+
41
+ def setup_session
42
+ @glossy_api = Patron::Session.new
43
+ @glossy_api.timeout = 30
44
+
45
+ @glossy_api.base_url = @base_url
46
+
47
+ @glossy_api.auth_type = :basic
48
+ @glossy_api.username = @api_key
49
+ @glossy_api.password = ''
50
+
51
+ @glossy_api.proxy = @proxy
52
+
53
+ @glossy_api.headers['User-Agent'] = "glossyapp-api-ruby/#{GlossyApp::VERSION}"
54
+ @glossy_api.headers['Accept'] = 'application/json'
55
+
56
+ #@glossy_api.enable_debug STDOUT
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,24 @@
1
+ require 'patron'
2
+ require 'JSON'
3
+
4
+ require "glossyapp/session/documents"
5
+ require "glossyapp/session/publications"
6
+ require "glossyapp/session/page_elements"
7
+ require "glossyapp/session/assets"
8
+
9
+
10
+ module GlossyApp
11
+
12
+ class Util
13
+
14
+ def self.download_file(url, download_path)
15
+ File.open(download_path, "wb") do |saved_file|
16
+ open(url) do |read_file|
17
+ saved_file.write(read_file.read)
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -1,3 +1,3 @@
1
- module Glossyapp
2
- VERSION = "0.1.0"
1
+ module GlossyApp
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/glossyapp.rb CHANGED
@@ -1,5 +1,11 @@
1
+ require 'pathname'
2
+
3
+ cwd = Pathname(__FILE__).dirname
4
+ $:.unshift(cwd.to_s) unless $:.include?(cwd.to_s) || $:.include?(cwd.expand_path.to_s)
5
+
1
6
  require "glossyapp/version"
7
+ require "glossyapp/session"
8
+ require "glossyapp/util"
2
9
 
3
- module Glossyapp
4
- # Your code goes here...
10
+ module GlossyApp
5
11
  end
data/old_vanga_main.rb ADDED
@@ -0,0 +1,198 @@
1
+
2
+ def create_article(article, gallery = nil, asset_file_id = nil)
3
+ post_data = {
4
+ 'article' => {
5
+ 'title' => article[:title],
6
+ 'subtitle' => article[:subtitle],
7
+ 'abstract' => article[:pretext],
8
+ 'content' => article[:textbody]
9
+ }
10
+ }
11
+
12
+ if gallery
13
+ post_data['article']['image_gallery_id'] = gallery[:glossyapp_id]
14
+ end
15
+
16
+ if asset_file_id
17
+ post_data['article']['header_image_asset_file_id'] = asset_file_id
18
+ end
19
+
20
+ response = @glossy_api.post("/documents/#{@options[:document_id]}/articles", post_data.to_json, {"Content-Type" => "application/json"})
21
+ if [200, 201].include?(response.status)
22
+ article = JSON.parse(response.body)
23
+
24
+ article_id = article['id']
25
+ return article_id
26
+
27
+ else
28
+ raise "Couldn't create article: #{response.inspect}\n#{response.body}"
29
+ end
30
+ end
31
+
32
+ def delete_page_element(page_id, page_element_id)
33
+ response = @glossy_api.delete "/documents/#{@options[:document_id]}/pages/#{page_id}/page_elements/#{page_element_id}"
34
+ if [200].include?(response.status)
35
+ return true
36
+ else
37
+ raise "Couldn't get pages: #{response.inspect}\n#{response.body}"
38
+ end
39
+ end
40
+
41
+ def create_page_element(page_id, x, y, width, height, type, data, title = nil)
42
+ data = {
43
+ 'page_element' => {
44
+ 'page_id' => page_id,
45
+ 'type' => type,
46
+ 'title' => title,
47
+ 'x' => x,
48
+ 'y' => y,
49
+ 'width' => width,
50
+ 'height' => height
51
+ }.merge(data)
52
+ }
53
+
54
+ response = @glossy_api.post("/documents/#{@options[:document_id]}/pages/#{page_id}/page_elements", data.to_json, {"Content-Type" => "application/json"})
55
+ if [200, 201].include?(response.status)
56
+ json = JSON.parse(response.body)
57
+ # TODO return id of page element
58
+ else
59
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
60
+ end
61
+ end
62
+
63
+ def page_id_for_page_position(position)
64
+ response = @glossy_api.get "/documents/#{@options[:document_id]}/pages?positions=#{position}"
65
+ if [200].include?(response.status)
66
+ json = JSON.parse(response.body)
67
+ if json['pages'].is_a?(Array) and json['pages'].size > 0
68
+ return json['pages'].first['id']
69
+ else
70
+ return nil
71
+ end
72
+ else
73
+ return nil
74
+ end
75
+ end
76
+
77
+ def toc_section_id_for_section_with_title(title)
78
+ response = @glossy_api.get "/documents/#{@options[:document_id]}/toc_sections?title=#{CGI.escape(title)}"
79
+ if [200].include?(response.status)
80
+ json = JSON.parse(response.body)
81
+ if json['toc_sections'].is_a?(Array) and json['toc_sections'].size > 0
82
+ return json['toc_sections'].first['id']
83
+ else
84
+ return nil
85
+ end
86
+ else
87
+ return nil
88
+ end
89
+ end
90
+
91
+ def create_toc_section(title, description)
92
+ post_data = {
93
+ 'toc_section' => {
94
+ 'title' => title,
95
+ 'description' => description
96
+ }
97
+ }
98
+
99
+ response = @glossy_api.post("/documents/#{@options[:document_id]}/toc_sections", post_data.to_json, {"Content-Type" => "application/json"})
100
+ if [200, 201].include?(response.status)
101
+ json = JSON.parse(response.body)
102
+ return json['id']
103
+ else
104
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
105
+ end
106
+ end
107
+
108
+ def create_toc_entry(section_id, title, page_index)
109
+ post_data = {
110
+ 'toc_entry' => {
111
+ 'title' => title,
112
+ 'page_index' => page_index
113
+ }
114
+ }
115
+
116
+ response = @glossy_api.post("/documents/#{@options[:document_id]}/toc_sections/#{section_id}/toc_entries", post_data.to_json, {"Content-Type" => "application/json"})
117
+ if [200, 201].include?(response.status)
118
+ json = JSON.parse(response.body)
119
+ return json['id']
120
+ else
121
+ raise "Invalid response: #{response.inspect}\n#{response.body}"
122
+ end
123
+ end
124
+
125
+
126
+
127
+
128
+
129
+ def old_vanga_main
130
+
131
+ # Check if document exists on GlossyApp
132
+ if check_if_document_exists
133
+ @logger.info "Document #{@options[:document_id]} does exist"
134
+ else
135
+ raise "Document #{@options[:document_id]} does NOT exist"
136
+ end
137
+
138
+
139
+ # create assets_folder_for_this_import
140
+ asset_folder_id = create_assets_folder("Import of: #{Time.now}")
141
+
142
+ # create articles and ToC
143
+ articles.each do |article|
144
+ # create header image for article
145
+ asset_file_id = nil
146
+ if article[:url_headerimage_large]
147
+ begin
148
+ Tempfile.open('article_header_image') do |f|
149
+ download_file(article[:url_headerimage_large], f.path)
150
+
151
+ asset_file_id = create_file_asset(f.path, asset_folder_id)
152
+ end
153
+ rescue Exception => e
154
+ asset_file_id = nil
155
+ end
156
+ end
157
+
158
+ # create gallery for article
159
+ gallery = gallery_from_article(article)
160
+ gallery[:glossyapp_id] = create_gallery(gallery) if gallery
161
+
162
+ # create article
163
+ article_id = create_article(article, gallery, asset_file_id)
164
+
165
+ # create ToC entry
166
+ unless article[:title].nil? or article[:title].size <= 0
167
+ section_id = toc_section_id_for_section_with_title(article[:title])
168
+ unless section_id
169
+ if article[:title] && article[:subtitle]
170
+ section_id = create_toc_section(article[:title], article[:subtitle])
171
+ create_toc_entry(section_id, article[:title], article[:pageStart].to_i - 1)
172
+ end
173
+ end
174
+ end
175
+
176
+ # create ToC section entries & article_links
177
+ article[:pageStart].to_i.upto(article[:pageEnd].to_i) do |page_position|
178
+
179
+ # create article_link
180
+ page_id = page_id_for_page_position(page_position)
181
+ if page_id
182
+ create_page_element(page_id,
183
+ 0,
184
+ 0,
185
+ 0,
186
+ 0,
187
+ 'article_link',
188
+ {'article_id' => article_id },
189
+ article[:title])
190
+ else
191
+ @logger.error "Did not find page for article..."
192
+ end
193
+
194
+ end
195
+ end
196
+ end
197
+
198
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glossyapp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philipp Schmid
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-17 00:00:00.000000000 Z
11
+ date: 2015-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,48 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: patron
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.4'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
41
83
  description: Helps you to write a glossyapp importer
42
84
  email:
43
85
  - schmidp@schmidp.com
@@ -54,7 +96,15 @@ files:
54
96
  - bin/setup
55
97
  - glossyapp.gemspec
56
98
  - lib/glossyapp.rb
99
+ - lib/glossyapp/session.rb
100
+ - lib/glossyapp/session/assets.rb
101
+ - lib/glossyapp/session/documents.rb
102
+ - lib/glossyapp/session/page_elements.rb
103
+ - lib/glossyapp/session/pages.rb
104
+ - lib/glossyapp/session/publications.rb
105
+ - lib/glossyapp/util.rb
57
106
  - lib/glossyapp/version.rb
107
+ - old_vanga_main.rb
58
108
  homepage: http://www.glossyapp.com
59
109
  licenses:
60
110
  - MIT