Class: LL::WK::API::Connection::Curb

Inherits:
Base
  • Object
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

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/ll/wk/api/connection/curb.rb', line 8

def config
  @config
end

#emailObject

Returns the value of attribute email.



8
9
10
# File 'lib/ll/wk/api/connection/curb.rb', line 8

def email
  @email
end

#endpointObject

Returns the value of attribute endpoint.



8
9
10
# File 'lib/ll/wk/api/connection/curb.rb', line 8

def endpoint
  @endpoint
end

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/ll/wk/api/connection/curb.rb', line 8

def params
  @params
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/ll/wk/api/connection/curb.rb', line 8

def token
  @token
end

#token_issuedObject

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.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

Instance Method Details

#authObject



11
12
13
# File 'lib/ll/wk/api/connection/curb.rb', line 11

def auth
  "Token token=\"#{@token}\", email=\"#{@email}\""
end

#do_pagination(end_point, params, pages) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# 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) ⇒ 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)
  # 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_tokenObject



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.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) ⇒ 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

Returns:

  • (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_payloadObject



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

Yields:

  • (out)


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