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 +4 -4
- data/README.md +42 -1
- data/lib/easy_api.rb +1 -1
- data/lib/easy_api/object.rb +14 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc2ec38871b0d6b22e7b384f277d39353334de01
|
4
|
+
data.tar.gz: fad14f7aa23dcf1321fa6e07eeeeacd756d05357
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
data/lib/easy_api/object.rb
CHANGED
@@ -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
|
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
|
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
|
39
|
-
|
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
|