social_nets_db 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/lib/social_nets_db/social_net.rb +34 -11
- data/lib/social_nets_db/support.rb +0 -20
- data/lib/social_nets_db/version.rb +1 -1
- data/lib/social_nets_db.rb +0 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16056e32bc256d743a1b7fa14d16d714d242ee3a3d4ee106d71d51e6bed03c58
|
4
|
+
data.tar.gz: e91524ea8abd15b0f24584645605398ffb6026039fa082a240d04e719850f7df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72bdf3a625923ec900dcc76d98dd2e01932848b003b51dfcc3fc0589484086a5a2bafb6f71c9a9120e33b410c180dc53837a681b79030879a01016d3d6bb3285
|
7
|
+
data.tar.gz: a2fa51dad61a848b70cc82e923ab19b4c41b435487cff99f5620e4ff3c25e0f41d845513bfaffcd0df60ba60c7d7cd0246c36fc9979455846d620ce9fc4786a9
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -9,6 +9,9 @@ class SocialNetsDB
|
|
9
9
|
extend FnValidations
|
10
10
|
include FnValidations
|
11
11
|
|
12
|
+
# @param [String, Symbol] uid Social net UID (which must be among the top-level keys in db.yml)
|
13
|
+
# @param [Hash] data
|
14
|
+
#
|
12
15
|
def initialize(uid, data)
|
13
16
|
validate_argument_type! data, Hash
|
14
17
|
validate_argument_type! uid, [String, Symbol]
|
@@ -24,28 +27,39 @@ class SocialNetsDB
|
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
30
|
+
# @return [Hash] Raw data we have on the initialized social net
|
31
|
+
#
|
27
32
|
def to_h
|
28
33
|
self.class.send :raw_data_for, @uid
|
29
34
|
end
|
30
35
|
|
36
|
+
# @return [String] full URL of the social net
|
37
|
+
#
|
31
38
|
def url
|
32
39
|
"https://#{domain}"
|
33
40
|
end
|
34
41
|
|
42
|
+
# @return [String] full URL of user’s page in the social net
|
43
|
+
# @param [String, Symbol, Integer] username
|
44
|
+
# @param [String, Symbol, Integer] username
|
45
|
+
# @example
|
46
|
+
# SocialNetsDB::SocialNet.find("facebook").user_page(username: "dhh")
|
47
|
+
# #=> "https://facebook.com/dhh"
|
48
|
+
#
|
35
49
|
def user_page(username: nil, account_id: nil)
|
36
50
|
return unless page = to_h["profile_url"]
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
template.sub("${domain}", domain.to_s).sub("${uid}", account_id)
|
46
|
-
end
|
51
|
+
fail(ArgumentError, "Either a username or an account id must be provided") if [username, account_id].none?
|
52
|
+
if username && page["by_username"]
|
53
|
+
validate_argument_type! username, [String, Symbol, Integer]
|
54
|
+
page["by_username"].sub("${domain}", domain.to_s).sub("${uid}", username.to_s)
|
55
|
+
elsif account_id && page["by_account_id"]
|
56
|
+
validate_argument_type! account_id, [String, Symbol, Integer]
|
57
|
+
page["by_account_id"].sub("${domain}", domain.to_s).sub("${uid}", account_id.to_s)
|
58
|
+
end
|
47
59
|
end
|
48
60
|
|
61
|
+
# @return [Array] available methods for bilding user page URL
|
62
|
+
#
|
49
63
|
def user_page_methods
|
50
64
|
["account_id", "username"].select { |key| present_str? to_h.dig("profile_url", "by_#{key}") }
|
51
65
|
end
|
@@ -56,12 +70,15 @@ class SocialNetsDB
|
|
56
70
|
class << self
|
57
71
|
|
58
72
|
# TODO this must be transofrmed into array of structs
|
73
|
+
# @return [Array<SocialNetsDB::SocialNet>] a list of all social nets initialized as objects
|
59
74
|
def all
|
60
|
-
RECORDS
|
75
|
+
RECORDS.map { |uid, data| new(uid, data) }
|
61
76
|
end
|
62
77
|
|
78
|
+
|
63
79
|
# @param [String] name Social network name
|
64
80
|
# @param [String, Symbol] uid Social network UID
|
81
|
+
# @return [SocialNetsDB::SocialNet, nil]
|
65
82
|
#
|
66
83
|
def find_by(name: nil, uid: nil)
|
67
84
|
return find_by_uid(uid) if present_str?(uid)
|
@@ -69,13 +86,19 @@ class SocialNetsDB
|
|
69
86
|
fail ArgumentError, "`name:` or `uid:` must be provided. You are passing name: #{name.inspect}, uid: #{uid.inspect}"
|
70
87
|
end
|
71
88
|
|
89
|
+
|
90
|
+
# @param [String] name Social network name
|
91
|
+
# @return [SocialNetsDB::SocialNet, nil]
|
92
|
+
#
|
72
93
|
def find_by_name(name)
|
73
94
|
validate_argument_presence! name
|
74
95
|
return unless record = RECORDS.select { |uid, data| data["name"] == name }.first
|
75
96
|
find_by_uid record[0]
|
76
97
|
end
|
77
98
|
|
99
|
+
|
78
100
|
# @param [String, Symbol] uid Social network UID
|
101
|
+
# @return [SocialNetsDB::SocialNet, nil]
|
79
102
|
#
|
80
103
|
def find_by_uid(uid)
|
81
104
|
validate_argument_type! uid, String
|
@@ -13,24 +13,4 @@ module Support
|
|
13
13
|
arg.is_a?(String) && arg != ""
|
14
14
|
end
|
15
15
|
|
16
|
-
# From https://avdi.codes/recursively-symbolize-keys/
|
17
|
-
#
|
18
|
-
def recursively_symbolize_keys(hash)
|
19
|
-
validate_argument_type! hash, Hash
|
20
|
-
hash.inject({}) { |result, (key, value)|
|
21
|
-
new_key = case key
|
22
|
-
when String then key.to_sym
|
23
|
-
else key
|
24
|
-
end
|
25
|
-
|
26
|
-
new_value = case value
|
27
|
-
when Hash then symbolize_keys(value)
|
28
|
-
else value
|
29
|
-
end
|
30
|
-
|
31
|
-
result[new_key] = new_value
|
32
|
-
result
|
33
|
-
}
|
34
|
-
end
|
35
|
-
|
36
16
|
end
|
data/lib/social_nets_db.rb
CHANGED
@@ -10,8 +10,6 @@ require_relative "social_nets_db/social_net"
|
|
10
10
|
|
11
11
|
class SocialNetsDB
|
12
12
|
|
13
|
-
# RECORDS = recursively_symbolize_keys(YAML.load_file(File.expand_path("social_nets_db/db.yml", __dir__))).freeze
|
14
|
-
# RECORDS = YAML.load_file(File.expand_path("social_nets_db/db.yml", __dir__)).map { _1.transform_keys(&:to_sym) }.freeze
|
15
13
|
RECORDS = YAML.load_file(File.expand_path("social_nets_db/db.yml", __dir__)).freeze
|
16
14
|
|
17
15
|
end
|