gliffy 0.0.6 → 0.0.7

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.
@@ -0,0 +1,14 @@
1
+ module Gliffy
2
+ class API::Error < Exception
3
+ attr_reader :code, :text
4
+
5
+ def initialize(code, text)
6
+ @code = code
7
+ @text = text
8
+ end
9
+
10
+ def to_s
11
+ "#{code.to_i}: #{text}"
12
+ end
13
+ end
14
+ end
@@ -10,19 +10,31 @@ module Gliffy
10
10
  end
11
11
 
12
12
  def raw(partial_url, params = {})
13
- @api.raw(api_root + partial_url, params)
13
+ api.raw(api_root + partial_url, params)
14
14
  end
15
15
 
16
16
  def get(partial_url, params)
17
- @api.get(api_root + partial_url, params)
17
+ response = api.get(api_root + partial_url, params)
18
+
19
+ if response.error?
20
+ handle_error response
21
+ end
22
+
23
+ response
18
24
  end
19
25
 
20
26
  def post(partial_url, params)
21
- @api.post(api_root + partial_url, params)
27
+ response = api.post(api_root + partial_url, params)
28
+
29
+ if response.error?
30
+ handle_error response
31
+ end
32
+
33
+ response
22
34
  end
23
35
 
24
36
  def web(partial_url, params)
25
- @api.web(web_root + partial_url, params)
37
+ api.web(web_root + partial_url, params)
26
38
  end
27
39
 
28
40
  def get_folders(account_id)
@@ -30,8 +42,64 @@ module Gliffy
30
42
  :action => 'get')
31
43
  end
32
44
 
45
+ def update_document_metadata(document_id, name, shared)
46
+ params = {
47
+ :action => "update",
48
+ }
49
+
50
+ if not name.nil?
51
+ params[:documentName] = name
52
+ end
53
+
54
+ if not shared.nil?
55
+ params[:public] = shared ? "true" : "false"
56
+ end
57
+
58
+ post("/accounts/#{account_id}/documents/#{document_id}/meta-data.xml",
59
+ params)
60
+ end
61
+
62
+ def delete_document(document_id)
63
+ post("/accounts/#{account_id}/documents/#{document_id}.xml",
64
+ :action => "delete")
65
+ end
66
+
67
+ def create_document(name, type, original_id, path)
68
+ params = {
69
+ :action => "create",
70
+ :documentName => name,
71
+ :documentType => type
72
+ }
73
+
74
+ if not original_id.nil?
75
+ params[:templateDiagramId] = original_id
76
+ end
77
+
78
+ if not path.nil?
79
+ params[:folderPath] = path
80
+ end
81
+
82
+ post("/accounts/#{account_id}/documents.xml",
83
+ params)
84
+ end
85
+
33
86
  private
34
87
 
88
+ def handle_error(response)
89
+ code = response.integer("//g:response/g:error/@http-status")
90
+ text = response.string("//g:response/g:error/text()")
91
+
92
+ raise Gliffy::API::Error.new(code, text)
93
+ end
94
+
95
+ def api
96
+ @api
97
+ end
98
+
99
+ def account_id
100
+ api.account_id
101
+ end
102
+
35
103
  def initialize(protocol, api)
36
104
  @protocol = protocol
37
105
  @api = api
@@ -32,6 +32,10 @@ module Gliffy
32
32
  !@base.at_xpath(path, namespaces).nil?
33
33
  end
34
34
 
35
+ def error?
36
+ exists("//g:response[@success='false']")
37
+ end
38
+
35
39
  private
36
40
 
37
41
  # Gliffy XML namespace to be used in XPath expressions
@@ -1,69 +1,103 @@
1
- module Gliffy
2
- class Document
3
- attr_reader :owner
4
- attr_reader :id
5
- attr_reader :is_public
6
- attr_reader :versions
7
- attr_reader :name
8
- attr_reader :modified, :created, :published
9
-
10
- def self.load(owner, node)
11
- @loaded ||= {}
12
-
13
- id = node.integer('@id')
14
- if not @loaded.has_key? id then
15
- @loaded[id] = Gliffy::Document.new(
16
- owner,
17
- id,
18
- :is_public => node.exists('@is-public'),
19
- :versions => node.integer('@num-versions'),
20
- :name => node.string('g:name'),
21
- :modified => node.timestamp('g:mod-date'),
22
- :created => node.timestamp('g:create-date'),
23
- :published => node.timestamp('g:published-date')
24
- )
25
- end
26
-
27
- @loaded[id]
28
- end
29
-
30
- def self.clear_cache
31
- @loaded = {}
32
- end
33
-
34
- def editor(return_url, return_text)
35
- api.web(
36
- "/gliffy/",
37
- :launchDiagramId => id,
38
- :returnURL => return_url,
39
- :returnButtonText => return_text
40
- )
41
- end
42
-
43
- def png
44
- Document::PNG.new(self)
45
- end
46
-
47
- def public?
48
- is_public
49
- end
50
-
51
- def api
52
- owner.api
53
- end
54
-
55
- private
56
-
57
- def initialize(owner, id, params)
58
- @owner = owner
59
- @id = id
60
-
61
- @is_public = params[:is_public]
62
- @versions = params[:versions]
63
- @name = params[:name]
64
- @modified = params[:modified]
65
- @created = params[:created]
66
- @published = params[:published]
67
- end
68
- end
69
- end
1
+ require "observer"
2
+
3
+ module Gliffy
4
+ class Document
5
+ include Observable
6
+
7
+ TYPE_DIAGRAM = "diagram"
8
+
9
+ attr_reader :owner
10
+ attr_reader :id
11
+ attr_reader :is_public
12
+ attr_reader :versions
13
+ attr_reader :name
14
+ attr_reader :modified, :created, :published
15
+
16
+ def self.load(owner, node)
17
+ @loaded ||= {}
18
+
19
+ id = node.integer('@id')
20
+ if not @loaded.has_key? id then
21
+ @loaded[id] = Gliffy::Document.new(
22
+ owner,
23
+ id,
24
+ :is_public => node.exists('@is-public'),
25
+ :versions => node.integer('@num-versions'),
26
+ :name => node.string('g:name'),
27
+ :modified => node.timestamp('g:mod-date'),
28
+ :created => node.timestamp('g:create-date'),
29
+ :published => node.timestamp('g:published-date')
30
+ )
31
+ end
32
+
33
+ @loaded[id]
34
+ end
35
+
36
+ def self.clear_cache
37
+ @loaded = {}
38
+ end
39
+
40
+ def rename(new_name)
41
+ @name = new_name
42
+
43
+ api.update_document_metadata(id, new_name, nil)
44
+ end
45
+
46
+ def delete
47
+ api.delete_document(id)
48
+ @is_deleted = true
49
+
50
+ changed
51
+ notify_observers :delete, self
52
+ end
53
+
54
+ def deleted?
55
+ @is_deleted
56
+ end
57
+
58
+ def editor(return_url, return_text)
59
+ api.web(
60
+ "/gliffy/",
61
+ :launchDiagramId => id,
62
+ :returnURL => return_url,
63
+ :returnButtonText => return_text
64
+ )
65
+ end
66
+
67
+ def svg
68
+ Document::Presentation::SVG.new(self)
69
+ end
70
+
71
+ def xml
72
+ Document::Presentation::XML.new(self)
73
+ end
74
+
75
+ def png
76
+ Document::Presentation::PNG.new(self)
77
+ end
78
+
79
+ def public?
80
+ is_public
81
+ end
82
+
83
+ def api
84
+ owner.api
85
+ end
86
+
87
+ private
88
+
89
+ def initialize(owner, id, params)
90
+ @owner = owner
91
+ @id = id
92
+
93
+ @is_public = params[:is_public]
94
+ @versions = params[:versions]
95
+ @name = params[:name]
96
+ @modified = params[:modified]
97
+ @created = params[:created]
98
+ @published = params[:published]
99
+
100
+ @is_deleted = false
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,27 @@
1
+ module Gliffy
2
+ class Document::Presentation
3
+ attr_reader :document
4
+
5
+ def initialize(document)
6
+ @document = document
7
+ end
8
+
9
+ private
10
+
11
+ def api
12
+ document.api
13
+ end
14
+
15
+ def account
16
+ document.owner
17
+ end
18
+
19
+ def document_id
20
+ document.id
21
+ end
22
+
23
+ def account_id
24
+ account.id
25
+ end
26
+ end
27
+ end
@@ -1,16 +1,10 @@
1
1
  module Gliffy
2
- class Document::PNG
3
- attr_reader :document
4
-
2
+ class Document::Presentation::PNG < Document::Presentation
5
3
  SIZE_THUMBNAIL = "T"
6
4
  SIZE_SMALL = "S"
7
5
  SIZE_MEDIUM = "M"
8
6
  SIZE_FULL = "L"
9
7
 
10
- def initialize(document)
11
- @document = document
12
- end
13
-
14
8
  def content(size)
15
9
  api.raw(
16
10
  "/accounts/#{account_id}/documents/#{document_id}.png",
@@ -34,23 +28,5 @@ module Gliffy
34
28
  def full
35
29
  content(SIZE_FULL)
36
30
  end
37
-
38
- private
39
-
40
- def api
41
- document.api
42
- end
43
-
44
- def account
45
- document.owner
46
- end
47
-
48
- def document_id
49
- document.id
50
- end
51
-
52
- def account_id
53
- account.id
54
- end
55
31
  end
56
- end
32
+ end
@@ -0,0 +1,10 @@
1
+ module Gliffy
2
+ class Document::Presentation::SVG < Document::Presentation
3
+ def content
4
+ api.raw(
5
+ "/accounts/#{account_id}/documents/#{document_id}.svg",
6
+ :action => 'get'
7
+ )
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Gliffy
2
+ class Document::Presentation::XML < Document::Presentation
3
+ def content
4
+ api.raw(
5
+ "/accounts/#{account_id}/documents/#{document_id}.xml",
6
+ :action => 'get'
7
+ )
8
+ end
9
+ end
10
+ end
data/lib/gliffy/folder.rb CHANGED
@@ -1,71 +1,91 @@
1
- require 'uri'
2
-
3
- module Gliffy
4
- class Folder
5
- attr_reader :name, :path
6
- attr_reader :owner, :parent
7
- attr_reader :folders
8
-
9
- def self.load(owner, node)
10
- Gliffy::Folder.new(
11
- owner,
12
- node.string('g:name'),
13
- node.string('g:path'),
14
- node.nodes('g:folder').map {|n| Gliffy::Folder.load(owner, n)}
15
- )
16
- end
17
-
18
- def initialize(owner, name, path, folders)
19
- @owner = owner
20
- @name = name
21
- @path = path
22
-
23
- @folders = folders
24
- @folders.each do |f|
25
- f.parent = self
26
- end
27
- end
28
-
29
- def parent=(parent)
30
- if path != parent.path + "/" + name then
31
- raise "Invalid parent"
32
- end
33
-
34
- @parent = parent
35
- end
36
-
37
- def documents
38
- @documents ||= load_documents
39
- end
40
-
41
- def root?
42
- path == "ROOT"
43
- end
44
-
45
- private
46
-
47
- def api
48
- owner.api
49
- end
50
-
51
- def account_id
52
- owner.id
53
- end
54
-
55
- def escaped_path
56
- path.gsub(' ', '+')
57
- end
58
-
59
- def load_documents
60
- # Path is alphanumeric + spaces and '/'. Spaces should be
61
- # escaped; slashes should NOT be escaped.
62
- url = "/accounts/#{account_id}/folders/#{escaped_path}/documents.xml"
63
- response = api.get(url,
64
- :action => "get")
65
-
66
- response
67
- .nodes('//g:document')
68
- .map { |n| Gliffy::Document.load(owner, n) }
69
- end
70
- end
71
- end
1
+ require 'uri'
2
+
3
+ module Gliffy
4
+ class Folder
5
+ attr_reader :name, :path
6
+ attr_reader :owner, :parent
7
+ attr_reader :folders
8
+
9
+ def self.load(owner, node)
10
+ Gliffy::Folder.new(
11
+ owner,
12
+ node.string('g:name'),
13
+ node.string('g:path'),
14
+ node.nodes('g:folder').map {|n| Gliffy::Folder.load(owner, n)}
15
+ )
16
+ end
17
+
18
+ def initialize(owner, name, path, folders)
19
+ @owner = owner
20
+ @name = name
21
+ @path = path
22
+
23
+ @folders = folders
24
+ @folders.each do |f|
25
+ f.parent = self
26
+ end
27
+ end
28
+
29
+ def parent=(parent)
30
+ if path != parent.path + "/" + name then
31
+ raise "Invalid parent"
32
+ end
33
+
34
+ @parent = parent
35
+ end
36
+
37
+ def create_document(name)
38
+ api.create_document(name, Gliffy::Document::TYPE_DIAGRAM, nil, path)
39
+ end
40
+
41
+ def documents
42
+ @documents ||= load_documents
43
+ end
44
+
45
+ def root?
46
+ path == "ROOT"
47
+ end
48
+
49
+ # observer callback
50
+ def update(event, target)
51
+ case event
52
+ when :delete
53
+ @documents = @documents.delete_if { |element| element == target }
54
+ else
55
+ raise ArgumentError.new(event)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def api
62
+ owner.api
63
+ end
64
+
65
+ def account_id
66
+ owner.id
67
+ end
68
+
69
+ def escaped_path
70
+ path.gsub(' ', '+')
71
+ end
72
+
73
+ def load_documents
74
+ # Path is alphanumeric + spaces and '/'. Spaces should be
75
+ # escaped; slashes should NOT be escaped.
76
+ url = "/accounts/#{account_id}/folders/#{escaped_path}/documents.xml"
77
+ response = api.get(url,
78
+ :action => "get")
79
+
80
+ response
81
+ .nodes('//g:document')
82
+ .map { |n| load_document n }
83
+ end
84
+
85
+ def load_document(n)
86
+ document = Gliffy::Document.load(owner, n)
87
+ document.add_observer(self)
88
+ document
89
+ end
90
+ end
91
+ end