timetree 0.0.1 → 0.1.4

This diff has not been reviewed by any users.
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ module TimeTree
7
+ # Command for HTTP request.
8
+ class HttpCommand
9
+ def initialize(host, client)
10
+ @host = host
11
+ @client = client
12
+ @logger = TimeTree.configuration.logger
13
+ end
14
+
15
+ # @param path [String] String or URI to access.
16
+ # @param params [Hash] Hash of URI query unencoded key/value pairs.
17
+ def get(path, params = {})
18
+ logger.info "GET #{connection.build_url("#{@host}#{path}", params)}"
19
+ res = connection.get path, params
20
+ @client.update_ratelimit(res)
21
+ logger.debug "Response status:#{res.status}, body:#{res.body}"
22
+ res
23
+ end
24
+
25
+ # @param path [String] String or URI to access.
26
+ # @param body_params [Hash]
27
+ # The request bodythat will eventually be converted to JSON.
28
+ def post(path, body_params = {})
29
+ @logger.debug "POST #{@host}#{path} body:#{body_params}"
30
+ headers = { 'Content-Type' => 'application/json' }
31
+ res = connection.run_request :post, path, body_params.to_json, headers
32
+ @client.update_ratelimit(res)
33
+ logger.debug "Response status:#{res.status}, body:#{res.body}"
34
+ res
35
+ end
36
+
37
+ # @param path [String] String or URI to access.
38
+ # @param body_params [Hash]
39
+ # The request bodythat will eventually be converted to JSON.
40
+ def put(path, body_params = {})
41
+ logger.debug "PUT #{@host}#{path} body:#{body_params}"
42
+ headers = { 'Content-Type' => 'application/json' }
43
+ res = connection.run_request :put, path, body_params.to_json, headers
44
+ @client.update_ratelimit(res)
45
+ logger.debug "Response status:#{res.status}, body:#{res.body}"
46
+ res
47
+ end
48
+
49
+ # @param path [String] String or URI to access.
50
+ # @param params [Hash] Hash of URI query unencoded key/value pairs.
51
+ def delete(path, params = {})
52
+ @logger.debug "DELETE #{@host}#{path} params:#{params}"
53
+ res = connection.delete path, params
54
+ @client.update_ratelimit(res)
55
+ logger.debug "Response status:#{res.status}, body:#{res.body}"
56
+ res
57
+ end
58
+
59
+ private
60
+
61
+ attr_reader :logger
62
+
63
+ def connection
64
+ Faraday.new(
65
+ url: @host,
66
+ headers: base_request_headers
67
+ ) do |builder|
68
+ builder.response :json, parser_options: { symbolize_names: true }, content_type: /\bjson$/
69
+ end
70
+ end
71
+
72
+ def base_request_headers
73
+ {
74
+ 'Accept' => 'application/vnd.timetree.v1+json',
75
+ 'Authorization' => "Bearer #{@client.token}"
76
+ }
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TimeTree
4
+ # Model for TimeTree color theme.
5
+ class Label < BaseModel
6
+ # @return [String]
7
+ attr_accessor :name
8
+ # @return [String]
9
+ attr_accessor :color
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TimeTree
4
+ # Model for TimeTree user.
5
+ class User < BaseModel
6
+ # @return [String]
7
+ attr_accessor :name
8
+ # @return [String]
9
+ attr_accessor :description
10
+ # @return [String]
11
+ attr_accessor :image_url
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TimeTree
4
- VERSION = '0.0.1'
4
+ VERSION = '0.1.4'
5
5
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.metadata['homepage_uri'] = spec.homepage
18
18
  spec.metadata['source_code_uri'] = 'https://github.com/koshilife/timetree-api-ruby-client'
19
19
  spec.metadata['changelog_uri'] = "#{spec.metadata['source_code_uri']}/blob/master/CHANGELOG.md"
20
- spec.metadata['documentation_uri'] = 'https://www.rubydoc.info/gems/timetree/'
20
+ spec.metadata['documentation_uri'] = "https://www.rubydoc.info/gems/timetree/#{spec.version}"
21
21
 
22
22
  # Specify which files should be added to the gem when it is released.
23
23
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -30,10 +30,12 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  spec.add_runtime_dependency 'faraday', '~> 1.0.1'
32
32
  spec.add_runtime_dependency 'faraday_middleware', '~> 1.0.0'
33
+ spec.add_runtime_dependency 'zeitwerk', '~> 2.3.0'
33
34
 
34
35
  spec.add_development_dependency 'bundler', '~> 2.0'
35
36
  spec.add_development_dependency 'minitest', '~> 5.14.0'
36
37
  spec.add_development_dependency 'minitest-reporters', '~> 1.4.2'
37
38
  spec.add_development_dependency 'rake', '~> 12.0'
38
39
  spec.add_development_dependency 'webmock', '~> 3.7.6'
40
+ spec.add_development_dependency 'simplecov', '~> 0.18.5'
39
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timetree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenji Koshikawa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-21 00:00:00.000000000 Z
11
+ date: 2020-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: zeitwerk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.3.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +122,20 @@ dependencies:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
124
  version: 3.7.6
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.18.5
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.18.5
111
139
  description: Client for accessing TimeTree APIs
112
140
  email:
113
141
  - koshikawa2009@gmail.com
@@ -127,9 +155,16 @@ files:
127
155
  - bin/console
128
156
  - bin/setup
129
157
  - lib/timetree.rb
158
+ - lib/timetree/activity.rb
159
+ - lib/timetree/api_error.rb
160
+ - lib/timetree/base_model.rb
161
+ - lib/timetree/calendar.rb
130
162
  - lib/timetree/client.rb
131
163
  - lib/timetree/configuration.rb
132
- - lib/timetree/models.rb
164
+ - lib/timetree/event.rb
165
+ - lib/timetree/http_command.rb
166
+ - lib/timetree/label.rb
167
+ - lib/timetree/user.rb
133
168
  - lib/timetree/version.rb
134
169
  - timetree.gemspec
135
170
  homepage: https://github.com/koshilife/timetree-api-ruby-client
@@ -139,7 +174,7 @@ metadata:
139
174
  homepage_uri: https://github.com/koshilife/timetree-api-ruby-client
140
175
  source_code_uri: https://github.com/koshilife/timetree-api-ruby-client
141
176
  changelog_uri: https://github.com/koshilife/timetree-api-ruby-client/blob/master/CHANGELOG.md
142
- documentation_uri: https://www.rubydoc.info/gems/timetree/
177
+ documentation_uri: https://www.rubydoc.info/gems/timetree/0.1.4
143
178
  post_install_message:
144
179
  rdoc_options: []
145
180
  require_paths:
@@ -1,282 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'time'
4
-
5
- module TimeTree
6
- class BaseModel
7
- attr_reader :id
8
- attr_reader :type
9
- attr_accessor :relationships
10
-
11
- # @param data [Hash]
12
- # @param client [TimeTree::Client]
13
- # @return [TimeTree::BaseModel]
14
- def self.to_model(data, included: nil, client: nil)
15
- id = data[:id]
16
- type = data[:type]
17
- return if type.nil?
18
-
19
- attributes = data[:attributes] || {}
20
- relationships = data[:relationships] || {}
21
- params = {
22
- id: id,
23
- type: type,
24
- client: client,
25
- attributes: attributes,
26
- relationships: relationships,
27
- included: included
28
- }
29
-
30
- case type
31
- when 'user'
32
- User.new(**params)
33
- when 'label'
34
- Label.new(**params)
35
- when 'event'
36
- Event.new(**params)
37
- when 'calendar'
38
- Calendar.new(**params)
39
- when 'activity'
40
- Activity.new(**params)
41
- end
42
- end
43
-
44
- def initialize(id:, type:, client: nil, attributes: nil, relationships: nil, included: nil)
45
- @id = id
46
- @type = type
47
- @client = client
48
- set_attributes attributes
49
- set_relationships relationships, included
50
- end
51
-
52
- def inspect
53
- "\#<#{self.class}:#{object_id} id:#{id}>"
54
- end
55
-
56
- private
57
-
58
- def to_model(data)
59
- self.class.to_model data, client: @client
60
- end
61
-
62
- def set_attributes(attributes)
63
- return unless attributes.is_a? Hash
64
- return if attributes.empty?
65
-
66
- setter_methods = self.class.instance_methods.select { |method| method.to_s.end_with? '=' }
67
- attributes.each do |key, value|
68
- setter = "#{key.to_sym}=".to_sym
69
- next unless setter_methods.include? setter
70
-
71
- if defined?(self.class::TIME_FIELDS) && self.class::TIME_FIELDS.include?(key)
72
- value = Time.parse value
73
- end
74
- instance_variable_set "@#{key}", value
75
- end
76
- end
77
-
78
- def set_relationships(relationships, included)
79
- return unless relationships.is_a? Hash
80
- return if relationships.empty?
81
- return unless defined? self.class::RELATIONSHIPS
82
- return if self.class::RELATIONSHIPS.empty?
83
-
84
- self.class::RELATIONSHIPS.each do |key|
85
- relation = relationships[key]
86
- next unless relation
87
- next unless relation[:data]
88
-
89
- @relationships ||= {}
90
- @relationships[key] = relation[:data]
91
- end
92
-
93
- return if included.nil?
94
- return unless included.is_a? Array
95
- return if included.empty?
96
-
97
- set_relationship_data_if_included(included)
98
- end
99
-
100
- def set_relationship_data_if_included(included)
101
- @_relation_data_dic = {}
102
- included.each do |data|
103
- item = to_model(data)
104
- next unless item
105
-
106
- @_relation_data_dic[item.type] ||= {}
107
- @_relation_data_dic[item.type][item.id] = item
108
- end
109
- detect_relation_data = lambda { |type, id|
110
- return unless @_relation_data_dic[type]
111
-
112
- @_relation_data_dic[type][id]
113
- }
114
- @relationships.each do |key, id_data|
115
- relation_data = nil
116
- if id_data.is_a? Array
117
- relation_data = []
118
- id_data.each do |d|
119
- item = detect_relation_data.call(d[:type], d[:id])
120
- relation_data << item if item
121
- end
122
- elsif id_data.is_a? Hash
123
- relation_data = detect_relation_data.call(id_data[:type], id_data[:id])
124
- end
125
- instance_variable_set "@#{key}", relation_data if relation_data
126
- end
127
- end
128
- end
129
-
130
- class User < BaseModel
131
- attr_accessor :name
132
- attr_accessor :description
133
- attr_accessor :image_url
134
- end
135
-
136
- class Label < BaseModel
137
- attr_accessor :name
138
- attr_accessor :color
139
- end
140
-
141
- class Calendar < BaseModel
142
- attr_accessor :name
143
- attr_accessor :description
144
- attr_accessor :color
145
- attr_accessor :order
146
- attr_accessor :image_url
147
- attr_accessor :created_at
148
-
149
- TIME_FIELDS = %i[created_at].freeze
150
- RELATIONSHIPS = %i[labels members].freeze
151
-
152
- def event(event_id)
153
- return if @client.nil?
154
-
155
- @client.event id, event_id
156
- end
157
-
158
- def upcoming_events(days: 7, timezone: 'UTC')
159
- return if @client.nil?
160
-
161
- @client.upcoming_events id, days: days, timezone: timezone
162
- end
163
-
164
- def labels
165
- return @labels if defined? @labels
166
- return if @client.nil?
167
-
168
- @labels = @client.calendar_labels id
169
- end
170
-
171
- def members
172
- return @members if defined? @members
173
- return if @client.nil?
174
-
175
- @members = @client.calendar_members id
176
- end
177
- end
178
-
179
- class Event < BaseModel
180
- attr_accessor :category
181
- attr_accessor :title
182
- attr_accessor :all_day
183
- attr_accessor :start_at
184
- attr_accessor :start_timezone
185
- attr_accessor :end_at
186
- attr_accessor :end_timezone
187
- attr_accessor :recurrences
188
- attr_accessor :recurring_uuid
189
- attr_accessor :description
190
- attr_accessor :location
191
- attr_accessor :url
192
- attr_accessor :updated_at
193
- attr_accessor :created_at
194
- attr_accessor :calendar_id
195
-
196
- attr_reader :creator
197
- attr_reader :label
198
- attr_reader :attendees
199
-
200
- TIME_FIELDS = %i[start_at end_at updated_at created_at].freeze
201
- RELATIONSHIPS = %i[creator label attendees].freeze
202
-
203
- def create
204
- return if @client.nil?
205
-
206
- @client.create_event calendar_id, data_params
207
- end
208
-
209
- def create_comment(message)
210
- return if @client.nil?
211
-
212
- params = { type: 'activity', attributes: { calendar_id: calendar_id, event_id: id, content: message } }
213
- activity = to_model params
214
- return if activity.nil?
215
-
216
- activity.create
217
- end
218
-
219
- def update
220
- return if @client.nil?
221
- return if id.nil?
222
-
223
- @client.update_event calendar_id, id, data_params
224
- end
225
-
226
- def delete
227
- return if @client.nil?
228
- return if id.nil?
229
-
230
- @client.delete_event calendar_id, id
231
- end
232
-
233
- def data_params
234
- attributes_params = {
235
- category: category,
236
- title: title,
237
- all_day: all_day,
238
- start_at: start_at.iso8601,
239
- start_timezone: start_timezone,
240
- end_at: end_at.iso8601,
241
- end_timezone: end_timezone,
242
- description: description,
243
- location: location,
244
- url: url
245
- }
246
- relationhips_params = {}
247
- if @relationships[:label]
248
- label_data = { id: @relationships[:label], type: 'label' }
249
- relationhips_params[:label] = { data: label_data }
250
- end
251
- if @relationships[:attendees]
252
- attendees_data = @relationships[:attendees].map { |_id| { id: _id, type: 'user' } }
253
- relationhips_params[:attendees] = { data: attendees_data }
254
- end
255
- {
256
- data: { attributes: attributes_params, relationships: relationhips_params }
257
- }
258
- end
259
- end
260
-
261
- class Activity < BaseModel
262
- attr_accessor :content
263
- attr_accessor :updated_at
264
- attr_accessor :created_at
265
- attr_accessor :calendar_id
266
- attr_accessor :event_id
267
-
268
- TIME_FIELDS = %i[updated_at created_at].freeze
269
-
270
- def create
271
- return if @client.nil?
272
-
273
- @client.create_activity calendar_id, event_id, data_params
274
- end
275
-
276
- def data_params
277
- {
278
- data: { attributes: { content: content } }
279
- }
280
- end
281
- end
282
- end