Class: TpCommandLine::ActivityTrack

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ActivityTrack

Returns a new instance of ActivityTrack.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tp_cli.rb', line 10

def initialize(args)
  @config_data = TpCommandLine::Config.new.load_config
  description = determine_description(args)
  @server_url = @config_data["timepulse_url"]
  @request_options = {
    method: :post,
    body: JSON.dump({
      activity: {
        description: description,
        project_id: @config_data['project_id'],
        source: "API",
        time: Time.now.utc
      }
    }),
    headers: {
      login: @config_data['login'],
      Authorization: @config_data['authorization'],
      'Accept-Encoding' => 'application/json',
      'Content-Type' => 'application/json'
       }
    }
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/tp_cli.rb', line 8

def request
  @request
end

Instance Method Details

#determine_description(args) ⇒ Object

determine user activity and user feedback or what to POST



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tp_cli.rb', line 34

def determine_description(args)
  if args[0] == "note" && args[1].is_a?(String)
    args[1]
  elsif args[0] == "cwd"
    directory = @config_data["directory_name"] || Dir.getwd
    "Changed working directory to #{directory}"
  else
    puts "\nPlease specify action: 'tp-cli note' or 'tp-cli cwd'"
    exit
  end
end

#handle_response(response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tp_cli.rb', line 58

def handle_response(response)
  case response.response_code
  when 201
    puts "\nThe activity was sent to TimePulse."
  when 401
    puts "\nThe TimePulse server was unable to authorize you. Check the API token in your timepulse.yml files."
  when 422
    puts "\nThere was an error saving to TimePulse. Please check the information in your timepulse.yml files."
    if response.body
      puts "Rails Errors:"
      errors = JSON.parse(response.body)
      errors.each { |k, v| v.each { |vsub| puts "#{k} #{vsub}"}}
    end
  when 500
    puts "\nThere was an internal server error while handling your request. Tell your TimePulse administrator to check their logs."
  when 0
    puts "\nPlease check your internet connection and that the project site is not currently offline.\nCurl Error: #{response.return_code}"
  else
    puts "\nTimePulse returned an unexpected response: #{response.response_code}"
  end
end

#send_to_timepulseObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tp_cli.rb', line 46

def send_to_timepulse
  @request = Typhoeus::Request.new(@server_url, @request_options)

  begin
    @request.run
  rescue StandardError => err
    puts "\n Please check your internet connection and that the project site is not currently offline."
  end

  handle_response(@request.response)
end