Class: SocialNetsDB::SocialNet
- Inherits:
-
Object
- Object
- SocialNetsDB::SocialNet
- Extended by:
- FnValidations, Support
- Includes:
- FnValidations, Support
- Defined in:
- lib/social_nets_db/social_net.rb
Instance Attribute Summary collapse
-
#uid ⇒ Object
Returns the value of attribute uid.
Class Method Summary collapse
-
.all ⇒ Array<SocialNetsDB::SocialNet>
TODO this must be transofrmed into array of structs.
- .find_by(name: nil, uid: nil) ⇒ SocialNetsDB::SocialNet?
- .find_by_name(name) ⇒ SocialNetsDB::SocialNet?
- .find_by_uid(uid) ⇒ SocialNetsDB::SocialNet? (also: find)
- .names ⇒ Object
- .uids ⇒ Object
- .values_for_select ⇒ Object
Instance Method Summary collapse
-
#initialize(uid, data) ⇒ SocialNet
constructor
A new instance of SocialNet.
-
#to_h ⇒ Hash
Raw data we have on the initialized social net.
-
#url ⇒ String
Full URL of the social net.
-
#user_page(username: nil, account_id: nil) ⇒ String
Full URL of user’s page in the social net.
-
#user_page_methods ⇒ Array
Available methods for bilding user page URL.
Methods included from Support
Methods included from FnValidations
validate_argument_boolean!, validate_argument_positive!, validate_argument_presence!, validate_argument_type!, validate_collection_item_types!
Constructor Details
#initialize(uid, data) ⇒ SocialNet
Returns a new instance of SocialNet.
15 16 17 18 19 |
# File 'lib/social_nets_db/social_net.rb', line 15 def initialize(uid, data) validate_argument_type! data, Hash validate_argument_type! uid, [String, Symbol] @data, @uid = data, uid end |
Instance Attribute Details
#uid ⇒ Object
Returns the value of attribute uid.
21 22 23 |
# File 'lib/social_nets_db/social_net.rb', line 21 def uid @uid end |
Class Method Details
.all ⇒ Array<SocialNetsDB::SocialNet>
TODO this must be transofrmed into array of structs
74 75 76 |
# File 'lib/social_nets_db/social_net.rb', line 74 def all RECORDS.map { |uid, data| new(uid, data) } end |
.find_by(name: nil, uid: nil) ⇒ SocialNetsDB::SocialNet?
83 84 85 86 87 |
# File 'lib/social_nets_db/social_net.rb', line 83 def find_by(name: nil, uid: nil) return find_by_uid(uid) if present_str?(uid) return find_by_name(name) if present_str?(name) fail ArgumentError, "`name:` or `uid:` must be provided. You are passing name: #{name.inspect}, uid: #{uid.inspect}" end |
.find_by_name(name) ⇒ SocialNetsDB::SocialNet?
93 94 95 96 97 |
# File 'lib/social_nets_db/social_net.rb', line 93 def find_by_name(name) validate_argument_presence! name return unless record = RECORDS.select { |uid, data| data["name"] == name }.first find_by_uid record[0] end |
.find_by_uid(uid) ⇒ SocialNetsDB::SocialNet? Also known as: find
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/social_nets_db/social_net.rb', line 103 def find_by_uid(uid) validate_argument_type! uid, String validate_argument_presence! uid # unless uids.include?(@uid = uid) # fail ArgumentError, "Social net with UID #{uid} is not recognized. Currently supported UIDs are: #{uids.join(", ")}. To add a new social net, send us a PR." # end return unless data = raw_data_for(uid) self.new(uid, data) end |
.names ⇒ Object
118 119 120 |
# File 'lib/social_nets_db/social_net.rb', line 118 def names RECORDS.map { |uid, data| data["name"] } end |
.uids ⇒ Object
122 123 124 |
# File 'lib/social_nets_db/social_net.rb', line 122 def uids RECORDS.keys.map(&:to_s) end |
.values_for_select ⇒ Object
126 127 128 |
# File 'lib/social_nets_db/social_net.rb', line 126 def values_for_select RECORDS.map { |uid, data| [data["name"], uid] } end |
Instance Method Details
#to_h ⇒ Hash
Returns Raw data we have on the initialized social net.
32 33 34 |
# File 'lib/social_nets_db/social_net.rb', line 32 def to_h self.class.send :raw_data_for, @uid end |
#url ⇒ String
Returns full URL of the social net.
38 39 40 |
# File 'lib/social_nets_db/social_net.rb', line 38 def url "https://#{domain}" end |
#user_page(username: nil, account_id: nil) ⇒ String
Returns full URL of user’s page in the social net.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/social_nets_db/social_net.rb', line 49 def user_page(username: nil, account_id: nil) return unless page = to_h["profile_url"] fail(ArgumentError, "Either a username or an account id must be provided") if [username, account_id].none? if username && page["by_username"] validate_argument_type! username, [String, Symbol, Integer] page["by_username"].sub("${domain}", domain.to_s).sub("${uid}", username.to_s) elsif account_id && page["by_account_id"] validate_argument_type! account_id, [String, Symbol, Integer] page["by_account_id"].sub("${domain}", domain.to_s).sub("${uid}", account_id.to_s) end end |
#user_page_methods ⇒ Array
Returns available methods for bilding user page URL.
63 64 65 |
# File 'lib/social_nets_db/social_net.rb', line 63 def user_page_methods ["account_id", "username"].select { |key| present_str? to_h.dig("profile_url", "by_#{key}") } end |