Class: RemoteCopy

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

Instance Method Summary collapse

Instance Method Details

#create(channel) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/rcp.rb', line 8

def create(channel)
  uri = URI.parse(base_remote)

  http    = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)

  File.open(File.join(ENV['HOME'], '.rcp'), 'w') { |f| f.write(channel) }
end

#pullObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rcp.rb', line 37

def pull
  channel = File.read(File.join(ENV['HOME'], '.rcp')) || 'global'

  uri           = URI.parse(base_remote + "/pop?channel=#{channel}")

  response      = Net::HTTP.get_response(uri)
  response_json = JSON.parse(response.body)

  @content = response_json["item"]["content"]

  prompt_question if urls.any?

  open_urls if urls.any? && should_i_open?

  copy_to_clipboard

  prompt_message
end

#push(content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rcp.rb', line 21

def push(content)
  @content = content

  uri = URI.parse(base_remote)

  http    = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)

  channel = File.read(File.join(ENV['HOME'], '.rcp')) || 'global'

  request.set_form_data({ "item[content]" => content,
                          "item[channel]" => channel })

  response = http.request(request)
end

#use(channel) ⇒ Object



17
18
19
# File 'lib/rcp.rb', line 17

def use(channel)
  File.open(File.join(ENV['HOME'], '.rcp'), 'w') { |f| f.write(channel) }
end