Class: PushbulletRuby::Pushable::File

Inherits:
PushbulletRuby::Pushable show all
Defined in:
lib/pushbullet_ruby/pushable/file.rb

Instance Attribute Summary

Attributes inherited from PushbulletRuby::Pushable

#client, #identifier, #params, #receiver

Instance Method Summary collapse

Methods inherited from PushbulletRuby::Pushable

#initialize, push, #receiver_type, #specify_receiver

Constructor Details

This class inherits a constructor from PushbulletRuby::Pushable

Instance Method Details

#pushObject

Raises:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pushbullet_ruby/pushable/file.rb', line 4

def push
  raise MissingParameter unless required_parameters.all? { |e| params.keys.include?(e) }

  file_name = params[:file_name]
  file_path = params[:file_path]

  upload_file(file_name, file_path) do |data|
    payload = {
        file_name: data['file_name'],
        file_type: data['file_type'],
        file_url:  data['file_url'],
        body:      params['body'],
        type:      type
    }

    payload = specify_receiver(payload)

    client.post('/v2/pushes', payload)
  end
end

#required_parametersObject



29
30
31
# File 'lib/pushbullet_ruby/pushable/file.rb', line 29

def required_parameters
  [:file_name, :file_path, :body]
end

#typeObject



25
26
27
# File 'lib/pushbullet_ruby/pushable/file.rb', line 25

def type
  :file
end

#upload_file(file_name, file_path) {|data.body| ... } ⇒ Object

Yields:

  • (data.body)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pushbullet_ruby/pushable/file.rb', line 33

def upload_file(file_name, file_path, &block)
  mime_type = MIME::Types.type_for(file_path).first.to_s

  data = upload_request(file_name, mime_type)

  upload_url = data.body['upload_url']
  params     = data.body['data']

  io = Faraday::UploadIO.new(file_path, mime_type)

  client.post upload_url, params.merge(file: io)

  yield data.body
end

#upload_request(file_name, mime_type) ⇒ Object



48
49
50
# File 'lib/pushbullet_ruby/pushable/file.rb', line 48

def upload_request(file_name, mime_type)
  client.post '/v2/upload-request', file_name: file_name, file_type: mime_type
end