Class: SocialNetsDB::SocialNet

Inherits:
Object
  • Object
show all
Extended by:
FnValidations, Support
Includes:
FnValidations, Support
Defined in:
lib/social_nets_db/social_net.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support

present_str?

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.

Parameters:

  • uid (String, Symbol)

    Social net UID (which must be among the top-level keys in db.yml)

  • data (Hash)


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

#uidObject

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

.allArray<SocialNetsDB::SocialNet>

TODO this must be transofrmed into array of structs

Returns:



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?

Parameters:

  • name (String) (defaults to: nil)

    Social network name

  • uid (String, Symbol) (defaults to: nil)

    Social network UID

Returns:



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?

Parameters:

  • name (String)

    Social network name

Returns:



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

Parameters:

  • uid (String, Symbol)

    Social network UID

Returns:



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

.namesObject



118
119
120
# File 'lib/social_nets_db/social_net.rb', line 118

def names
  RECORDS.map { |uid, data| data["name"] }
end

.uidsObject



122
123
124
# File 'lib/social_nets_db/social_net.rb', line 122

def uids
  RECORDS.keys.map(&:to_s)
end

.values_for_selectObject



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_hHash

Returns Raw data we have on the initialized social net.

Returns:

  • (Hash)

    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

#urlString

Returns full URL of the social net.

Returns:

  • (String)

    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.

Examples:

SocialNetsDB::SocialNet.find("facebook").user_page(username: "dhh")
#=> "https://facebook.com/dhh"

Parameters:

  • username (String, Symbol, Integer) (defaults to: nil)
  • username (String, Symbol, Integer) (defaults to: nil)

Returns:

  • (String)

    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, ].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  &&  page["by_account_id"]
      validate_argument_type! , [String, Symbol, Integer]
      page["by_account_id"].sub("${domain}", domain.to_s).sub("${uid}", .to_s)
  end
end

#user_page_methodsArray

Returns available methods for bilding user page URL.

Returns:

  • (Array)

    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