-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
profile.rb
95 lines (81 loc) · 3.43 KB
/
profile.rb
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'addressable/uri'
require 'cgi'
require 'memoizable'
module Twitter
module Profile
PROFILE_IMAGE_SUFFIX_REGEX = /_normal(\.gif|\.jpe?g|\.png)$/i.freeze
PREDICATE_URI_METHOD_REGEX = /_uri\?$/.freeze
include Memoizable
class << self
private
def alias_predicate_uri_methods(method)
%w[_url? _uri_https? _url_https?].each do |replacement|
alias_method_sub(method, PREDICATE_URI_METHOD_REGEX, replacement)
end
end
def alias_method_sub(method, pattern, replacement)
alias_method(method.to_s.sub(pattern, replacement).to_sym, method)
end
end
# Return the URL to the user's profile banner image
#
# @param size [String, Symbol] The size of the image. Must be one of: 'mobile', 'mobile_retina', 'web', 'web_retina', 'ipad', or 'ipad_retina'
# @return [Addressable::URI]
def profile_banner_uri(size = :web)
parse_uri(insecure_uri([@attrs[:profile_banner_url], size].join('/'))) unless @attrs[:profile_banner_url].nil?
end
alias profile_banner_url profile_banner_uri
# Return the secure URL to the user's profile banner image
#
# @param size [String, Symbol] The size of the image. Must be one of: 'mobile', 'mobile_retina', 'web', 'web_retina', 'ipad', or 'ipad_retina'
# @return [Addressable::URI]
def profile_banner_uri_https(size = :web)
parse_uri([@attrs[:profile_banner_url], size].join('/')) unless @attrs[:profile_banner_url].nil?
end
alias profile_banner_url_https profile_banner_uri_https
# @return [Boolean]
def profile_banner_uri?
!!@attrs[:profile_banner_url]
end
memoize :profile_banner_uri?
alias_predicate_uri_methods :profile_banner_uri?
# Return the URL to the user's profile image
#
# @param size [String, Symbol] The size of the image. Must be one of: 'mini', 'normal', 'bigger' or 'original'
# @return [Addressable::URI]
def profile_image_uri(size = :normal)
parse_uri(insecure_uri(profile_image_uri_https(size))) unless @attrs[:profile_image_url_https].nil?
end
alias profile_image_url profile_image_uri
# Return the secure URL to the user's profile image
#
# @param size [String, Symbol] The size of the image. Must be one of: 'mini', 'normal', 'bigger' or 'original'
# @return [Addressable::URI]
def profile_image_uri_https(size = :normal)
# The profile image URL comes in looking like like this:
# https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png
# It can be converted to any of the following sizes:
# https://a0.twimg.com/profile_images/1759857427/image1326743606.png
# https://a0.twimg.com/profile_images/1759857427/image1326743606_mini.png
# https://a0.twimg.com/profile_images/1759857427/image1326743606_bigger.png
parse_uri(@attrs[:profile_image_url_https].sub(PROFILE_IMAGE_SUFFIX_REGEX, profile_image_suffix(size))) unless @attrs[:profile_image_url_https].nil?
end
alias profile_image_url_https profile_image_uri_https
# @return [Boolean]
def profile_image_uri?
!!@attrs[:profile_image_url_https]
end
memoize :profile_image_uri?
alias_predicate_uri_methods :profile_image_uri?
private
def parse_uri(uri)
Addressable::URI.parse(uri)
end
def insecure_uri(uri)
uri.to_s.sub(/^https/i, 'http')
end
def profile_image_suffix(size)
size.to_sym == :original ? '\\1' : "_#{size}\\1"
end
end
end