easy_api 0.1.2 → 0.1.3

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: da65ca4da22f0eb00d5a0f415b430d466daf0f23
4
- data.tar.gz: 3e170ac28fe723c30ef34e4667348b3f92b27c38
3
+ metadata.gz: dc2ec38871b0d6b22e7b384f277d39353334de01
4
+ data.tar.gz: fad14f7aa23dcf1321fa6e07eeeeacd756d05357
5
5
  SHA512:
6
- metadata.gz: 467ac9f0bd656e485e3d0857210d00237006a5b359c716e4cb0208ee47d14dda94b3c7bbfd920432b2c651740ad4131816174a64b54537b1cb54de2a9e4c7c34
7
- data.tar.gz: 679aaed06073c49e81d59808ba307bc2c2fae6871fa70b5f401a6e6ee473949254163d09d31ab5454e07f7dd228b089c6c1d8538a2ab4427515c11f88ede4a3a
6
+ metadata.gz: 27a9e72ae98ba763de7d57f183c9a229facc02aa8fea402ad5a509163e562a1f9de328305a6066cde9d8a8be341071eac605ba54b645b00513b430c7f33657f2
7
+ data.tar.gz: e8cc2b9f385d35e3e736d9a3a6cace8181c083e3560388e7d6a86d3fe43242e04bb6fb13fa5a33af9982fc23bec5de45a60c133ce49f6623f7cbb9aa22f7d2cb
data/README.md CHANGED
@@ -20,7 +20,48 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ Example usage of Telegram API:
24
+ ```ruby
25
+ class PhotoSize < EasyApi::Object
26
+ def required_attribute_names
27
+ [:file_id, :width, :height,]
28
+ end
29
+ def optional_attribute_names
30
+ [:file_size]
31
+ end
32
+ end
33
+
34
+ class Video < EasyApi::Object
35
+ def required_attribute_names
36
+ [:file_id, :width, :height, :duration]
37
+ end
38
+ def optional_attribute_names
39
+ [:thumb, :mime_type, :file_size]
40
+ end
41
+ def schema
42
+ {thumb: PhotoSize}
43
+ end
44
+ end
45
+
46
+ Video.new(
47
+ file_id: "abc123",
48
+ width: 720,
49
+ height: 480,
50
+ duration: 123,
51
+ thumb: {
52
+ file_id: "123abc",
53
+ width: 128,
54
+ height: 76,
55
+ file_size: 123123123
56
+ },
57
+ file_size: 123445
58
+ )
59
+ ```
60
+
61
+ Outputs:
62
+ ```
63
+ #<Video:0x00007fac3f2ac450 @attributes={:file_id=>"abc123", :width=>720, :height=>480, :duration=>123, :thumb=>#<PhotoSize:0x00007fac3f2ade40 @attributes={:file_id=>"123abc", :width=>128, :height=>76, :file_size=>123123123}>, :file_size=>123445}>
64
+ ```
24
65
 
25
66
  ## Development
26
67
 
data/lib/easy_api.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "easy_api/object"
2
2
 
3
3
  module EasyApi
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -21,22 +21,26 @@ module EasyApi
21
21
  end
22
22
 
23
23
  @attributes = data.map do |k, v|
24
+ [k.to_sym, v]
25
+ end.map do |k, v|
24
26
  next [k, v] unless v.instance_of? Hash
25
27
 
26
28
  v =
27
- if object_class = schema[k.to_sym]
29
+ if object_class = schema[k]
28
30
  object_class.new(v)
29
31
  else
30
32
  OpenStruct.new(v)
31
33
  end
32
34
 
33
- [k.to_sym, v]
35
+ [k, v]
34
36
  end.to_h
37
+
38
+ @attributes = empty_optional_attributes.merge(@attributes)
35
39
  end
36
40
 
37
41
  def method_missing(m, *args, &block)
38
- if v = attributes[m]
39
- v
42
+ if attributes.has_key?(m)
43
+ attributes[m]
40
44
  else
41
45
  raise UnknownAttributeError
42
46
  end
@@ -64,5 +68,11 @@ module EasyApi
64
68
  def schema
65
69
  {}
66
70
  end
71
+
72
+ private
73
+
74
+ def empty_optional_attributes
75
+ optional_attribute_names.map { |attr| [attr, nil] }.to_h
76
+ end
67
77
  end
68
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Meijer