nylas 3.2.0 → 4.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/restful_model.rb DELETED
@@ -1,82 +0,0 @@
1
- require 'time_attr_accessor'
2
- require 'parameters'
3
-
4
- module Nylas
5
- class RestfulModel
6
- extend Nylas::TimeAttrAccessor
7
- include Nylas::Parameters
8
-
9
- parameter :id
10
- parameter :account_id
11
- parameter :cursor # Only used by the delta sync API
12
- time_attr_accessor :created_at
13
- attr_reader :raw_json
14
-
15
- def self.collection_name
16
- "#{self.to_s.downcase}s".split('::').last
17
- end
18
-
19
- def initialize(api, account_id = nil)
20
- raise StandardError.new unless api.class <= Nylas::API
21
- @account_id = account_id
22
- @_api = api
23
- end
24
-
25
- def ==(comparison_object)
26
- comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && comparison_object.id == id)
27
- end
28
-
29
- def inflate(json)
30
- @raw_json = json
31
- parameters.each do |property_name|
32
- send("#{property_name}=", json[property_name]) if json.has_key?(property_name)
33
- end
34
- end
35
-
36
- def save!(params={})
37
- if id
38
- update('PUT', '', as_json(), params)
39
- else
40
- update('POST', '', as_json(), params)
41
- end
42
- end
43
-
44
- def url(action = "")
45
- action = "/#{action}" unless action.empty?
46
- @_api.url_for_path("/#{self.class.collection_name}/#{id}#{action}")
47
- end
48
-
49
- def as_json(options = {})
50
- hash = {}
51
- parameters.each do |getter|
52
- unless options[:except] && options[:except].include?(getter)
53
- value = send(getter)
54
- unless value.is_a?(RestfulModelCollection)
55
- value = value.as_json(options) if value.respond_to?(:as_json)
56
- hash[getter] = value
57
- end
58
- end
59
- end
60
- hash
61
- end
62
-
63
- def update(http_method, action, data = {}, params = {})
64
- http_method = http_method.downcase
65
-
66
- ::RestClient.send(http_method, self.url(action), data.to_json, :content_type => :json, :params => params) do |response, request, result|
67
- unless http_method == 'delete'
68
- json = Nylas.interpret_response(result, response, :expected_class => Object)
69
- inflate(json)
70
- end
71
- end
72
- self
73
- end
74
-
75
- def destroy(params = {})
76
- ::RestClient.send('delete', self.url, :params => params) do |response, request, result|
77
- Nylas.interpret_response(result, response, {:raw_response => true})
78
- end
79
- end
80
-
81
- end
82
- end
@@ -1,160 +0,0 @@
1
- require 'restful_model'
2
-
3
- module Nylas
4
- class RestfulModelCollection
5
-
6
- attr_accessor :filters
7
-
8
- def initialize(model_class, api, filters = {})
9
- raise StandardError.new unless api.class <= Nylas::API
10
- @model_class = model_class
11
- @filters = filters
12
- @_api = api
13
- end
14
-
15
- def each
16
- return enum_for(:each) unless block_given?
17
-
18
- get_model_collection do |items|
19
- items.each do |item|
20
- yield item
21
- end
22
- end
23
- end
24
-
25
- def count
26
- RestClient.get(url, params: @filters.merge(view: 'count')) { |response,request,result|
27
- json = Nylas.interpret_response(result, response)
28
- return json['count']
29
- }
30
- end
31
-
32
- def first
33
- get_model_collection.first
34
- end
35
-
36
- def all
37
- range(0, Float::INFINITY)
38
- end
39
-
40
- def where(filters)
41
- collection = self.clone
42
-
43
- # deep copy the object, otherwise filter is shared among all
44
- # the instances of the collection, which leads to confusing behaviour.
45
- # - karim
46
- if collection.filters == nil
47
- collection.filters = {}
48
- else
49
- collection.filters = Marshal.load(Marshal.dump(collection.filters))
50
- end
51
-
52
- collection.filters.merge!(filters)
53
- collection
54
- end
55
-
56
- def range(offset = 0, limit = 100)
57
-
58
- accumulated = get_model_collection(offset, limit)
59
-
60
- accumulated = accumulated[0..limit] if limit < Float::INFINITY
61
- accumulated
62
- end
63
-
64
- def delete(item_or_id)
65
- item_or_id = item_or_id.id if item_or_id.is_a?(RestfulModel)
66
- RestClient.delete("#{url}/#{item_or_id}")
67
- end
68
-
69
- def find(id)
70
- return nil unless id
71
- get_model(id)
72
- end
73
-
74
- def build(args)
75
- for key in args.keys
76
- args[key.to_s] = args[key]
77
- end
78
- model = @model_class.new(@_api)
79
- model.inflate(args)
80
- model
81
- end
82
-
83
- def inflate_collection(items = [])
84
- models = []
85
-
86
- return unless items.is_a?(Array)
87
- items.each do |json|
88
- if @model_class < RestfulModel
89
- model = @model_class.new(@_api)
90
- model.inflate(json)
91
- else
92
- model = @model_class.new(json)
93
- end
94
- models.push(model)
95
- end
96
- models
97
- end
98
-
99
- def url
100
- @_api.url_for_path("/#{@model_class.collection_name}")
101
- end
102
-
103
- private
104
-
105
- def get_model(id)
106
- model = nil
107
-
108
- RestClient.get("#{url}/#{id}"){ |response,request,result|
109
- json = Nylas.interpret_response(result, response, {:expected_class => Object})
110
- if @model_class < RestfulModel
111
- model = @model_class.new(@_api)
112
- model.inflate(json)
113
- else
114
- model = @model_class.new(json)
115
- end
116
- }
117
- model
118
- end
119
-
120
- def get_model_collection(offset = nil, limit = nil, pagination_options = { per_page: 100 })
121
- filters = @filters.clone
122
- filters[:offset] = offset || filters[:offset] || 0
123
- filters[:limit] = limit || filters[:limit] || 100
124
-
125
- accumulated = []
126
-
127
- finished = false
128
-
129
- current_calls_filters = filters.clone
130
- while (!finished) do
131
- current_calls_filters[:limit] = pagination_options[:per_page] > filters[:limit] ? filters[:limit] : pagination_options[:per_page]
132
- RestClient.get(url, :params => current_calls_filters) do |response, request, result|
133
- items = Nylas.interpret_response(result, response, { :expected_class => Array })
134
- new_items = inflate_collection(items)
135
- yield new_items if block_given?
136
- accumulated = accumulated.concat(new_items)
137
- finished = no_more_pages?(accumulated, items, filters, pagination_options)
138
- end
139
-
140
- current_calls_filters[:offset] += pagination_options[:per_page]
141
- end
142
-
143
- accumulated
144
- end
145
-
146
- def no_more_pages?(accumulated, items, filters, pagination_options)
147
- accumulated.length >= filters[:limit] || items.length < pagination_options[:per_page]
148
- end
149
- end
150
-
151
-
152
- # a ManagementModelCollection is similar to a RestfulModelCollection except
153
- # it's used by models under the /a/<app_id> namespace (mostly account status
154
- # and billing methods).
155
- class ManagementModelCollection < RestfulModelCollection
156
- def url
157
- @_api.url_for_management
158
- end
159
- end
160
- end
@@ -1,12 +0,0 @@
1
- module Nylas
2
- module TimeAttrAccessor
3
- def time_attr_accessor(attr)
4
- parameter attr
5
- define_method "#{attr}=" do |value|
6
- if value
7
- instance_variable_set "@#{attr}", Time.at(value).utc
8
- end
9
- end
10
- end
11
- end
12
- end
data/lib/version.rb DELETED
@@ -1,3 +0,0 @@
1
- module Nylas
2
- VERSION = "3.2.0"
3
- end