adafruit-io 1.0.4 → 2.0.0.beta.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.
@@ -1,90 +0,0 @@
1
- require 'uri'
2
- require 'adafruit/io/client'
3
- require 'adafruit/io/client/io_object'
4
- require 'active_model'
5
- require 'active_support/core_ext/object/instance_variables'
6
-
7
- module Adafruit
8
- module IO
9
- class IOObject
10
- attr_accessor :unsaved_values, :id_or_key, :values
11
-
12
- def initialize(client = nil, id_or_key = nil)
13
- @client = client
14
- @id_or_key = id_or_key
15
- @unsaved_values = Set.new
16
- @values = {}
17
- end
18
-
19
- protected
20
- # serialize_params derived from stripe-ruby library, MIT License,
21
- # Copyright (c) 2011- Stripe, Inc. (https://stripe.com)
22
- #
23
- # https://github.com/stripe/stripe-ruby/blob/9cf5089dc15534b7ed581e0ce4d84fa82f592efb/lib/stripe/api_operations/update.rb#L37
24
- def serialize_params(obj)
25
- return '' if obj.nil?
26
-
27
- unsaved_keys = obj.instance_variable_get(:@unsaved_values)
28
- obj_values = obj.instance_variable_get(:@values)
29
- update_hash = {}
30
-
31
- unsaved_keys.each do |k|
32
- update_hash[k] = obj_values[k]
33
- end
34
-
35
- update_hash
36
- end
37
-
38
- def process_response(response)
39
- response = JSON.parse(response, :symbolize_names => true)
40
-
41
- if response.is_a?(Array)
42
- obj_list = []
43
- response.each do |r|
44
- if @feed.present?
45
- obj_list.push(self.class.new(@client, @feed, @id_or_key).parse(r))
46
- else
47
- obj_list.push(self.class.new(@client, @id_or_key).parse(r))
48
- end
49
- end
50
- else
51
- if @feed.present?
52
- obj_list = self.class.new(@client, @feed, @id_or_key).parse(response)
53
- else
54
- obj_list = self.class.new(@client, @id_or_key).parse(response)
55
- end
56
- end
57
-
58
- return obj_list
59
- end
60
-
61
- def parse(o)
62
- o.each do |k, v|
63
- singleton_class.class_eval do; attr_accessor_with_changes "#{k}"; end
64
- send("#{k}=", v)
65
- @unsaved_values.delete(k.to_s)
66
- end
67
-
68
- return self
69
- end
70
-
71
- def self.attr_accessor_with_changes(attr_name)
72
- attr_name = attr_name.to_s
73
-
74
- #getter
75
- self.class_eval("def #{attr_name};@#{attr_name};end")
76
-
77
- #setter
78
- self.class_eval %Q{
79
- def #{attr_name}=(val)
80
- key = __callee__.to_s.chomp("=")
81
- @unsaved_values.add(key)
82
- @values[key] = val
83
- @#{attr_name} = val
84
- end
85
- }
86
-
87
- end
88
- end
89
- end
90
- end
@@ -1,42 +0,0 @@
1
- require 'open-uri'
2
-
3
- module Adafruit
4
- module IO
5
- class Client
6
- module RequestHandler
7
- def handle_get(url, options = {})
8
- response = conn.get do |req|
9
- req.url URI::encode("api/#{url}")
10
- options.each do |k,v|
11
- req.params[k] = v
12
- end
13
- end
14
- return response.body
15
- end
16
-
17
- def handle_post(url, data, options = {})
18
- response = conn.post do |req|
19
- req.url URI::encode("api/#{url}")
20
- req.body = data
21
- end
22
- return response.body
23
- end
24
-
25
- def handle_put(url, data, options = {})
26
- response = conn.put do |req|
27
- req.url URI::encode("api/#{url}")
28
- req.body = data
29
- end
30
- return response.body
31
- end
32
-
33
- def handle_delete(url, options = {})
34
- response = conn.delete do |req|
35
- req.url URI::encode("api/#{url}")
36
- end
37
- return response.status
38
- end
39
- end
40
- end
41
- end
42
- end