nestful 1.0.6 → 1.0.7

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: 878611ee4c2ca7b8ea65fa4014fd7496eb34e791
4
- data.tar.gz: f4cebde0bdf692351b2743e4078ecf8d2c593da4
3
+ metadata.gz: 39b78f8d5346095d86fb0c3f12840b2239aa06dc
4
+ data.tar.gz: f05421aa7dbf52811fba5552315087aec3df3f12
5
5
  SHA512:
6
- metadata.gz: 09dd455b8dacdabcc290c2789baca95c2c289cfaa193f86e66d880e37589c15621aa388dbb736a3fe7b0a1af982cb4df080c5e5e335cde7d4026c436dd8497c7
7
- data.tar.gz: 4c20fe075decefbad94276787cf6ece4567f75b396fed8c55f871d9c89c4472bba4a191777a00d1f35c50f6c6282a3256610856307b09d16dc2a12246993f07d
6
+ metadata.gz: db3c0bbefc9e9e5b031f070f4735b50dd2970adb6b5b50a7eb6787f5d4478b96cc08952c16a5d56fd4241a3a643637301b863e512b9131207149d6302895f9ec
7
+ data.tar.gz: d6d34a808b5417bdde3a0e92aed856822e6835a577aec77d9d38cf04836f2b71fe9285bf526d4ca69be5c17afbd29139259d5523deae495c0d85b717cfd9c89a
@@ -0,0 +1,12 @@
1
+ require 'nestful'
2
+ require 'csv'
3
+
4
+ page_speed = Nestful::Mash.get(
5
+ 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed',
6
+ url: 'http://monocle.io',
7
+ key: ENV['GOOGLE_SECRET']
8
+ )
9
+
10
+ CSV.open('stats.csv', 'a') do |csv|
11
+ csv << [Time.now, page_speed.score, *page_speed.pageStats.values]
12
+ end
@@ -6,6 +6,7 @@ module Nestful
6
6
  autoload :Formats, 'nestful/formats'
7
7
  autoload :Connection, 'nestful/connection'
8
8
  autoload :Helpers, 'nestful/helpers'
9
+ autoload :Mash, 'nestful/mash'
9
10
  autoload :Request, 'nestful/request'
10
11
  autoload :Response, 'nestful/response'
11
12
  autoload :Resource, 'nestful/resource'
@@ -0,0 +1,206 @@
1
+ module Nestful
2
+ class Mash < Hash
3
+ def self.get(*args)
4
+ from_response(Nestful.get(*args))
5
+ end
6
+
7
+ def self.post(*args)
8
+ from_response(Nestful.post(*args))
9
+ end
10
+
11
+ def self.put(*args)
12
+ from_response(Nestful.put(*args))
13
+ end
14
+
15
+ def self.delete(*args)
16
+ from_response(Nestful.delete(*args))
17
+ end
18
+
19
+ def self.request(*args)
20
+ from_response(Nestful.request(*args))
21
+ end
22
+
23
+ def self.from_response(response)
24
+ case response.decoded
25
+ when Hash
26
+ self.new(response.decoded)
27
+ when Array
28
+ response.decoded.map {|v| self.new(v) }
29
+ else
30
+ response
31
+ end
32
+ end
33
+
34
+ alias_method :to_s, :inspect
35
+
36
+ attr_reader :response
37
+
38
+ def initialize(source_hash = nil, default = nil, &blk)
39
+ deep_update(source_hash) if source_hash
40
+ default ? super(default) : super(&blk)
41
+ end
42
+
43
+ class << self; alias [] new; end
44
+
45
+ def id #:nodoc:
46
+ self['id']
47
+ end
48
+
49
+ def type #:nodoc:
50
+ self['type']
51
+ end
52
+
53
+ alias_method :regular_reader, :[]
54
+ alias_method :regular_writer, :[]=
55
+
56
+ # Retrieves an attribute set in the Mash. Will convert
57
+ # any key passed in to a string before retrieving.
58
+ def custom_reader(key)
59
+ value = regular_reader(convert_key(key))
60
+ yield value if block_given?
61
+ value
62
+ end
63
+
64
+ # Sets an attribute in the Mash. Key will be converted to
65
+ # a string before it is set, and Hashes will be converted
66
+ # into Mashes for nesting purposes.
67
+ def custom_writer(key,value) #:nodoc:
68
+ regular_writer(convert_key(key), convert_value(value))
69
+ end
70
+
71
+ alias_method :[], :custom_reader
72
+ alias_method :[]=, :custom_writer
73
+
74
+ # This is the bang method reader, it will return a new Mash
75
+ # if there isn't a value already assigned to the key requested.
76
+ def initializing_reader(key)
77
+ ck = convert_key(key)
78
+ regular_writer(ck, self.class.new) unless key?(ck)
79
+ regular_reader(ck)
80
+ end
81
+
82
+ # This is the under bang method reader, it will return a temporary new Mash
83
+ # if there isn't a value already assigned to the key requested.
84
+ def underbang_reader(key)
85
+ ck = convert_key(key)
86
+ if key?(ck)
87
+ regular_reader(ck)
88
+ else
89
+ self.class.new
90
+ end
91
+ end
92
+
93
+ def fetch(key, *args)
94
+ super(convert_key(key), *args)
95
+ end
96
+
97
+ def delete(key)
98
+ super(convert_key(key))
99
+ end
100
+
101
+ alias_method :regular_dup, :dup
102
+ # Duplicates the current mash as a new mash.
103
+ def dup
104
+ self.class.new(self, self.default)
105
+ end
106
+
107
+ def key?(key)
108
+ super(convert_key(key))
109
+ end
110
+ alias_method :has_key?, :key?
111
+ alias_method :include?, :key?
112
+ alias_method :member?, :key?
113
+
114
+ # Performs a deep_update on a duplicate of the
115
+ # current mash.
116
+ def deep_merge(other_hash, &blk)
117
+ dup.deep_update(other_hash, &blk)
118
+ end
119
+ alias_method :merge, :deep_merge
120
+
121
+ # Recursively merges this mash with the passed
122
+ # in hash, merging each hash in the hierarchy.
123
+ def deep_update(other_hash, &blk)
124
+ other_hash.each_pair do |k,v|
125
+ key = convert_key(k)
126
+ if regular_reader(key).is_a?(Mash) and v.is_a?(::Hash)
127
+ custom_reader(key).deep_update(v, &blk)
128
+ else
129
+ value = convert_value(v, true)
130
+ value = blk.call(key, self[k], value) if blk
131
+ custom_writer(key, value)
132
+ end
133
+ end
134
+ self
135
+ end
136
+ alias_method :deep_merge!, :deep_update
137
+ alias_method :update, :deep_update
138
+ alias_method :merge!, :update
139
+
140
+ # Performs a shallow_update on a duplicate of the current mash
141
+ def shallow_merge(other_hash)
142
+ dup.shallow_update(other_hash)
143
+ end
144
+
145
+ # Merges (non-recursively) the hash from the argument,
146
+ # changing the receiving hash
147
+ def shallow_update(other_hash)
148
+ other_hash.each_pair do |k,v|
149
+ regular_writer(convert_key(k), convert_value(v, true))
150
+ end
151
+ self
152
+ end
153
+
154
+ def replace(other_hash)
155
+ (keys - other_hash.keys).each { |key| delete(key) }
156
+ other_hash.each { |key, value| self[key] = value }
157
+ self
158
+ end
159
+
160
+ # Will return true if the Mash has had a key
161
+ # set in addition to normal respond_to? functionality.
162
+ def respond_to?(method_name, include_private=false)
163
+ return true if key?(method_name) || method_name.to_s.slice(/[=?!_]\Z/)
164
+ super
165
+ end
166
+
167
+ def method_missing(method_name, *args, &blk)
168
+ return self.[](method_name, &blk) if key?(method_name)
169
+ match = method_name.to_s.match(/(.*?)([?=!_]?)$/)
170
+ case match[2]
171
+ when "="
172
+ self[match[1]] = args.first
173
+ when "?"
174
+ !!self[match[1]]
175
+ when "!"
176
+ initializing_reader(match[1])
177
+ when "_"
178
+ underbang_reader(match[1])
179
+ else
180
+ default(method_name, *args, &blk)
181
+ end
182
+ end
183
+
184
+ protected
185
+
186
+ def convert_key(key) #:nodoc:
187
+ key.to_s
188
+ end
189
+
190
+ def convert_value(val, duping=false) #:nodoc:
191
+ case val
192
+ when self.class
193
+ val.dup
194
+ when Hash
195
+ duping ? val.dup : val
196
+ when ::Hash
197
+ val = val.dup if duping
198
+ self.class.new(val)
199
+ when Array
200
+ val.collect{ |e| convert_value(e) }
201
+ else
202
+ val
203
+ end
204
+ end
205
+ end
206
+ end
@@ -138,6 +138,20 @@ module Nestful
138
138
  end
139
139
  end
140
140
 
141
+ def ==(other)
142
+ other.equal?(self) ||
143
+ (other.instance_of?(self.class) && other.attributes == attributes)
144
+ end
145
+
146
+ # Tests for equality (delegates to ==).
147
+ def eql?(other)
148
+ self == other
149
+ end
150
+
151
+ def hash
152
+ attributes.hash
153
+ end
154
+
141
155
  alias_method :respond_to_without_attributes?, :respond_to?
142
156
 
143
157
  def respond_to?(method, include_priv = false)
@@ -1,3 +1,3 @@
1
1
  module Nestful
2
- VERSION = "1.0.6"
2
+ VERSION = "1.0.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nestful
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex MacCaw
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-21 00:00:00.000000000 Z
11
+ date: 2013-10-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -23,6 +23,7 @@ files:
23
23
  - README.markdown
24
24
  - Rakefile
25
25
  - examples/oembed.rb
26
+ - examples/pagespeed.rb
26
27
  - examples/resource.rb
27
28
  - lib/nestful.rb
28
29
  - lib/nestful/.DS_Store
@@ -33,6 +34,7 @@ files:
33
34
  - lib/nestful/formats/form_format.rb
34
35
  - lib/nestful/formats/json_format.rb
35
36
  - lib/nestful/helpers.rb
37
+ - lib/nestful/mash.rb
36
38
  - lib/nestful/request.rb
37
39
  - lib/nestful/resource.rb
38
40
  - lib/nestful/response.rb