social_nets_db 0.0.7 → 0.0.8
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/CHANGELOG.md +4 -0
- data/README.md +2 -2
- data/lib/social_nets_db/db.yml +1 -1
- data/lib/social_nets_db/fn_validations.rb +33 -33
- data/lib/social_nets_db/social_net.rb +36 -36
- data/lib/social_nets_db/support.rb +9 -9
- data/lib/social_nets_db/tag_helper.rb +28 -28
- data/lib/social_nets_db/version.rb +1 -1
- data/lib/social_nets_db.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: 3b07bc3f5bac88fb0cd2995b67cb093d86e3c7f9c2305862485e92bf52f4d440
|
4
|
+
data.tar.gz: 013df8d860fdfc63cd909d26636d47c0ebf25de19d6a461e2d6e2021aaa371f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97b15945fa4e52ea9b26c8306765d50025891b157bb2c4318d150aee07a25af17c50e7168cca7952813da7e29f3c4c7d6599d11abcdb2885562d569802758e39
|
7
|
+
data.tar.gz: 276f11e57a87fc304e815b114a49a549df8e12b838ad28ea9b76e5d7b22cf32da055db98b2319862b4d5ce155874f6d9c3d2089f8501208457d3cab44c874fb0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -122,7 +122,7 @@ social_net = SocialNetsDB::SocialNet.find_by_uid("facebook")
|
|
122
122
|
Which are supported UIDs?
|
123
123
|
|
124
124
|
```ruby
|
125
|
-
SocialNetsDB.uids
|
125
|
+
SocialNetsDB::SocialNet.uids
|
126
126
|
#=> [
|
127
127
|
# "behance",
|
128
128
|
# "dribble",
|
@@ -253,7 +253,7 @@ model SocialNetAccount < ApplicationRecord
|
|
253
253
|
belongs_to :user
|
254
254
|
|
255
255
|
validates :account_id, presence: true, if: Proc.new { |record| record.username.blank? }
|
256
|
-
validates :social_net_uid, presence: true, inclusion: { in: SocialNetsDB.uids }
|
256
|
+
validates :social_net_uid, presence: true, inclusion: { in: SocialNetsDB::SocialNet.uids }
|
257
257
|
validates :username, presence: true, if: Proc.new { |record| record.account_id.blank? }
|
258
258
|
|
259
259
|
def social_net
|
data/lib/social_nets_db/db.yml
CHANGED
@@ -2,44 +2,44 @@
|
|
2
2
|
|
3
3
|
module FnValidations
|
4
4
|
|
5
|
-
|
5
|
+
module_function
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
def validate_argument_type! argument, permitted_types
|
8
|
+
types = Array(permitted_types)
|
9
|
+
type_names = types.map { |klass| "`#{klass}`" }.join(" or ")
|
10
|
+
valid = types.any? { |type| argument.is_a? type }
|
11
|
+
message = "#{type_names} is expected, you pass a `#{argument.class}` (#{argument})"
|
12
|
+
fail TypeError, message unless valid
|
13
|
+
argument
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
def validate_collection_item_types! collection, permitted_types
|
17
|
+
types = Array(permitted_types)
|
18
|
+
type_names = types.map { |name| "`#{name}`" }.join(" or ")
|
19
|
+
valid = collection.all? { |item| types.include?(item.class) }
|
20
|
+
message = "#{type_names} is expected, one the items you pass is of incorrect type"
|
21
|
+
fail TypeError, message unless valid
|
22
|
+
collection
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def validate_argument_boolean! argument
|
26
|
+
validate_argument_type! argument, [TrueClass, FalseClass]
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
29
|
+
def validate_argument_presence! argument
|
30
|
+
validate_argument_type! argument, [String, Symbol]
|
31
|
+
return argument if argument.is_a?(Symbol)
|
32
|
+
message = "You must pass a non-empty String, you are passing #{argument.inspect}"
|
33
|
+
fail ArgumentError, message unless argument.is_a?(String) && argument != ""
|
34
|
+
argument
|
35
|
+
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
def validate_argument_positive! argument
|
38
|
+
validate_argument_type! argument, Numeric
|
39
|
+
message = "You must pass a positive value, you pass #{argument.inspect}"
|
40
|
+
fail ArgumentError, message unless argument.positive?
|
41
|
+
argument
|
42
|
+
end
|
43
43
|
|
44
44
|
end
|
45
45
|
|
@@ -9,9 +9,9 @@ class SocialNetsDB
|
|
9
9
|
extend FnValidations
|
10
10
|
include FnValidations
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
# @param [String, Symbol] uid Social net UID (which must be among the top-level keys in db.yml)
|
13
|
+
# @param [Hash] data
|
14
|
+
#
|
15
15
|
def initialize(uid, data)
|
16
16
|
validate_argument_type! data, Hash
|
17
17
|
validate_argument_type! uid, [String, Symbol]
|
@@ -20,46 +20,46 @@ class SocialNetsDB
|
|
20
20
|
|
21
21
|
attr_accessor :uid
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
[:color, :domain, :icons, :name, :tags].each do |method_symbol|
|
24
|
+
define_method(method_symbol) do
|
25
|
+
fallback_value = method_symbol == :tags ? [] : nil
|
26
|
+
@data.fetch(method_symbol.to_s, fallback_value)
|
27
|
+
end
|
28
|
+
end
|
29
29
|
|
30
|
-
|
31
|
-
|
30
|
+
# @return [Hash] Raw data we have on the initialized social net
|
31
|
+
#
|
32
32
|
def to_h
|
33
33
|
self.class.send :raw_data_for, @uid
|
34
34
|
end
|
35
35
|
|
36
|
-
|
37
|
-
|
36
|
+
# @return [String] full URL of the social net
|
37
|
+
#
|
38
38
|
def url
|
39
39
|
"https://#{domain}"
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
+
#
|
49
49
|
def user_page(username: nil, account_id: nil)
|
50
50
|
return unless page = to_h["profile_url"]
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
59
59
|
end
|
60
60
|
|
61
|
-
|
62
|
-
|
61
|
+
# @return [Array] available methods for bilding user page URL
|
62
|
+
#
|
63
63
|
def user_page_methods
|
64
64
|
["account_id", "username"].select { |key| present_str? to_h.dig("profile_url", "by_#{key}") }
|
65
65
|
end
|
@@ -70,7 +70,7 @@ class SocialNetsDB
|
|
70
70
|
class << self
|
71
71
|
|
72
72
|
# TODO this must be transofrmed into array of structs
|
73
|
-
|
73
|
+
# @return [Array<SocialNetsDB::SocialNet>] a list of all social nets initialized as objects
|
74
74
|
def all
|
75
75
|
RECORDS.map { |uid, data| new(uid, data) }
|
76
76
|
end
|
@@ -78,7 +78,7 @@ class SocialNetsDB
|
|
78
78
|
|
79
79
|
# @param [String] name Social network name
|
80
80
|
# @param [String, Symbol] uid Social network UID
|
81
|
-
|
81
|
+
# @return [SocialNetsDB::SocialNet, nil]
|
82
82
|
#
|
83
83
|
def find_by(name: nil, uid: nil)
|
84
84
|
return find_by_uid(uid) if present_str?(uid)
|
@@ -87,9 +87,9 @@ class SocialNetsDB
|
|
87
87
|
end
|
88
88
|
|
89
89
|
|
90
|
-
|
91
|
-
|
92
|
-
|
90
|
+
# @param [String] name Social network name
|
91
|
+
# @return [SocialNetsDB::SocialNet, nil]
|
92
|
+
#
|
93
93
|
def find_by_name(name)
|
94
94
|
validate_argument_presence! name
|
95
95
|
return unless record = RECORDS.select { |uid, data| data["name"] == name }.first
|
@@ -98,7 +98,7 @@ class SocialNetsDB
|
|
98
98
|
|
99
99
|
|
100
100
|
# @param [String, Symbol] uid Social network UID
|
101
|
-
|
101
|
+
# @return [SocialNetsDB::SocialNet, nil]
|
102
102
|
#
|
103
103
|
def find_by_uid(uid)
|
104
104
|
validate_argument_type! uid, String
|
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
module Support
|
4
4
|
|
5
|
-
|
5
|
+
extend FnValidations
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
# Direct analogue of Rails' `String#present?`
|
8
|
+
#
|
9
|
+
# @return [Boolean] is the passed argument is a non-empty String
|
10
|
+
# @param arg [Any] the thing to check
|
11
|
+
#
|
12
|
+
def present_str?(arg)
|
13
|
+
arg.is_a?(String) && arg != ""
|
14
|
+
end
|
15
15
|
|
16
16
|
end
|
@@ -4,33 +4,33 @@ require_relative "support"
|
|
4
4
|
|
5
5
|
module TagHelper
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
7
|
+
include Support
|
8
|
+
|
9
|
+
def tag_attributes_to_s(options)
|
10
|
+
fail ArgumentError, "A Hash must be passed, you pass #{options.class}: #{options.inspect}" unless options.is_a? Hash
|
11
|
+
options.map { |pair| stringify_pair(*pair) }
|
12
|
+
.reject { |stringified_attribute| [nil, ""].include?(stringified_attribute) }
|
13
|
+
.join(" ")
|
14
|
+
end
|
15
|
+
|
16
|
+
private def stringify_pair(attribute_name, attribute_value)
|
17
|
+
str_value = stringify_value(attribute_value)
|
18
|
+
return "" if [nil, ""].include? str_value
|
19
|
+
"#{attribute_name}=\"#{str_value}\""
|
20
|
+
end
|
21
|
+
|
22
|
+
private def stringify_value(value)
|
23
|
+
value.is_a?(Array) ? value.join(" ") : value.to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
def merge_style_values(existing:, incoming:)
|
27
|
+
return existing unless present_str? incoming
|
28
|
+
[existing, "#{incoming};"].join("; ").gsub(";;", ";")
|
29
|
+
end
|
30
|
+
|
31
|
+
def merge_class_values(existing:, incoming:)
|
32
|
+
return existing unless present_str? incoming
|
33
|
+
[existing, incoming]
|
34
|
+
end
|
35
35
|
|
36
36
|
end
|
data/lib/social_nets_db.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: social_nets_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Pedan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|