Class: BitlyQuickly
- Inherits:
-
Object
show all
- Defined in:
- lib/bitly_quickly.rb,
lib/bitly_quickly/version.rb
Overview
Defined Under Namespace
Classes: BitlyError, InvalidRequestOrResponseError, NotFoundError, RateLimitExceededError, TemporarilyUnavailableError, UnknownError
Constant Summary
collapse
- DEFAULT_API_ADDRESS =
'https://api-ssl.bitly.com'
- OJ_OPTIONS =
{
mode: :compat,
symbol_keys: true,
time_format: :xmlschema,
second_precision: 0,
}
- VERSION =
'0.0.5'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of BitlyQuickly.
26
27
28
29
|
# File 'lib/bitly_quickly.rb', line 26
def initialize(options)
@access_token = options.delete(:access_token) || raise( ArgumentError.new('Missing access_token option') )
@api_address = options.delete(:api_address) || DEFAULT_API_ADDRESS
end
|
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
23
24
25
|
# File 'lib/bitly_quickly.rb', line 23
def access_token
@access_token
end
|
#api_address ⇒ Object
Returns the value of attribute api_address.
23
24
25
|
# File 'lib/bitly_quickly.rb', line 23
def api_address
@api_address
end
|
Instance Method Details
#api_url(path) ⇒ Object
63
64
65
|
# File 'lib/bitly_quickly.rb', line 63
def api_url(path)
URI.join(api_address, path).to_s
end
|
#get_many_responses(array_of_long_urls) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/bitly_quickly.rb', line 39
def get_many_responses(array_of_long_urls)
hydra = Typhoeus::Hydra.new
responses = {}
array_of_long_urls.each do |long_url|
request = make_shorten_request long_url
request.on_complete do |response|
json_response = response_to_json response
responses[ response.request.long_url ] = json_response[:data][:url]
end
hydra.queue request
end
hydra.run
responses
end
|
#get_response(long_url) ⇒ Object
87
88
89
90
91
|
# File 'lib/bitly_quickly.rb', line 87
def get_response(long_url)
request = make_shorten_request long_url
response = request.run
response_to_json response
end
|
#get_single_response(long_url) ⇒ Object
58
59
60
61
|
# File 'lib/bitly_quickly.rb', line 58
def get_single_response(long_url)
response = get_response long_url
response[:data][:url]
end
|
#make_shorten_request(long_url) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/bitly_quickly.rb', line 67
def make_shorten_request(long_url)
request = Typhoeus::Request.new(
api_url('/v3/shorten'),
{
params: {
access_token: access_token,
longUrl: long_url
},
}
)
class << request
attr_accessor :long_url
end
request.long_url = long_url
request
end
|
#parse_response(response) ⇒ Object
112
113
114
|
# File 'lib/bitly_quickly.rb', line 112
def parse_response(response)
Oj.load response.body, OJ_OPTIONS
end
|
#response_to_json(response) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/bitly_quickly.rb', line 93
def response_to_json(response)
json_response = parse_response response
case json_response[:status_code]
when 200
return json_response
when 403
raise RateLimitExceededError, json_response[:status_txt]
when 404
raise NotFoundError, json_response[:status_txt]
when 500
raise InvalidRequestOrResponseError, json_response[:status_txt]
when 503
raise TemporarilyUnavailableError, json_response[:status_txt]
else
raise UnknownError, json_response[:status_txt]
end
end
|
#shorten(long_url_or_array) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/bitly_quickly.rb', line 31
def shorten(long_url_or_array)
if long_url_or_array.respond_to? :each
get_many_responses long_url_or_array
else
get_single_response long_url_or_array
end
end
|