Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new and much more powerful API #3

Merged
merged 6 commits into from
May 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pushbullet

Ruby client of [Pushbullet](https://www.pushbullet.com/) API.
Ruby client of [Pushbullet](https://www.pushbullet.com/).

## Installation

Expand Down Expand Up @@ -30,31 +30,35 @@ You can send following list:
- file

```ruby
# get json about own device list
# Get own device list
client.devices

client.push_note(DEVICE_ID, 'This is Title', 'This is Body!!!!')
client.push_file(DEVICE_ID, 'File Name', 'path/to/file')
```
# note
client.push_note(device_iden, 'Title of note', 'A note's content')

#### :secret: Push to Friend's device :secret:
# link
client.push_link(device_iden, 'Title of link', 'https://www.pushbullet.com', 'This website is awesome.')

You can send following list:
- note
- link
- address
- list
# address
client.push_address(device_iden, 'Name of address', 'Addrss of place')

```ruby
# get json about friend list of Pushbullet
# list
client.push_list(device_iden, 'Title of lists', [{text: 'Buy milk', checked: true}, {text: 'Buy Soy milk', checked: false}])

# file
client.push_file(device_iden, 'File name', 'path/to/file', 'An optional message')

# Get friend list of Pushbullet
client.contacts

client.push_note_to('[email protected]', 'Hi!!!', 'This is Body!!!!')
# Get self info
client.me
```

## TODO

- test test test...
- push friend's device
- test...

## Contributing

Expand Down
42 changes: 9 additions & 33 deletions lib/pushbullet/api.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
require 'pushbullet/api/contacts'
require 'pushbullet/api/devices'
require 'pushbullet/api/pushes'
require 'pushbullet/api/me'

module Pushbullet
module API
def devices
get('/api/devices')
end

def push_note(device_id, title, body)
push :note, device_id, title: title, body: body
end

def push_link(device_id, title, url)
push :link, device_id, title: title, url: url
end

def push_address(device_id, title, address)
push :address, device_id, title: title, address: address
end

# FIXME Now, can send *only* one item
def push_list(device_id, title, items)
push :list, device_id, title: title, items: items
end

def push_file(device_id, file_path)
mime_type = MIME::Types.type_for(file_path).first.to_s
io = Faraday::UploadIO.new(file_path, mime_type)

push :file, device_id, file: io
end

private

def push(type, device_id, payload)
post '/api/pushes', payload.merge(device_id: device_id, type: type)
end
include Contacts
include Devices
include Pushes
include Me
end
end
13 changes: 13 additions & 0 deletions lib/pushbullet/api/contacts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Pushbullet
module API
module Contacts
def contacts
get '/v2/contacts'
end

def delete_contact(contact_iden)
delete "/v2/contacts/#{contact_iden}"
end
end
end
end
13 changes: 13 additions & 0 deletions lib/pushbullet/api/devices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Pushbullet
module API
module Devices
def devices
get '/v2/devices'
end

def delete_device(device_iden)
delete "/v2/devices/#{device_iden}"
end
end
end
end
9 changes: 9 additions & 0 deletions lib/pushbullet/api/me.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Pushbullet
module API
module Me
def me
get '/v2/users/me'
end
end
end
end
71 changes: 71 additions & 0 deletions lib/pushbullet/api/pushes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Pushbullet
module API
module Pushes
def push_note(device_iden, title, body)
push :note, device_iden, title: title, body: body
end

def push_link(device_iden, title, url, body)
push :link, device_iden, title: title, url: url, body: body
end

def push_address(device_iden, name, address)
push :address, device_iden, name: name, address: address
end

def push_list(device_iden, title, items)
push :list, device_iden, title: title, items: items
end

def push_file(device_iden, file_name, file_path, body)
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: body
}

push :file, device_iden, payload
end
end

def pushes(modified_after = nil, cursor = nil)
params = {modified_after: modified_after, cursor: cursor}

params = params.values.all?(&:nil?) ? {} : params

get 'v2/pushes', params
end

def delete_push(push_iden)
delete "/v2/pushes/#{push_iden}"
end

private

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']
payload = data.body['data']

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

post upload_url, payload.merge(file: io)

yield data.body
end

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

def push(type, device_iden, payload)
post '/v2/pushes', payload.merge(device_iden: device_iden, type: type)
end
end
end
end
11 changes: 11 additions & 0 deletions lib/pushbullet/basic_authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Pushbullet
class BasicAuthentication < Faraday::Request::BasicAuthentication
def call(env)
if env.url.to_s.match(Pushbullet::Client::ENDPOINT)
env.request_headers[KEY] = @header_value
end

@app.call(env)
end
end
end
39 changes: 36 additions & 3 deletions lib/pushbullet/client.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
require 'faraday'
require 'mime/types'
require 'pushbullet/request'

require 'pushbullet/api'
require 'pushbullet/secret_api'
require 'pushbullet/basic_authentication'
require 'pushbullet/http_exception'
require 'pushbullet/parse_json'
require 'pushbullet/request'

module Pushbullet
class Client
include Request
include API
include SecretAPI

ENDPOINT = 'https://api.pushbullet.com'

attr_reader :api_key

def initialize(api_key)
@api_key = api_key
end

def middleware
@middleware ||= Faraday::RackBuilder.new do |f|
f.request :multipart
f.request :url_encoded

f.use Pushbullet::BasicAuthentication, @api_key, ''
f.use Pushbullet::ParseJSON
f.use Pushbullet::HttpException

f.adapter :net_http
end
end

def connection_options
@connection_options ||= {
:builder => middleware,
:headers => {
:accept => 'application/json',
:user_agent => "Pushbullet Ruby Gem #{Pushbullet::VERSION}",
}
}
end

private

def connection
@connection ||= Faraday.new(ENDPOINT, connection_options)
end
end
end
22 changes: 0 additions & 22 deletions lib/pushbullet/connection.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/pushbullet/http_exception.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'faraday'

module Pushbullet
class BadRequest < StandardError; end
class Unauthorized < StandardError; end
Expand Down
6 changes: 5 additions & 1 deletion lib/pushbullet/parse_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
module Pushbullet
class ParseJSON < Faraday::Response::Middleware
def on_complete(env)
env[:body] = JSON.parse(env[:body])
env[:body] = JSON.parse(env[:body]) unless unparsable_status_codes.include?(env.status)
end

def unparsable_status_codes
[204]
end
end
end
17 changes: 9 additions & 8 deletions lib/pushbullet/request.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
require 'pushbullet/connection'

module Pushbullet
module Request
include Connection

def get(path)
request(:get, path)
def get(path, params = {})
request(:get, path, params)
end

def post(path, payload)
request(:post, path, payload)
end

def delete(path)
request(:delete, path)
end

private

def request(method, path, payload = {})
def request(method, path, params = {})
response = connection.send(method) {|request|
request.url path
request.body = payload if method == :post
request.params = params if method == :get
request.body = params if method == :post
}
end
end
Expand Down
30 changes: 0 additions & 30 deletions lib/pushbullet/secret_api.rb

This file was deleted.

Loading