cisco_spark 0.1.1 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a5540370e43218f4fb8bb8386b58d209e98f380
4
- data.tar.gz: 24dda8bfd4c8b55f8192a87ab698ebd2a62be3b4
3
+ metadata.gz: c90cd5f9c41757ffde59ad64721f2b3e8ef51008
4
+ data.tar.gz: 3b508c1b6e52bebf5f6b50bee0ee62940e676bbc
5
5
  SHA512:
6
- metadata.gz: b1c8072e90d16b0773484d8719d09c6bb9679211e1ce2153951581d317c68ad25c9d45728423c6906a60370e3b220110db0d3ea38db29a1add29c11ef735002c
7
- data.tar.gz: 9aa03be67d73155bc74674aa668a7175d6534f4278c3571a83f81fbd191a6e4b3282a954ce2f45e902d73403779f34ccf125a6e4969659d4f55cc9aa5b71152f
6
+ metadata.gz: 6a2d4981ac441a9be1942be4c3b40b607a05af0e85fe577d85907a71c4a91ae986c3b70eda9478502b33fe361088080e1dd267a55a3ebb44b2d882422182382b
7
+ data.tar.gz: 9621af4da71d27a523f016689ba841d32e5fb2731bdf0e255e1cce3ed7735790a9e4f0e20eaa2d1c06bfcf7524de2b81127a3156e10aa86a0efd6981f94892e3
@@ -3,15 +3,50 @@ require 'json'
3
3
 
4
4
  module CiscoSpark
5
5
  class Api
6
- attr_accessor :resource, :params
6
+ attr_accessor :resource, :params, :request_body
7
7
 
8
- def initialize(resource, params={})
8
+ def get(resource, params={})
9
9
  @resource = resource
10
10
  @params = params
11
+
12
+ do_get
13
+ end
14
+
15
+ def post(resource, request_body={})
16
+ @resource = resource
17
+ @request_body = request_body
18
+
19
+ do_post
20
+ end
21
+
22
+ def put(resource, request_body={})
23
+ @resource = resource
24
+ @request_body = request_body
25
+
26
+ puts "request_body: #{request_body}"
27
+
28
+ do_put
29
+ end
30
+
31
+ def delete(resource)
32
+ @resource = resource
33
+
34
+ do_delete
35
+ end
36
+
37
+ private
38
+
39
+ def do_delete
40
+ delete_request = request(Net::HTTP::Delete)
41
+ response = http_client.request(delete_request)
42
+ debug(response) if CiscoSpark.debug
43
+
44
+ response.is_a?(Net::HTTPSuccess)
11
45
  end
12
46
 
13
- def result
14
- response = http_client.request(request)
47
+ def do_get
48
+ get_request = request(Net::HTTP::Get)
49
+ response = http_client.request(get_request)
15
50
  debug(response) if CiscoSpark.debug
16
51
 
17
52
  if response.is_a?(Net::HTTPSuccess)
@@ -19,7 +54,32 @@ module CiscoSpark
19
54
  end
20
55
  end
21
56
 
22
- private
57
+ def do_post
58
+ post_request = request(Net::HTTP::Post)
59
+ post_request.set_form_data(request_body)
60
+ debug(post_request) if CiscoSpark.debug
61
+
62
+ response = http_client.request(post_request)
63
+ debug(response) if CiscoSpark.debug
64
+
65
+ if response.is_a?(Net::HTTPSuccess)
66
+ JSON.parse(response.body)
67
+ end
68
+ end
69
+
70
+ def do_put
71
+ post_request = request(Net::HTTP::Put)
72
+ post_request.set_form_data(request_body)
73
+ debug(post_request) if CiscoSpark.debug
74
+
75
+ response = http_client.request(post_request)
76
+ debug(response) if CiscoSpark.debug
77
+
78
+ puts "response.body: #{response.body}"
79
+ if response.is_a?(Net::HTTPSuccess)
80
+ JSON.parse(response.body)
81
+ end
82
+ end
23
83
 
24
84
  def http_client
25
85
  client = Net::HTTP.new(request_uri.host, request_uri.port)
@@ -27,11 +87,10 @@ module CiscoSpark
27
87
  client
28
88
  end
29
89
 
30
- def request
31
- request = Net::HTTP::Get.new(request_uri)
90
+ def request(request_class)
91
+ request = request_class.new(request_uri)
32
92
  request['Content-type'] = "application/json; charset=utf-8"
33
93
  request['Authorization'] = "Bearer #{CiscoSpark.api_key}"
34
- debug(request) if CiscoSpark.debug
35
94
  request
36
95
  end
37
96
 
@@ -49,8 +108,14 @@ module CiscoSpark
49
108
  end
50
109
 
51
110
  def params
52
- @params.each_with_object({}) do |(key, value), hash|
53
- hash[Utils.camelize(key)] = value
111
+ (@params || {}).each_with_object({}) do |(key, value), hash|
112
+ hash[Utils.camelize(key)] = value if value
113
+ end
114
+ end
115
+
116
+ def request_body
117
+ @request_body.each_with_object({}) do |(key, value), hash|
118
+ hash[Utils.camelize(key)] = value if value
54
119
  end
55
120
  end
56
121
 
@@ -2,5 +2,5 @@ module DataCaster
2
2
  Array = lambda { |data| data }
3
3
  Boolean = lambda { |data| data }
4
4
  String = lambda { |data| data.to_s }
5
- DateTime = lambda { |data| ::DateTime.parse(data) }
5
+ DateTime = lambda { |data| data ? ::DateTime.parse(data) : nil }
6
6
  end
@@ -8,7 +8,9 @@ module CiscoSpark
8
8
  end
9
9
 
10
10
  module ClassMethods
11
- def resource(resource)
11
+ def resource(resource=nil)
12
+ return @resource unless resource
13
+
12
14
  @resource = resource
13
15
  end
14
16
 
@@ -19,17 +21,35 @@ module CiscoSpark
19
21
  attr_accessor *@attributes.keys
20
22
  end
21
23
 
24
+ def mutable_attributes(*attributes)
25
+ @mutable_attributes = attributes
26
+ end
27
+
22
28
  def fetch_all(options={})
23
- response = Api.new(@resource, options).result
29
+ response = Api.new.get(@resource, options)
24
30
  parse_collection(response['items'])
25
31
  end
26
32
 
27
33
  def fetch(id, options={})
28
- resource = "#{@resource}/#{id}"
29
- response = Api.new(resource, options).result
34
+ response = Api.new.get("#{@resource}/#{id}", options)
30
35
  parse(response)
31
36
  end
32
37
 
38
+ def create(attributes)
39
+ response = Api.new.post(@resource, attributes)
40
+ parse(response)
41
+ end
42
+
43
+ def update(id, attributes)
44
+ attributes = attributes.select{ |name, _v| @mutable_attributes.include?(name) }
45
+ response = Api.new.put("#{@resource}/#{id}", attributes)
46
+ parse(response)
47
+ end
48
+
49
+ def destroy(id)
50
+ Api.new.delete("#{@resource}/#{id}")
51
+ end
52
+
33
53
  def parse_collection(collection)
34
54
  collection.map{ |hash| parse(hash) }
35
55
  end
@@ -46,13 +66,44 @@ module CiscoSpark
46
66
  assign_attributes(attributes)
47
67
  end
48
68
 
69
+ def fetch
70
+ merge_attributes(self.class.fetch(id))
71
+ end
72
+
73
+ def persist
74
+ id ? update : create
75
+ end
76
+
77
+ def destroy
78
+ self.class.destroy(id)
79
+ end
80
+
81
+ def to_h
82
+ self.class.attributes.keys.each_with_object({}) { |name, hash|
83
+ hash[name] = send(name)
84
+ }
85
+ end
86
+
49
87
  private
50
88
 
89
+ def create
90
+ merge_attributes(self.class.create(to_h))
91
+ end
92
+
93
+ def update
94
+ attrs = to_h
95
+ id = attrs.delete(:id)
96
+ merge_attributes(self.class.update(id, attrs))
97
+ end
98
+
99
+ def merge_attributes(object)
100
+ assign_attributes(object.to_h)
101
+ end
102
+
51
103
  def assign_attributes(attributes)
52
104
  attributes.each do |name, value|
53
105
  send("#{name}=", value)
54
106
  end
55
107
  end
56
-
57
108
  end
58
109
  end
@@ -15,6 +15,7 @@ module CiscoSpark
15
15
  is_monitor: DataCaster::Boolean,
16
16
  created: DataCaster::DateTime,
17
17
  )
18
+ mutable_attributes :is_moderator, :is_monitor
18
19
 
19
20
  def person
20
21
  CiscoSpark::Person.fetch(person_id)
@@ -13,5 +13,19 @@ module CiscoSpark
13
13
  created: DataCaster::DateTime,
14
14
  )
15
15
 
16
+ def self.all_by_email(email, options={})
17
+ options[:email] = email
18
+ self.fetch_all(options)
19
+ end
20
+
21
+ def self.all_by_name(name, options={})
22
+ options[:display_name] = name
23
+ self.fetch_all(options)
24
+ end
25
+
26
+ def memberships(options={})
27
+ options[person_id] = id
28
+ CiscoSpark::Membership.fetch_all(options)
29
+ end
16
30
  end
17
31
  end
@@ -13,9 +13,30 @@ module CiscoSpark
13
13
  is_locked: DataCaster::Boolean,
14
14
  sip_address: DataCaster::String
15
15
  )
16
+ mutable_attributes :title
16
17
 
17
- def messages
18
- CiscoSpark::Message.fetch_all(room_id: id)
18
+ def messages(options={})
19
+ options[:room_id] = id
20
+ CiscoSpark::Message.fetch_all(options)
21
+ end
22
+
23
+ def memberships(options={})
24
+ options[:room_id] = id
25
+ CiscoSpark::Membership.fetch_all(options)
26
+ end
27
+
28
+ def send_message(message)
29
+ message.room_id = id
30
+ message.persit
31
+ end
32
+
33
+ def add_person(person, options={})
34
+ CiscoSpark::Membership.new(
35
+ room_id: id,
36
+ person_id: person.id,
37
+ is_moderator: options.fetch(:is_moderator, false),
38
+ is_monitor: options.fetch(:is_monitor, false),
39
+ ).persist
19
40
  end
20
41
  end
21
42
  end
@@ -1,3 +1,3 @@
1
1
  module CiscoSpark
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cisco_spark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Maher
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-09 00:00:00.000000000 Z
11
+ date: 2016-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler