Class: Everhour2toggl::Toggl::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/everhour2toggl/toggl/post.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_token:, input:) ⇒ Post

Returns a new instance of Post.



7
8
9
10
# File 'lib/everhour2toggl/toggl/post.rb', line 7

def initialize(api_token:, input:)
  @api_token = api_token
  @input = input
end

Instance Method Details

#postObject



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

def post
  uri = URI.parse("https://www.toggl.com/api/v8/time_entries")
  request = Net::HTTP::Post.new(uri)
  request.basic_auth(@api_token, "api_token")
  request.content_type = "application/json"

  req_options = {
      use_ssl: uri.scheme == "https",
  }

  entries = JSON.parse(File.read(@input))
  responses = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    entries.each do |entry|
      request.body = JSON.dump(entry)
      http.request(request)
    end
  end
  # puts responses
end