cisco_spark 0.2.1 → 0.2.2

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: 5b49c8a731f17def9f8f8bad7e5a14105fc9e5e4
4
- data.tar.gz: 84999b34944fc0f22f6e07e8148d011208de6011
3
+ metadata.gz: 9e7bab6e1d7eb7f898297f5f1c350a800e9bfcb7
4
+ data.tar.gz: 2fba2068d4a125b24ddca0808a7e49981281083b
5
5
  SHA512:
6
- metadata.gz: 572138fbffa7e999a58f628ded5555cc6b578a841e5f3153d93a7182622cf8e4d338a0a640f37923af9687b03fb6a76312a38cf0ad4e47cfc9ffefbf1c49e607
7
- data.tar.gz: 873fa97d1be88c0343c6a881b8e77a9550b310d037e1d95cdd90e9183a44c6b04f8d723e76880d76a57874a7d8eabc936d66437a5e42cda8d9a8cfed123b2517
6
+ metadata.gz: 153091bc0d40028cdddcb87c33cc987d28d630bf05ce34ab7e75ab370992cbe448f547650388116fa351ad9d769ba98987b9513ae6d040af61297bd5aa31efa7
7
+ data.tar.gz: be05e4a6bc6e80d85322993b3c2b9974b3671f7e05a2ca25d4d00af7f9e3486c457e4b74e185c37174e702b07f52763c2012371709dd0f1dea5a2446020c20c5
data/README.md CHANGED
@@ -2,9 +2,7 @@
2
2
  [![Build Status](https://travis-ci.org/NGMarmaduke/cisco_spark-ruby.svg?branch=master)](https://travis-ci.org/NGMarmaduke/cisco_spark-ruby)
3
3
  [![Coverage Status](https://coveralls.io/repos/github/NGMarmaduke/cisco_spark-ruby/badge.svg?branch=master)](https://coveralls.io/github/NGMarmaduke/cisco_spark-ruby?branch=master)
4
4
 
5
- Ruby SDK for [Cisco Spark](https://developer.ciscospark.com).
6
-
7
- Work in progress currently
5
+ Ruby client for [Cisco Spark](https://developer.ciscospark.com).
8
6
 
9
7
  ## Installation
10
8
 
@@ -24,13 +22,257 @@ Or install it yourself as:
24
22
 
25
23
  ## Usage
26
24
 
27
- WIP
25
+ ### Configuration
26
+ Configuration can be done in an initializer on app boot.
27
+ An API key or users OAuth token is required to make any API requests.
28
+
29
+ API key example:
30
+ ```ruby
31
+ CiscoSpark.configure do |config|
32
+ config.api_key = 'YOUR KEY'
33
+ end
34
+ ```
35
+
36
+ If you are using OAuth token you can wrap API operaions in a block, all API calls within the block will then use that token.
37
+
38
+ OAuth token example:
39
+ ```ruby
40
+ CiscoSpark.with_token('OAuth token') do
41
+ users_rooms = CiscoSpark::Room.fetch_all
42
+ end
43
+ ```
44
+
45
+ ### Models
46
+ All models have methods to interact with the API and parse response data
47
+
48
+ ###### `klass.fetch_all`
49
+ Fetches all records for a given model, returns a `CiscoSpark::Collection`
50
+ Accepts any parameters that the API allows.
51
+
52
+ ```ruby
53
+ rooms = CiscoSpark::Room.fetch_all(max: 5)
54
+ => #<CiscoSpark::Collection>
55
+ ```
56
+
57
+ ###### `klass.fetch`
58
+ Fetches a single record for the given resource ID, returns an instance
59
+ Accepts any parameters that the API allows.
60
+
61
+ ```ruby
62
+ room = CiscoSpark::Room.fetch('Y2lzY...', show_sip_address: true)
63
+ => #<CiscoSpark::Room>
64
+ ```
65
+
66
+ ###### `klass.create`
67
+ Creates a resource with given attribues, returns an instance
68
+ Accepts any parameters that the API allows.
69
+
70
+ ```ruby
71
+ room = CiscoSpark::Room.create(title: 'Ruby Room')
72
+ => #<CiscoSpark::Room>
73
+ ```
74
+
75
+ ###### `klass.update`
76
+ Updates a resource with given attribues, returns an instance
77
+ Accepts any parameters that the API allows.
78
+
79
+ ```ruby
80
+ room = CiscoSpark::Room.update('Y2lzY...', title: 'Groovey Ruby Room')
81
+ => #<CiscoSpark::Room>
82
+ ```
83
+
84
+ ###### `klass.detroy`
85
+ Destroys a resource with the given ID, returns an boolean to indicate success
86
+ Accepts any parameters that the API allows.
87
+
88
+ ```ruby
89
+ room = CiscoSpark::Room.destroy('Y2lzY...')
90
+ => true
91
+ ```
92
+
93
+ ###### `klass.parse_collection`
94
+ Parses a valid JSON string or a ruby hash/array into a collection of models.
95
+ This is useful for processing data recieved from a webhook etc.
96
+
97
+ ```ruby
98
+ json_string = '{"items": [{ "id": "Y2lzY...", "key": "value" }]}'
99
+ rooms = CiscoSpark::Room.parse_collection(json_string)
100
+ => #<CiscoSpark::Collection>
101
+ ```
102
+
103
+ ###### `klass.parse`
104
+ Parses a valid JSON string or a ruby hash/array into a model.
105
+ This is useful for processing data recieved from a webhook etc.
106
+
107
+ ```ruby
108
+ json_string = '{ "id": "Y2lzY...", "key": "value" }'
109
+ room = CiscoSpark::Room.parse(json_string)
110
+ => #<CiscoSpark::Room>
111
+ ```
112
+
113
+ ###### `klass.new`
114
+ Creates a new instance of the model with given attributes
115
+
116
+ ```ruby
117
+ room = CiscoSpark::Room.new(title: 'New room')
118
+ => #<CiscoSpark::Room>
119
+ ```
120
+
121
+ ###### `instance.persist`
122
+ You can call persist on an instance to create or update it through the API.
123
+ If the instance already has an ID a `PUT` will be made to update the mutable attributes
124
+
125
+ ```ruby
126
+ room = CiscoSpark::Room.new(title: 'New room')
127
+ room.persist
128
+ => #<CiscoSpark::Room>
129
+ ```
130
+
131
+ ###### `instance.fetch`
132
+ You can call fetch on an instance fetch or refresh an instance
133
+
134
+ ```ruby
135
+ room = CiscoSpark::Room.new(id: 'Y2lzY...')
136
+ room.fetch
137
+ => #<CiscoSpark::Room>
138
+ ```
139
+
140
+ ###### `instance.destroy`
141
+ You can call destroy on an instance to destroy it through the API
142
+
143
+ ```ruby
144
+ room = CiscoSpark::Room.new(id: 'Y2lzY...')
145
+ room.destroy
146
+ => true
147
+ ```
148
+
149
+ ###### `instance.to_h`
150
+ Convert a model instance into a hash
151
+
152
+ ```ruby
153
+ room = CiscoSpark::Room.fetch('Y2lzY...')
154
+ room.to_h
155
+ => Hash
156
+ ```
157
+
158
+ ##### `CiscoSpark::Person`
159
+ [API reference](https://developer.ciscospark.com/resource-people.html)
160
+
161
+ ###### `CiscoSpark::Person.all_by_email`
162
+ Search all people by email,
163
+
164
+ ```ruby
165
+ people = CiscoSpark::Person.all_by_email('nickpmaher@gmail.com', max: 5)
166
+ => #<CiscoSpark::Collection>
167
+ ```
168
+
169
+ ###### `CiscoSpark::Person.all_by_name`
170
+ Search all people by display name
171
+
172
+ ```ruby
173
+ people = CiscoSpark::Person.all_by_name('Nick', max: 5)
174
+ => #<CiscoSpark::Collection>
175
+ ```
176
+
177
+ ###### `#memberships`
178
+ Get all memberships for person
179
+
180
+ ```ruby
181
+ person = CiscoSpark::Person.new(id: 'Y2lz...')
182
+ person.memberships(max: 5)
183
+ => #<CiscoSpark::Collection>
184
+ ```
185
+
186
+ ##### `CiscoSpark::Room`
187
+ [API reference](https://developer.ciscospark.com/resource-rooms.html)
188
+
189
+ ###### `#memberships`
190
+ Get all memberships for room
191
+
192
+ ```ruby
193
+ room = CiscoSpark::Room.new(id: 'Y3de...')
194
+ room.memberships(max: 5)
195
+ => #<CiscoSpark::Collection>
196
+ ```
197
+
198
+ ###### `#messages`
199
+ Get all messages for room
200
+
201
+ ```ruby
202
+ room = CiscoSpark::Room.new(id: 'Y3de...')
203
+ room.messages(max: 5)
204
+ => #<CiscoSpark::Collection>
205
+ ```
206
+
207
+ ###### `#messages_before_message`
208
+ Get all message before a given message, accepts a `CiscoSpark::Message` or a message ID
209
+
210
+ ```ruby
211
+ room.all_before_message('Y3de...', max: 5)
212
+ => #<CiscoSpark::Collection>
213
+ ```
214
+
215
+ ###### `#messages_before`
216
+ Get all message before a given time, accepts a `DateTime` or a string
217
+
218
+ ```ruby
219
+ room.messages_before(DateTime.now, max: 5)
220
+ => #<CiscoSpark::Collection>
221
+ ```
222
+
223
+ ###### `#send_message`
224
+ Send a message to the room
225
+
226
+ ```ruby
227
+ message = CiscoSpark::Message.new(text: 'Hello Spark')
228
+
229
+ room = CiscoSpark::Room.new(id: 'Y3de...')
230
+ room.send_message(message)
231
+ => #<CiscoSpark::Message>
232
+ ```
233
+
234
+ ###### `#add_person`
235
+ Creates a new membership to the room for a given person
236
+
237
+ ```ruby
238
+ person = CiscoSpark::Person.fetch('Y2lz')
239
+
240
+ room = CiscoSpark::Room.new(id: 'Y3de...')
241
+ room.add_person(person)
242
+ => #<CiscoSpark::Membership>
243
+ ```
244
+
245
+ ##### `CiscoSpark::Membership`
246
+ [API reference](https://developer.ciscospark.com/resource-memberships.html)
247
+
248
+ ###### `#person`
249
+ Returns the person for this membership
250
+
251
+ ###### `#room`
252
+ Returns the room this membership belongs to
253
+
254
+ ##### `CiscoSpark::Message`
255
+ [API reference](https://developer.ciscospark.com/resource-messages.html)
256
+
257
+ ###### `#person`
258
+ Returns the person that sent this message
259
+
260
+ ###### `#person_to`
261
+ Returns the person that the message was sent to
262
+
263
+ ##### `CiscoSpark::Webhook`
264
+ [API reference](https://developer.ciscospark.com/resource-webhooks.html)
265
+
266
+ ### `CiscoSpark::Collection`
267
+ Wraps a collection of models. Responds to all enumerable methods e.g. `first`, `map`, `each` etc...
268
+
269
+ Call `next` on a collection to load the next page of data
28
270
 
29
271
  ## Development
30
272
 
31
273
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
274
 
33
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
275
+ To install this gem onto your local machine, run `bundle exec rake install`.
34
276
 
35
277
  ## Contributing
36
278
 
@@ -23,8 +23,6 @@ module CiscoSpark
23
23
  @resource = resource
24
24
  @request_body = request_body
25
25
 
26
- puts "request_body: #{request_body}"
27
-
28
26
  do_put
29
27
  end
30
28
 
@@ -49,9 +47,7 @@ module CiscoSpark
49
47
  response = http_client.request(get_request)
50
48
  debug(response) if CiscoSpark.debug
51
49
 
52
- if response.is_a?(Net::HTTPSuccess)
53
- JSON.parse(response.body)
54
- end
50
+ response
55
51
  end
56
52
 
57
53
  def do_post
@@ -62,9 +58,7 @@ module CiscoSpark
62
58
  response = http_client.request(post_request)
63
59
  debug(response) if CiscoSpark.debug
64
60
 
65
- if response.is_a?(Net::HTTPSuccess)
66
- JSON.parse(response.body)
67
- end
61
+ response
68
62
  end
69
63
 
70
64
  def do_put
@@ -75,10 +69,7 @@ module CiscoSpark
75
69
  response = http_client.request(post_request)
76
70
  debug(response) if CiscoSpark.debug
77
71
 
78
- puts "response.body: #{response.body}"
79
- if response.is_a?(Net::HTTPSuccess)
80
- JSON.parse(response.body)
81
- end
72
+ response
82
73
  end
83
74
 
84
75
  def http_client
@@ -0,0 +1,48 @@
1
+ module CiscoSpark
2
+ class Collection
3
+ attr_accessor :collection, :model_klass
4
+
5
+ def initialize(model_klass, collection=[], response=nil)
6
+ @model_klass = model_klass
7
+ @collection = collection
8
+
9
+ parse_pagination(response)
10
+ end
11
+
12
+
13
+
14
+ def method_missing(name, *args, &block)
15
+ if collection.respond_to?(name)
16
+ collection.send(name, *args, &block)
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ def next
23
+ return false unless next_params
24
+ response = model_klass.fetch_all_raw(next_params)
25
+ @collection = model_klass.parse_collection(response.body)
26
+ parse_pagination(response)
27
+ self
28
+ end
29
+
30
+ private
31
+
32
+ attr_accessor :next_params
33
+
34
+ def parse_pagination(response)
35
+ @next_params = nil
36
+ return unless response && response['Link']
37
+
38
+ matches = /<(?<next>.*)>; rel="next"/.match(response['Link'])
39
+ next_url = matches[:next]
40
+
41
+ if next_url
42
+ next_uri = URI.parse(next_url)
43
+ @next_params = Hash[URI::decode_www_form(next_uri.query)]
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -1,4 +1,5 @@
1
1
  require "cisco_spark/api"
2
+ require "cisco_spark/collection"
2
3
  require "cisco_spark/data_caster"
3
4
 
4
5
  module CiscoSpark
@@ -25,36 +26,44 @@ module CiscoSpark
25
26
  @mutable_attributes = attributes
26
27
  end
27
28
 
29
+ def fetch_all_raw(options={})
30
+ Api.new.get(@resource, options)
31
+ end
32
+
28
33
  def fetch_all(options={})
29
- response = Api.new.get(@resource, options)
30
- parse_collection(response['items'])
34
+ response = fetch_all_raw(options)
35
+ collection = parse_collection(response.body)
36
+ CiscoSpark::Collection.new(self, collection, response)
31
37
  end
32
38
 
33
39
  def fetch(id, options={})
34
40
  response = Api.new.get("#{@resource}/#{id}", options)
35
- parse(response)
41
+ parse(response.body)
36
42
  end
37
43
 
38
44
  def create(attributes)
39
45
  response = Api.new.post(@resource, attributes)
40
- parse(response)
46
+ parse(response.body)
41
47
  end
42
48
 
43
49
  def update(id, attributes)
44
50
  attributes = attributes.select{ |name, _v| @mutable_attributes.include?(name) }
45
51
  response = Api.new.put("#{@resource}/#{id}", attributes)
46
- parse(response)
52
+ parse(response.body)
47
53
  end
48
54
 
49
55
  def destroy(id)
50
56
  Api.new.delete("#{@resource}/#{id}")
51
57
  end
52
58
 
53
- def parse_collection(collection)
54
- collection.map{ |hash| parse(hash) }
59
+ def parse_collection(object)
60
+ object = JSON.parse(object) if object.is_a?(String)
61
+ object = object.fetch('items', []) if object.is_a?(Hash)
62
+ object.map{ |hash| parse(hash) }
55
63
  end
56
64
 
57
65
  def parse(hash)
66
+ hash = JSON.parse(hash) if hash.is_a?(String)
58
67
  params = attributes.each_with_object({}) do |(attribute, caster), params|
59
68
  params[attribute] = caster.call(hash[Utils.camelize(attribute)])
60
69
  end
@@ -68,6 +77,7 @@ module CiscoSpark
68
77
 
69
78
  def fetch
70
79
  merge_attributes(self.class.fetch(id))
80
+ self
71
81
  end
72
82
 
73
83
  def persist
@@ -88,12 +98,14 @@ module CiscoSpark
88
98
 
89
99
  def create
90
100
  merge_attributes(self.class.create(to_h))
101
+ self
91
102
  end
92
103
 
93
104
  def update
94
105
  attrs = to_h
95
106
  id = attrs.delete(:id)
96
107
  merge_attributes(self.class.update(id, attrs))
108
+ self
97
109
  end
98
110
 
99
111
  def merge_attributes(object)
@@ -20,6 +20,22 @@ module CiscoSpark
20
20
  CiscoSpark::Message.fetch_all(options)
21
21
  end
22
22
 
23
+ def messages_before_message(message, options={})
24
+ message_id = message.is_a?(CiscoSpark::Message) ? message.id : message
25
+ options[:before_message] = message_id
26
+ options[:room_id] = id
27
+ CiscoSpark::Message.fetch_all(options)
28
+ end
29
+
30
+ def messages_before(date, options={})
31
+ if date.is_a?(DateTime)
32
+ date = date.to_time.iso8601
33
+ end
34
+ options[:room_id] = id
35
+ options[:before] = date
36
+ CiscoSpark::Message.fetch_all(options)
37
+ end
38
+
23
39
  def memberships(options={})
24
40
  options[:room_id] = id
25
41
  CiscoSpark::Membership.fetch_all(options)
@@ -27,7 +43,7 @@ module CiscoSpark
27
43
 
28
44
  def send_message(message)
29
45
  message.room_id = id
30
- message.persit
46
+ message.persist
31
47
  end
32
48
 
33
49
  def add_person(person, options={})
@@ -0,0 +1,20 @@
1
+ require "cisco_spark/model"
2
+
3
+ module CiscoSpark
4
+ class Webhook
5
+ include Model
6
+ resource 'webhooks'
7
+
8
+ attributes(
9
+ id: DataCaster::String,
10
+ name: DataCaster::String,
11
+ target_url: DataCaster::String,
12
+ resource: DataCaster::String,
13
+ event: DataCaster::String,
14
+ filter: DataCaster::String,
15
+ created: DataCaster::DateTime,
16
+ )
17
+ mutable_attributes :name, :target_url
18
+
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module CiscoSpark
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cisco_spark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Maher
@@ -101,6 +101,7 @@ files:
101
101
  - cisco_spark.gemspec
102
102
  - lib/cisco_spark.rb
103
103
  - lib/cisco_spark/api.rb
104
+ - lib/cisco_spark/collection.rb
104
105
  - lib/cisco_spark/configuration.rb
105
106
  - lib/cisco_spark/data_caster.rb
106
107
  - lib/cisco_spark/errors.rb
@@ -109,6 +110,7 @@ files:
109
110
  - lib/cisco_spark/models/message.rb
110
111
  - lib/cisco_spark/models/person.rb
111
112
  - lib/cisco_spark/models/room.rb
113
+ - lib/cisco_spark/models/webhook.rb
112
114
  - lib/cisco_spark/utils.rb
113
115
  - lib/cisco_spark/version.rb
114
116
  homepage: https://github.com/NGMarmaduke/cisco_spark-ruby