Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Commit

Permalink
Update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
dankimio committed Sep 21, 2019
1 parent 408f890 commit a109511
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 50 deletions.
7 changes: 3 additions & 4 deletions houston.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path('../lib', __FILE__)
$LOAD_PATH.push File.expand_path('lib', __dir__)
require 'houston/version'

Gem::Specification.new do |s|
Expand All @@ -16,11 +15,11 @@ Gem::Specification.new do |s|
s.add_dependency 'commander', '~> 4.4'
s.add_dependency 'json'

s.add_development_dependency 'rspec', '~> 3.8'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec', '~> 3.8'
s.add_development_dependency 'simplecov'

s.files = Dir['./**/*'].reject { |file| file =~ /\.\/(bin|log|pkg|script|spec|test|vendor)/ }
s.files = Dir['./**/*'].reject { |file| file =~ %r{\./(bin|log|pkg|script|spec|test|vendor)} }
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.require_paths = ['lib']
Expand Down
20 changes: 10 additions & 10 deletions lib/houston/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Client

class << self
def development
client = self.new
client = new
client.gateway_uri = APPLE_DEVELOPMENT_GATEWAY_URI
client.feedback_uri = APPLE_DEVELOPMENT_FEEDBACK_URI
client
end

def production
client = self.new
client = new
client.gateway_uri = APPLE_PRODUCTION_GATEWAY_URI
client.feedback_uri = APPLE_PRODUCTION_FEEDBACK_URI
client
Expand All @@ -41,7 +41,7 @@ def push(*notifications)
ssl = connection.ssl

notifications.each_with_index do |notification, index|
next unless notification.kind_of?(Notification)
next unless notification.is_a?(Notification)
next if notification.sent?
next unless notification.valid?

Expand All @@ -51,13 +51,13 @@ def push(*notifications)
notification.mark_as_sent!

read_socket, _write_socket = IO.select([ssl], [ssl], [ssl], nil)
if (read_socket && read_socket[0])
if error = connection.read(6)
_command, status, index = error.unpack('ccN')
notification.apns_error_code = status
notification.mark_as_unsent!
end
end
next unless read_socket && read_socket[0]

next unless error = connection.read(6)

_command, status, index = error.unpack('ccN')
notification.apns_error_code = status
notification.mark_as_unsent!
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/houston/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def open(uri, certificate, passphrase)
def initialize(uri, certificate, passphrase)
@uri = URI(uri)
@certificate = certificate.to_s
@passphrase = passphrase.to_s unless passphrase.nil?
@passphrase = passphrase.to_s unless passphrase.nil?
end

def open
Expand All @@ -45,7 +45,7 @@ def open
end

def open?
not (@ssl && @socket).nil?
!(@ssl && @socket).nil?
end

def close
Expand All @@ -59,7 +59,7 @@ def close
end

def closed?
not open?
!open?
end
end
end
49 changes: 28 additions & 21 deletions lib/houston/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class APNSError < RuntimeError

def initialize(code)
raise ArgumentError unless CODES.include?(code)

super(CODES[code])
@code = code
end
Expand All @@ -34,8 +35,8 @@ def initialize(code)
attr_reader :sent_at
attr_writer :apns_error_code

alias :device :token
alias :device= :token=
alias device token
alias device= token=

def initialize(options = {})
@token = options.delete(:token) || options.delete(:device)
Expand All @@ -58,11 +59,17 @@ def initialize(options = {})
end

def payload
json = {}.merge(@custom_data || {}).inject({}) { |h, (k, v)| h[k.to_s] = v; h }
json = {}.merge(@custom_data || {}).each_with_object({}) { |(k, v), h| h[k.to_s] = v; }

json['aps'] ||= {}
json['aps']['alert'] = @alert if @alert
json['aps']['badge'] = @badge.to_i rescue 0 if @badge
if @badge
json['aps']['badge'] = begin
@badge.to_i
rescue StandardError
0
end
end
json['aps']['sound'] = @sound if @sound
json['aps']['category'] = @category if @category
json['aps']['content-available'] = 1 if @content_available
Expand Down Expand Up @@ -99,30 +106,30 @@ def valid?
end

def error
APNSError.new(@apns_error_code) if @apns_error_code && @apns_error_code.nonzero?
APNSError.new(@apns_error_code) if @apns_error_code&.nonzero?
end

private

def device_token_item
[1, 32, @token.gsub(/[<\s>]/, '')].pack('cnH64')
end
def device_token_item
[1, 32, @token.gsub(/[<\s>]/, '')].pack('cnH64')
end

def payload_item
json = payload.to_json
[2, json.bytes.count, json].pack('cna*')
end
def payload_item
json = payload.to_json
[2, json.bytes.count, json].pack('cna*')
end

def identifier_item
[3, 4, @id].pack('cnN') unless @id.nil?
end
def identifier_item
[3, 4, @id].pack('cnN') unless @id.nil?
end

def expiration_item
[4, 4, @expiry.to_i].pack('cnN') unless @expiry.nil?
end
def expiration_item
[4, 4, @expiry.to_i].pack('cnN') unless @expiry.nil?
end

def priority_item
[5, 1, @priority].pack('cnc') unless @priority.nil?
end
def priority_item
[5, 1, @priority].pack('cnc') unless @priority.nil?
end
end
end
2 changes: 1 addition & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

describe '#push' do
it 'should accept zero arguments' do
expect(Houston::Client.development.push()).to be_nil()
expect(Houston::Client.development.push).to be_nil
end
end
end
Expand Down
17 changes: 9 additions & 8 deletions spec/notification_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe Houston::Notification do
let(:notification_options) {
let(:notification_options) do
{
token: '<ce8be627 2e43e855 16033e24 b4c28922 0eeda487 9c477160 b2545e95 b68b5969>',
alert: 'Houston, we have a problem.',
Expand All @@ -17,7 +17,7 @@
url_args: %w[boarding A998],
thread_id: 'notify-team-ios'
}
}
end

subject { Houston::Notification.new(notification_options) }

Expand Down Expand Up @@ -107,7 +107,8 @@
'thread-id' => 'notify-team-ios'
},
'key1' => 1,
'key2' => 'abc')
'key2' => 'abc'
)
end

it 'should create a dictionary of only custom data and empty aps' do
Expand Down Expand Up @@ -149,15 +150,15 @@
end

it 'should create a dictionary only with mutable-content' do
expect(Houston::Notification.new(mutable_content: true).payload).to eq(
'aps' => { 'mutable-content' => 1 }
)
expect(Houston::Notification.new(mutable_content: true).payload).to eq(
'aps' => { 'mutable-content' => 1 }
)
end

it 'should create a dictionary only with thread-id' do
expect(Houston::Notification.new(thread_id: 'notify-ios-team').payload).to eq(
'aps' => { 'thread-id' => 'notify-ios-team' }
)
'aps' => { 'thread-id' => 'notify-ios-team' }
)
end

it 'should allow custom data inside aps key' do
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

class MockConnection
class << self
def open(uri, certificate, passphrase)
yield self.new
def open(_uri, _certificate, _passphrase)
yield new
end
end

Expand All @@ -23,7 +23,7 @@ def initialize
]
end

def read(bytes)
def read(_bytes)
return nil if @unregistered_devices.empty?

@unregistered_devices.shift.pack('N1n1H*')
Expand Down

0 comments on commit a109511

Please sign in to comment.