Class: LL::WK::API::Connection::Curb
- Inherits:
-
Base
- Object
- Base
- LL::WK::API::Connection::Curb
show all
- Defined in:
- lib/ll/wk/api/connection/curb.rb,
lib/ll/wk/api/connection/new_curb.rb
Instance Attribute Summary collapse
Attributes inherited from Base
#password, #url
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#authenticate!, authenticate_payload, #retrieve_data, trap_resp_code, #with_cursor, #with_page
Constructor Details
#initialize(url:, email:, password:) ⇒ Curb
Returns a new instance of Curb.
23
24
25
26
27
28
|
# File 'lib/ll/wk/api/connection/curb.rb', line 23
def initialize(url:, email:, password:)
@api_url = url
@email = email
@password = password
generate_token
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
8
9
10
|
# File 'lib/ll/wk/api/connection/curb.rb', line 8
def config
@config
end
|
#email ⇒ Object
Returns the value of attribute email.
8
9
10
|
# File 'lib/ll/wk/api/connection/curb.rb', line 8
def email
@email
end
|
#endpoint ⇒ Object
Returns the value of attribute endpoint.
8
9
10
|
# File 'lib/ll/wk/api/connection/curb.rb', line 8
def endpoint
@endpoint
end
|
#params ⇒ Object
Returns the value of attribute params.
8
9
10
|
# File 'lib/ll/wk/api/connection/curb.rb', line 8
def params
@params
end
|
#token ⇒ Object
Returns the value of attribute token.
8
9
10
|
# File 'lib/ll/wk/api/connection/curb.rb', line 8
def token
@token
end
|
#token_issued ⇒ Object
Returns the value of attribute token_issued.
8
9
10
|
# File 'lib/ll/wk/api/connection/curb.rb', line 8
def token_issued
@token_issued
end
|
Class Method Details
.autenticate(email, password, url) ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/ll/wk/api/connection/new_curb.rb', line 20
def self.autenticate(email, password, url)
curl_easy("#{url}/session/new") do |curl|
curl.['Content-Type'] = 'application/json'
curl.http_post(authenticate_payload(email, password))
resp = Json.parse(curl.body_str)
self.class.trap_resp_code(resp['status'])
return resp
end
end
|
Instance Method Details
#auth ⇒ Object
11
12
13
|
# File 'lib/ll/wk/api/connection/curb.rb', line 11
def auth
"Token token=\"#{@token}\", email=\"#{@email}\""
end
|
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/ll/wk/api/connection/curb.rb', line 93
def (end_point, params, pages)
results = []
(2..pages).each do |page|
curl_easy("#{@api_url}/#{end_point}#{params}&page=#{page}") do |curl|
with_get(curl) { |resp| with_each_page_data(resp) { |result| results << result } }
end
end
results
end
|
#from_api(end_point, params) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/ll/wk/api/connection/curb.rb', line 69
def from_api(end_point, params)
array = []
curl_easy("#{@api_url}/#{end_point}#{params}") do |curl|
with_get(curl) do |out|
with_each_page_data(out) do |result|
array << result
end
array << (end_point, params, page_count(out)) unless page_count(out).zero?
end
end
array.flatten!
array
rescue StandardError => e
raise e
end
|
#generate_token ⇒ Object
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/ll/wk/api/connection/curb.rb', line 40
def generate_token
Curl::Easy.new("#{@api_url}/session/new") do |curl|
curl.['Content-Type'] = 'application/json'
curl.http_post(token_payload)
resp = JSON.parse(curl.body_str)
raise 'Invalid' if resp['status'] && resp['status'] == 'unauthenticated'
@token_issued = Time.now
@token = resp['token']
end
end
|
#page_count(resp) ⇒ Object
65
66
67
|
# File 'lib/ll/wk/api/connection/curb.rb', line 65
def page_count(resp)
resp['paging']&.[]('total')
end
|
#response_from_api(endpoint, params) ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/ll/wk/api/connection/new_curb.rb', line 30
def response_from_api(endpoint, params)
curl_easy("#{url}/#{endpoint}?#{params.to_query}") do |curl|
curl.perform
resp = JSON.parse(curl.body_str)
self.class.trap_resp_code(curl.status)
yield(resp) if block_given?
return resp
end
end
|
#search_for_user_album_items(userid) ⇒ Object
55
56
57
|
# File 'lib/ll/wk/api/connection/curb.rb', line 55
def search_for_user_album_items(userid)
from_api('user_album_items?', "user_id=#{userid}")
end
|
#search_for_users(from_date = 0, to_date = Time.now.to_i) ⇒ Object
51
52
53
|
# File 'lib/ll/wk/api/connection/curb.rb', line 51
def search_for_users(from_date = 0, to_date = Time.now.to_i)
from_api('users?', "date_from=#{from_date}&date_to=#{to_date}")
end
|
#token_expired? ⇒ Boolean
34
35
36
37
38
|
# File 'lib/ll/wk/api/connection/curb.rb', line 34
def token_expired?
return true unless token
token_age = (Time.now - token_issued.to_i).to_i
true unless token_age < 76400
end
|
#token_payload ⇒ Object
30
31
32
|
# File 'lib/ll/wk/api/connection/curb.rb', line 30
def token_payload
{ user: { email: @email, password: @password } }.to_json
end
|
#with_each_page_data(resp) ⇒ Object
59
60
61
62
63
|
# File 'lib/ll/wk/api/connection/curb.rb', line 59
def with_each_page_data(resp)
resp['data'].each { |d| yield d }
rescue NoMethodError => e
raise e
end
|
#with_get(http) {|out| ... } ⇒ Object
86
87
88
89
90
91
|
# File 'lib/ll/wk/api/connection/curb.rb', line 86
def with_get(http)
http.perform
out = JSON.parse(http.body_str)
yield(out)
out
end
|