|
1 | 1 | require 'json' |
2 | 2 | require 'pry' |
3 | | -require 'tweetkit/connection' |
4 | 3 |
|
5 | 4 | module Tweetkit |
6 | | - class Response |
7 | | - attr_accessor :resources, :meta, :original_response, :tweets |
8 | | - |
9 | | - def initialize(response) |
10 | | - @original_response = response.body |
11 | | - parsed_response = JSON.parse(@original_response) |
12 | | - @tweets = Tweetkit::Response::Tweets.new(parsed_response) |
13 | | - @meta = Tweetkit::Response::Meta.new(@tweets.meta) |
14 | | - @next_token = @meta.next_token |
15 | | - @prev_token = @meta.prev_token |
16 | | - @resources = Tweetkit::Response::Resources.new(@tweets.resources) |
17 | | - end |
18 | | - |
19 | | - def next |
20 | | - binding.pry |
21 | | - @meta.next_token |
22 | | - end |
23 | | - |
24 | | - def prev |
25 | | - @meta.prev_token |
26 | | - end |
27 | | - |
28 | | - class Resources |
| 5 | + module Response |
| 6 | + class Tweets |
29 | 7 | include Enumerable |
30 | 8 |
|
31 | | - VALID_RESOURCES = Set['users', 'tweets', 'media'] |
32 | | - |
33 | | - attr_accessor :resources |
| 9 | + attr_accessor :meta, :original_response, :resources, :tweets |
34 | 10 |
|
35 | | - def initialize(resources) |
36 | | - @resources = resources |
37 | | - build_and_normalize_resources(resources) unless resources.nil? |
| 11 | + def initialize(response) |
| 12 | + @original_response = response.body |
| 13 | + response = JSON.parse(@original_response) |
| 14 | + @tweets = response['data'] ? response['data'].collect { |tweet| Tweetkit::Response::Tweets::Tweet.new(tweet) } : [] |
| 15 | + @meta = Tweetkit::Response::Tweets::Meta.new(response['meta']) |
| 16 | + @resources = response['includes'] |
38 | 17 | end |
39 | 18 |
|
40 | | - def build_and_normalize_resources(resources) |
41 | | - resources.each_key do |resource_type| |
42 | | - normalized_resource = build_and_normalize_resource(@resources[resource_type], resource_type) |
43 | | - instance_variable_set(:"@#{resource_type}", normalized_resource) |
44 | | - self.class.define_method(resource_type) { instance_variable_get("@#{resource_type}") } |
45 | | - end |
| 19 | + def each(*args, &block) |
| 20 | + tweets.each(*args, &block) |
46 | 21 | end |
47 | 22 |
|
48 | | - def build_and_normalize_resource(resource, resource_type) |
49 | | - Tweetkit::Response::Resources::Resource.new(resource, resource_type) |
| 23 | + def last |
| 24 | + tweets.last |
50 | 25 | end |
51 | 26 |
|
52 | | - def method_missing(method, **args) |
53 | | - return nil if VALID_RESOURCES.include?(method.to_s) |
54 | | - |
55 | | - super |
| 27 | + def next |
56 | 28 | end |
57 | 29 |
|
58 | | - def respond_to_missing?(method, *args) |
59 | | - VALID_RESOURCES.include?(method.to_s) || super |
| 30 | + def prev |
60 | 31 | end |
61 | 32 |
|
62 | | - class Resource |
63 | | - include Enumerable |
| 33 | + # def method_missing(method, **args) |
| 34 | + # tweets.public_send(method, **args) |
| 35 | + # end |
64 | 36 |
|
65 | | - attr_accessor :normalized_resource, :original_resource |
| 37 | + # def respond_to_missing?(method, *args) |
| 38 | + # tweets.respond_to?(method) |
| 39 | + # end |
66 | 40 |
|
67 | | - RESOURCE_NORMALIZATION_KEY = { |
68 | | - 'users': 'id' |
69 | | - }.freeze |
| 41 | + class Tweet |
| 42 | + attr_accessor :data |
70 | 43 |
|
71 | | - def initialize(resource, resource_type) |
72 | | - @original_resource = resource |
73 | | - @normalized_resource = {} |
74 | | - normalization_key = RESOURCE_NORMALIZATION_KEY[resource_type.to_sym] |
75 | | - resource.each do |data| |
76 | | - key = data[normalization_key] |
77 | | - @normalized_resource[key.to_i] = data |
78 | | - end |
| 44 | + def initialize(tweet) |
| 45 | + @data = tweet |
79 | 46 | end |
80 | 47 |
|
81 | | - def each(*args, &block) |
82 | | - @normalized_resource.each(*args, &block) |
| 48 | + def id |
| 49 | + data['id'] |
83 | 50 | end |
84 | 51 |
|
85 | | - def each_data(*args, &block) |
86 | | - @normalized_resource.values.each(*args, &block) |
| 52 | + def text |
| 53 | + data['text'] |
87 | 54 | end |
88 | 55 |
|
89 | | - def find(key) |
90 | | - @normalized_resource[key.to_i] |
91 | | - end |
| 56 | + # def method_missing(attribute) |
| 57 | + # data = tweet[attribute.to_s] |
| 58 | + # data.empty? ? super : data |
| 59 | + # end |
| 60 | + |
| 61 | + # def respond_to_missing?(method, *args) |
| 62 | + # tweet.respond_to?(method) || super |
| 63 | + # end |
92 | 64 | end |
93 | | - end |
94 | 65 |
|
95 | | - class Meta |
96 | | - include Enumerable |
| 66 | + class Resources |
| 67 | + include Enumerable |
97 | 68 |
|
98 | | - attr_accessor :meta, :next_token, :prev_token |
| 69 | + VALID_RESOURCES = Set['users', 'tweets', 'media'] |
99 | 70 |
|
100 | | - def initialize(meta) |
101 | | - @meta = meta |
102 | | - @next_token = @meta['next_token'] |
103 | | - @prev_token = @meta['prev_token'] |
104 | | - end |
| 71 | + attr_accessor :resources |
105 | 72 |
|
106 | | - def method_missing(attribute, **args) |
107 | | - data = meta[attribute.to_s] |
108 | | - data.empty? ? super : data |
109 | | - end |
| 73 | + def initialize(resources) |
| 74 | + @resources = resources |
| 75 | + build_and_normalize_resources(resources) unless resources.nil? |
| 76 | + end |
110 | 77 |
|
111 | | - def respond_to_missing?(method, *args) |
112 | | - meta.respond_to? method |
113 | | - end |
114 | | - end |
| 78 | + def build_and_normalize_resources(resources) |
| 79 | + resources.each_key do |resource_type| |
| 80 | + normalized_resource = build_and_normalize_resource(@resources[resource_type], resource_type) |
| 81 | + instance_variable_set(:"@#{resource_type}", normalized_resource) |
| 82 | + self.class.define_method(resource_type) { instance_variable_get("@#{resource_type}") } |
| 83 | + end |
| 84 | + end |
115 | 85 |
|
116 | | - class Tweets |
117 | | - include Enumerable |
| 86 | + def build_and_normalize_resource(resource, resource_type) |
| 87 | + Tweetkit::Response::Resources::Resource.new(resource, resource_type) |
| 88 | + end |
118 | 89 |
|
119 | | - attr_accessor :tweets, :meta, :resources |
| 90 | + def method_missing(method, **args) |
| 91 | + return nil if VALID_RESOURCES.include?(method.to_s) |
120 | 92 |
|
121 | | - def initialize(response) |
122 | | - @tweets = response['data'] ? response['data'].collect { |tweet| Tweetkit::Response::Tweet.new(tweet) } : [] |
123 | | - @meta = response['meta'] |
124 | | - @resources = response['includes'] |
125 | | - end |
| 93 | + super |
| 94 | + end |
126 | 95 |
|
127 | | - def each(*args, &block) |
128 | | - tweets.each(*args, &block) |
129 | | - end |
| 96 | + def respond_to_missing?(method, *args) |
| 97 | + VALID_RESOURCES.include?(method.to_s) || super |
| 98 | + end |
130 | 99 |
|
131 | | - def last |
132 | | - tweets.last |
133 | | - end |
| 100 | + class Resource |
| 101 | + include Enumerable |
134 | 102 |
|
135 | | - def to_s |
136 | | - @tweets.join(' ') |
137 | | - end |
| 103 | + attr_accessor :normalized_resource, :original_resource |
138 | 104 |
|
139 | | - def method_missing(method, **args) |
140 | | - tweets.public_send(method, **args) |
141 | | - end |
| 105 | + RESOURCE_NORMALIZATION_KEY = { |
| 106 | + 'users': 'id' |
| 107 | + }.freeze |
142 | 108 |
|
143 | | - def respond_to_missing?(method, *args) |
144 | | - tweets.respond_to?(method) |
145 | | - end |
146 | | - end |
| 109 | + def initialize(resource, resource_type) |
| 110 | + @original_resource = resource |
| 111 | + @normalized_resource = {} |
| 112 | + normalization_key = RESOURCE_NORMALIZATION_KEY[resource_type.to_sym] |
| 113 | + resource.each do |data| |
| 114 | + key = data[normalization_key] |
| 115 | + @normalized_resource[key.to_i] = data |
| 116 | + end |
| 117 | + end |
147 | 118 |
|
148 | | - class Tweet |
149 | | - attr_accessor :tweet |
| 119 | + def each(*args, &block) |
| 120 | + @normalized_resource.each(*args, &block) |
| 121 | + end |
150 | 122 |
|
151 | | - def initialize(tweet) |
152 | | - @tweet = tweet |
153 | | - end |
| 123 | + def each_data(*args, &block) |
| 124 | + @normalized_resource.values.each(*args, &block) |
| 125 | + end |
154 | 126 |
|
155 | | - def method_missing(attribute) |
156 | | - data = tweet[attribute.to_s] |
157 | | - data.empty? ? super : data |
| 127 | + def find(key) |
| 128 | + @normalized_resource[key.to_i] |
| 129 | + end |
| 130 | + end |
158 | 131 | end |
159 | 132 |
|
160 | | - def respond_to_missing?(method, *args) |
161 | | - tweet.respond_to?(method) || super |
| 133 | + class Meta |
| 134 | + attr_accessor :data |
| 135 | + |
| 136 | + def initialize(meta) |
| 137 | + @data = meta |
| 138 | + end |
| 139 | + |
| 140 | + def next_token |
| 141 | + @data['next_token'] |
| 142 | + end |
| 143 | + |
| 144 | + def prev_token |
| 145 | + @data['prev_token'] |
| 146 | + end |
| 147 | + |
| 148 | + # def method_missing(attribute, **args) |
| 149 | + # data = meta[attribute.to_s] |
| 150 | + # data.empty? ? super : data |
| 151 | + # end |
| 152 | + |
| 153 | + # def respond_to_missing?(method, *args) |
| 154 | + # meta.respond_to? method |
| 155 | + # end |
162 | 156 | end |
163 | 157 | end |
164 | 158 | end |
|
0 commit comments