printreleaf 1.0.1

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,27 @@
1
+ module PrintReleaf
2
+ class Certificate < Resource
3
+ path "/certificates"
4
+
5
+ action :find
6
+ action :list
7
+
8
+ property :id
9
+ property :account_id
10
+ property :period_start, transform_with: Transforms::Date
11
+ property :period_end, transform_with: Transforms::Date
12
+ property :pages, transform_with: Transforms::Integer
13
+ property :trees, transform_with: Transforms::Float
14
+ property :project_id
15
+ property :project
16
+ coerce_key :project, Forestry::Project
17
+
18
+ def account
19
+ @account ||= Account.find(account_id)
20
+ end
21
+
22
+ def project
23
+ @project ||= self[:project] || Forestry::Project.find(project_id)
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,35 @@
1
+ module PrintReleaf
2
+ class Deposit < Resource
3
+ path "/deposits"
4
+
5
+ action :find
6
+ action :list
7
+ action :create
8
+ action :delete
9
+
10
+ property :id
11
+ property :account_id
12
+ property :source_id
13
+ property :date, transform_with: Transforms::Date
14
+ property :pages, transform_with: Transforms::Integer
15
+ property :width, transform_with: Transforms::Float
16
+ property :height, transform_with: Transforms::Float
17
+ property :density, transform_with: Transforms::Float
18
+ property :paper_type_id
19
+
20
+ def account
21
+ @account ||= Account.find(account_id)
22
+ end
23
+
24
+ def source
25
+ return nil if source_id.nil?
26
+ @source ||= Source.find(source_id)
27
+ end
28
+
29
+ def paper_type
30
+ return nil if paper_type_id.nil?
31
+ @paper_type ||= Paper::Type.find(paper_type_id)
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,35 @@
1
+ module PrintReleaf
2
+ class Error < StandardError
3
+ end
4
+
5
+ class RequestError < Error
6
+ end
7
+
8
+ class BadRequest < Error
9
+ end
10
+
11
+ class Unauthorized < Error
12
+ end
13
+
14
+ class Forbidden < Error
15
+ end
16
+
17
+ class NotFound < Error
18
+ end
19
+
20
+ class RateLimitExceeded < Error
21
+ end
22
+
23
+ class ServerError < Error
24
+ end
25
+
26
+ class NetworkError < Error
27
+ end
28
+
29
+ class ResponseError < Error
30
+ end
31
+
32
+ class DoesNotImplement < Error
33
+ end
34
+ end
35
+
@@ -0,0 +1,30 @@
1
+ module PrintReleaf
2
+ module Forestry
3
+ class Project < Resource
4
+ path "/forestry/projects"
5
+
6
+ action :find
7
+ action :list
8
+
9
+ property :id
10
+ property :name
11
+ property :status
12
+ property :forest_latitude
13
+ property :forest_longitude
14
+ property :content_logo
15
+ property :content_masthead
16
+ property :content_introduction
17
+ property :content_body_html
18
+ property :content_images
19
+
20
+ def active?
21
+ status == "active"
22
+ end
23
+
24
+ def inactive?
25
+ status == "inactive"
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,20 @@
1
+ module PrintReleaf
2
+ class Invitation < Resource
3
+ path "/invitations"
4
+
5
+ action :find
6
+ action :list
7
+ action :create
8
+ action :delete
9
+
10
+ property :id
11
+ property :account_id
12
+ property :email
13
+ property :created_at, transform_with: Transforms::Date
14
+
15
+ def account
16
+ @account ||= Account.find(account_id)
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,23 @@
1
+ module PrintReleaf
2
+ module Paper
3
+ class Type < Resource
4
+ path "/paper/types"
5
+
6
+ action :find
7
+ action :list
8
+ action :create
9
+ action :delete
10
+
11
+ property :id
12
+ property :account_id
13
+ property :name
14
+ property :density, transform_with: Transforms::Float
15
+
16
+ def account
17
+ return nil if account_id.nil?
18
+ @account ||= Account.find(account_id)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,70 @@
1
+ module PrintReleaf
2
+ class Relation
3
+ include Enumerable
4
+
5
+ attr_reader :owner
6
+ attr_reader :resource_class
7
+ attr_reader :path
8
+ attr_reader :actions
9
+
10
+ def initialize(owner, resource_class, options={})
11
+ @owner = owner
12
+ @resource_class = resource_class
13
+ @path = options[:path] || resource_class.uri
14
+ @actions = Set.new(options[:actions] || resource_class.actions)
15
+ @actions.each do |action|
16
+ extend Actions.const_get(action.to_s.capitalize)
17
+ end
18
+ end
19
+
20
+ def uri
21
+ Util.join_uri(owner.uri, path)
22
+ end
23
+
24
+ def new(*args)
25
+ @resource_class.new(*args).tap do |resource|
26
+ resource.owner = owner
27
+ end
28
+ end
29
+
30
+ def related
31
+ if respond_to?(:list)
32
+ return list
33
+ else
34
+ raise "Relation not defined."
35
+ end
36
+ end
37
+
38
+ def each
39
+ return enum_for(:each) unless block_given?
40
+ related.each do |resource|
41
+ yield resource
42
+ end
43
+ end
44
+
45
+ def first
46
+ related.first
47
+ end
48
+
49
+ def last
50
+ related.last
51
+ end
52
+
53
+ def count
54
+ related.count
55
+ end
56
+
57
+ def length
58
+ related.length
59
+ end
60
+
61
+ def to_s
62
+ "#<#{self.class.name}(#{resource_class}>"
63
+ end
64
+
65
+ def inspect
66
+ "#<#{self.class}(#{resource_class}) owner=#{owner.compact_inspect} path=#{path} actions=#{actions}>"
67
+ end
68
+ end
69
+ end
70
+
@@ -0,0 +1,93 @@
1
+ module PrintReleaf
2
+ class Resource < Hashie::Trash
3
+ include Hashie::Extensions::IndifferentAccess
4
+ include Hashie::Extensions::IgnoreUndeclared
5
+ include Hashie::Extensions::Coercion
6
+
7
+ class << self
8
+ def path(value=nil)
9
+ @path = value if value
10
+ @path or raise "Path not declared."
11
+ end
12
+
13
+ def uri
14
+ path
15
+ end
16
+
17
+ def actions
18
+ @actions ||= Set.new
19
+ end
20
+
21
+ def action(sym)
22
+ actions.tap { |list|
23
+ list.add(sym)
24
+ }.each { |action|
25
+ include Actions.const_get(action.to_s.capitalize)
26
+ }
27
+ end
28
+ end
29
+
30
+ # Default properties
31
+ property :deleted
32
+
33
+ attr_reader :copy
34
+ attr_accessor :owner
35
+
36
+ def initialize(*args)
37
+ super
38
+ @copy = self.dup.freeze
39
+ end
40
+
41
+ def uri
42
+ scope = owner ? owner.uri : nil
43
+ Util.join_uri(scope, self.class.uri, self.id)
44
+ end
45
+
46
+ def find(*args)
47
+ raise DoesNotImplement, "Resource does not implement `find`"
48
+ end
49
+
50
+ def delete
51
+ raise DoesNotImplement, "Resource does not implement `delete`"
52
+ end
53
+
54
+ def reset(hash)
55
+ delete_if { true }
56
+ update(hash)
57
+ end
58
+
59
+ def changes
60
+ keys.map(&:to_sym).inject({}) do |diff, key|
61
+ unless self[key] == copy[key]
62
+ diff[key] = self[key]
63
+ end
64
+ diff
65
+ end
66
+ end
67
+
68
+ def deleted?
69
+ !!deleted
70
+ end
71
+
72
+ def to_s
73
+ "#<#{self.class.name}>"
74
+ end
75
+
76
+ def inspect
77
+ "".tap do |str|
78
+ str << "#<#{self.class} "
79
+ str << JSON.pretty_generate(self)
80
+ str << ">"
81
+ end
82
+ end
83
+
84
+ def compact_inspect
85
+ "".tap do |str|
86
+ str << "#<#{self.class} "
87
+ str << "id='#{self.id}'" if respond_to?(:id)
88
+ str << ">"
89
+ end
90
+ end
91
+ end
92
+ end
93
+
@@ -0,0 +1,24 @@
1
+ module PrintReleaf
2
+ class Server < Resource
3
+ path "/servers"
4
+
5
+ action :find
6
+ action :list
7
+ action :create
8
+ action :update
9
+ action :delete
10
+
11
+ property :id
12
+ property :account_id
13
+ property :type
14
+ property :created_at, transform_with: Transforms::Date
15
+ property :url
16
+ property :username
17
+ property :password
18
+
19
+ def account
20
+ @account ||= Account.find(account_id)
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,37 @@
1
+ module PrintReleaf
2
+ class Source < Resource
3
+ path "/sources"
4
+
5
+ action :find
6
+ action :list
7
+ action :create
8
+ action :update
9
+ action :delete
10
+ action :activate
11
+ action :deactivate
12
+
13
+ property :id
14
+ property :account_id
15
+ property :type
16
+ property :server_id
17
+ property :external_id
18
+ property :collection_scope
19
+ property :created_at, transform_with: Transforms::Date
20
+ property :status
21
+ property :activated_at, transform_with: Transforms::Date
22
+ property :deactivated_at, transform_with: Transforms::Date
23
+ property :health_check
24
+ property :health_check_checked_at, transform_with: Transforms::Date
25
+ property :health_check_changed_at, transform_with: Transforms::Date
26
+
27
+ def account
28
+ @account ||= Account.find(account_id)
29
+ end
30
+
31
+ def server
32
+ return nil if server_id.nil?
33
+ @server ||= Server.find(server_id)
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,32 @@
1
+ module PrintReleaf
2
+ class Transaction < Resource
3
+ path "/transactions"
4
+
5
+ action :find
6
+ action :list
7
+ action :create
8
+ action :delete
9
+
10
+ property :id
11
+ property :account_id
12
+ property :project_id
13
+ property :certificate_id
14
+ property :date, transform_with: Transforms::Date
15
+ property :trees, transform_with: Transforms::Float
16
+ property :items
17
+ coerce_key :items, Array[TransactionItem]
18
+
19
+ def account
20
+ @account ||= Account.find(account_id)
21
+ end
22
+
23
+ def project
24
+ @project ||= Forestry::Project.find(project_id)
25
+ end
26
+
27
+ def certificate
28
+ @certificate ||= Certificate.find(certificate_id)
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,15 @@
1
+ module PrintReleaf
2
+ class TransactionItem < Resource
3
+ property :pages, transform_with: Transforms::Integer
4
+ property :width, transform_with: Transforms::Float
5
+ property :height, transform_with: Transforms::Float
6
+ property :density, transform_with: Transforms::Float
7
+ property :paper_type_id
8
+
9
+ def paper_type
10
+ return nil if paper_type_id.nil?
11
+ @paper_type ||= Paper::Type.find(paper_type_id)
12
+ end
13
+ end
14
+ end
15
+