class LL::WK::API::Connection::Curb
Attributes
config[RW]
email[RW]
endpoint[RW]
params[RW]
token[RW]
token_issued[RW]
Public Class Methods
autenticate(email, password, url)
click to toggle source
# 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.headers['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
new(url:, email:, password:)
click to toggle source
# 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
Public Instance Methods
auth()
click to toggle source
# File lib/ll/wk/api/connection/curb.rb, line 11 def auth "Token token=\"#{@token}\", email=\"#{@email}\"" end
curl_easy(uri) { |c| ... }
click to toggle source
# File lib/ll/wk/api/connection/curb.rb, line 15 def curl_easy(uri) Curl::Easy.new(uri) do |c| c.follow_location = true c.headers['Authorization'] = auth unless token_expired? yield(c) if block_given? end end
do_pagination(end_point, params, pages)
click to toggle source
# File lib/ll/wk/api/connection/curb.rb, line 93 def do_pagination(end_point, params, pages) # grabs data from each of the pages returned by the API 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)
click to toggle source
# File lib/ll/wk/api/connection/curb.rb, line 69 def from_api(end_point, params) # gather data from the api 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 << do_pagination(end_point, params, page_count(out)) unless page_count(out).zero? end end array.flatten! array rescue StandardError => e raise e end
generate_token()
click to toggle source
# File lib/ll/wk/api/connection/curb.rb, line 40 def generate_token Curl::Easy.new("#{@api_url}/session/new") do |curl| curl.headers['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)
click to toggle source
# File lib/ll/wk/api/connection/curb.rb, line 65 def page_count(resp) resp['paging']&.[]('total') end
response_from_api(endpoint, params) { |resp| ... }
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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?()
click to toggle source
# 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()
click to toggle source
# 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) { |d| ... }
click to toggle source
# 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| ... }
click to toggle source
# 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