tweetkit 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/Gemfile.lock +1 -1
- data/bin/console +15 -0
- data/lib/tweetkit/response.rb +70 -24
- data/lib/tweetkit/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b07ebb4cb2a6e55ce6e360cc628b9ea213bc6f00b3b66a914272b178297a5d6
|
4
|
+
data.tar.gz: 3d931da262b922272d3547c0e7f0a0e49eebd540cc1a4c4389185c3dbc323dcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c4be49f6e7f30c633b52fce7a458cb0168a8f29d4e178effe289a5a2acd00ff6d2b4673b09e30434e33d0c422c18c83862a279042e22ea1c7b7d56d7d5866ed
|
7
|
+
data.tar.gz: a9dcd0fbac29cded9f8fe38712c1daa43c4945d0bfe07f96343385e4da3abd90958a7facfb551726c91de217ce0b500d36a6aa5dde301491ee9ef35fd973e893
|
data/Gemfile.lock
CHANGED
data/bin/console
CHANGED
@@ -11,4 +11,19 @@ require "tweetkit"
|
|
11
11
|
# Pry.start
|
12
12
|
|
13
13
|
require "irb"
|
14
|
+
|
15
|
+
def reload!
|
16
|
+
puts 'Reloading ...'
|
17
|
+
# Main project directory.
|
18
|
+
root_dir = File.expand_path('..', __dir__)
|
19
|
+
# Directories within the project that should be reloaded.
|
20
|
+
reload_dirs = %w{lib}
|
21
|
+
# Loop through and reload every file in all relevant project directories.
|
22
|
+
reload_dirs.each do |dir|
|
23
|
+
Dir.glob("#{root_dir}/#{dir}/**/*.rb").each { |f| load(f) }
|
24
|
+
end
|
25
|
+
# Return true when complete.
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
14
29
|
IRB.start(__FILE__)
|
data/lib/tweetkit/response.rb
CHANGED
@@ -1,33 +1,82 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'pry'
|
2
3
|
|
3
4
|
module Tweetkit
|
4
5
|
class Response
|
5
|
-
attr_accessor :
|
6
|
+
attr_accessor :resources, :meta, :original_response, :tweets
|
6
7
|
|
7
8
|
def initialize(response)
|
8
9
|
@original_response = response.body
|
9
10
|
parsed_response = JSON.parse(@original_response)
|
10
11
|
@tweets = Tweetkit::Response::Tweets.new(parsed_response)
|
11
12
|
@meta = Tweetkit::Response::Meta.new(@tweets.meta)
|
12
|
-
@
|
13
|
+
@resources = Tweetkit::Response::Resources.new(@tweets.resources)
|
13
14
|
end
|
14
15
|
|
15
|
-
class
|
16
|
+
class Resources
|
16
17
|
include Enumerable
|
17
18
|
|
18
|
-
|
19
|
+
VALID_RESOURCES = Set['users', 'tweets', 'media']
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
attr_accessor :resources
|
22
|
+
|
23
|
+
def initialize(resources)
|
24
|
+
@resources = resources
|
25
|
+
build_and_normalize_resources(resources) unless resources.nil?
|
22
26
|
end
|
23
27
|
|
24
|
-
def
|
25
|
-
|
26
|
-
|
28
|
+
def build_and_normalize_resources(resources)
|
29
|
+
resources.each_key do |resource_type|
|
30
|
+
normalized_resource = build_and_normalize_resource(@resources[resource_type], resource_type)
|
31
|
+
instance_variable_set(:"@#{resource_type}", normalized_resource)
|
32
|
+
self.class.define_method(resource_type) { instance_variable_get("@#{resource_type}") }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_and_normalize_resource(resource, resource_type)
|
37
|
+
Tweetkit::Response::Resources::Resource.new(resource, resource_type)
|
27
38
|
end
|
28
39
|
|
29
|
-
def
|
30
|
-
|
40
|
+
def method_missing(method, **args)
|
41
|
+
return nil if VALID_RESOURCES.include?(method.to_s)
|
42
|
+
|
43
|
+
super
|
44
|
+
end
|
45
|
+
|
46
|
+
def respond_to_missing?(method, *args)
|
47
|
+
VALID_RESOURCES.include?(method.to_s) || super
|
48
|
+
end
|
49
|
+
|
50
|
+
class Resource
|
51
|
+
include Enumerable
|
52
|
+
|
53
|
+
attr_accessor :normalized_resource, :original_resource
|
54
|
+
|
55
|
+
RESOURCE_NORMALIZATION_KEY = {
|
56
|
+
'users': 'id'
|
57
|
+
}.freeze
|
58
|
+
|
59
|
+
def initialize(resource, resource_type)
|
60
|
+
@original_resource = resource
|
61
|
+
@normalized_resource = {}
|
62
|
+
normalization_key = RESOURCE_NORMALIZATION_KEY[resource_type.to_sym]
|
63
|
+
resource.each do |data|
|
64
|
+
key = data[normalization_key]
|
65
|
+
@normalized_resource[key.to_i] = data
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def each(*args, &block)
|
70
|
+
@normalized_resource.each(*args, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
def each_data(*args, &block)
|
74
|
+
@normalized_resource.values.each(*args, &block)
|
75
|
+
end
|
76
|
+
|
77
|
+
def find(key)
|
78
|
+
@normalized_resource[key.to_i]
|
79
|
+
end
|
31
80
|
end
|
32
81
|
end
|
33
82
|
|
@@ -45,7 +94,7 @@ module Tweetkit
|
|
45
94
|
data.empty? ? super : data
|
46
95
|
end
|
47
96
|
|
48
|
-
def respond_to_missing?(method)
|
97
|
+
def respond_to_missing?(method, *args)
|
49
98
|
meta.respond_to? method
|
50
99
|
end
|
51
100
|
end
|
@@ -53,12 +102,12 @@ module Tweetkit
|
|
53
102
|
class Tweets
|
54
103
|
include Enumerable
|
55
104
|
|
56
|
-
attr_accessor :tweets, :meta, :
|
105
|
+
attr_accessor :tweets, :meta, :resources
|
57
106
|
|
58
107
|
def initialize(response)
|
59
|
-
@tweets = response['data'].collect { |tweet| Tweetkit::Response::Tweet.new(tweet) }
|
108
|
+
@tweets = response['data'] ? response['data'].collect { |tweet| Tweetkit::Response::Tweet.new(tweet) } : []
|
60
109
|
@meta = response['meta']
|
61
|
-
@
|
110
|
+
@resources = response['includes']
|
62
111
|
end
|
63
112
|
|
64
113
|
def each(*args, &block)
|
@@ -73,15 +122,12 @@ module Tweetkit
|
|
73
122
|
@tweets.join(' ')
|
74
123
|
end
|
75
124
|
|
76
|
-
def method_missing(
|
77
|
-
|
78
|
-
super unless result
|
79
|
-
rescue StandardError
|
80
|
-
super
|
125
|
+
def method_missing(method, **args)
|
126
|
+
tweets.public_send(method, **args)
|
81
127
|
end
|
82
128
|
|
83
|
-
def respond_to_missing?(method)
|
84
|
-
tweets.respond_to?
|
129
|
+
def respond_to_missing?(method, *args)
|
130
|
+
tweets.respond_to?(method)
|
85
131
|
end
|
86
132
|
end
|
87
133
|
|
@@ -97,8 +143,8 @@ module Tweetkit
|
|
97
143
|
data.empty? ? super : data
|
98
144
|
end
|
99
145
|
|
100
|
-
def respond_to_missing?(method)
|
101
|
-
tweet.respond_to?
|
146
|
+
def respond_to_missing?(method, *args)
|
147
|
+
tweet.respond_to?(method) || super
|
102
148
|
end
|
103
149
|
end
|
104
150
|
end
|
data/lib/tweetkit/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tweetkit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julian Foo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|