Class: PutsReq::CLIHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/putsreq/cli_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, to, local) ⇒ CLIHelper

Returns a new instance of CLIHelper.



5
6
7
8
9
# File 'lib/putsreq/cli_helper.rb', line 5

def initialize(token, to, local)
  @token = token
  @to = to
  @local = local
end

Instance Attribute Details

#localObject (readonly)

Returns the value of attribute local.



3
4
5
# File 'lib/putsreq/cli_helper.rb', line 3

def local
  @local
end

#toObject (readonly)

Returns the value of attribute to.



3
4
5
# File 'lib/putsreq/cli_helper.rb', line 3

def to
  @to
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/putsreq/cli_helper.rb', line 3

def token
  @token
end

Class Method Details

.parse_token(token) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/putsreq/cli_helper.rb', line 50

def parse_token(token)
  if token.start_with? 'http'
    # from http://putsreq.com/token or http://putsreq.com/token/inspect
    # to token
    uri = URI(token)
    return uri.path.split('/')[1]
  end

  token
end

.valid_to?(to) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/putsreq/cli_helper.rb', line 61

def valid_to?(to)
  url = URI.parse(to) rescue nil
  url&.kind_of?(URI::HTTP) || url&.kind_of?(URI::HTTPS)
end

Instance Method Details

#find_and_forward(id) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/putsreq/cli_helper.rb', line 41

def find_and_forward(id)
  if request = find_request(id)
    forward_request(request)
  else
    puts "Request #{id} not found"
  end
end

#subscribe_and_forwardObject



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
# File 'lib/putsreq/cli_helper.rb', line 11

def subscribe_and_forward
  puts "Listening requests from #{token}"
  puts "Forwarding to #{to}"
  puts 'Press CTRL+c to terminate'

  last_request_id = find_last_request_id

  loop do
    url = "#{base_url}#{token}/requests.json?last_request_id=#{last_request_id}"

    response = get(url)

    unless response.ok?
      puts "Could not retrieve Bucket #{token}"
      break
    end

    parsed_response = response.parsed_response

    parsed_response.each do |request|
      forward_request(request)
      last_request_id = request.dig('_id', '$oid')
    end

    timeout = parsed_response.none? ? 5 : 2.5

    sleep timeout
  end
end