From 62179ccc1b4b15a83b6d0c6f031af438085fa829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Leit=C3=A3o?= Date: Sun, 13 Dec 2015 15:55:06 +0000 Subject: [PATCH 1/3] Remove Module/Class nesting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo Leitão --- .rubocop.yml | 263 +++++++++++++++++++++++++++++++++++++ lib/pushbullet.rb | 2 + lib/pushbullet/channel.rb | 34 +++-- lib/pushbullet/client.rb | 54 ++++---- lib/pushbullet/contact.rb | 28 ++-- lib/pushbullet/device.rb | 22 ++-- lib/pushbullet/error.rb | 5 +- lib/pushbullet/push.rb | 40 +++--- lib/pushbullet/pushable.rb | 1 - lib/pushbullet/resource.rb | 40 +++--- 10 files changed, 368 insertions(+), 121 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a3f37bb..b550ba6 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1 +1,264 @@ require: rubocop-rspec + +Rails/TimeZone: + Enabled: false + +AccessorMethodName: + Enabled: false + +ActionFilter: + Enabled: false + +Alias: + Enabled: false + +ArrayJoin: + Enabled: false + +AsciiComments: + Enabled: false + +AsciiIdentifiers: + Enabled: false + +Attr: + Enabled: false + +BlockNesting: + Enabled: false + +CaseEquality: + Enabled: false + +CharacterLiteral: + Enabled: false + +ClassAndModuleChildren: + Enabled: false + +ClassLength: + Enabled: false + +ClassVars: + Enabled: false + +CollectionMethods: + PreferredMethods: + find: detect + reduce: inject + collect: map + find_all: select + +ColonMethodCall: + Enabled: false + +CommentAnnotation: + Enabled: false + +CyclomaticComplexity: + Enabled: false + +Delegate: + Enabled: false + +DeprecatedHashMethods: + Enabled: false + +Documentation: + Enabled: false + +DotPosition: + EnforcedStyle: trailing + +DoubleNegation: + Enabled: false + +EachWithObject: + Enabled: false + +EmptyLiteral: + Enabled: false + +Encoding: + Enabled: false + +EvenOdd: + Enabled: false + +FileName: + Enabled: false + +FlipFlop: + Enabled: false + +FormatString: + Enabled: false + +GlobalVars: + Enabled: false + +GuardClause: + Enabled: false + +IfUnlessModifier: + Enabled: false + +IfWithSemicolon: + Enabled: false + +InlineComment: + Enabled: false + +Lambda: + Enabled: false + +LambdaCall: + Enabled: false + +LineEndConcatenation: + Enabled: false + +LineLength: + Max: 85 + +MethodLength: + Enabled: false + +ModuleFunction: + Enabled: false + +NegatedIf: + Enabled: false + +NegatedWhile: + Enabled: false + +BlockDelimiters: + Exclude: + - spec/**/* + +Next: + Enabled: false + +NilComparison: + Enabled: false + +Not: + Enabled: false + +NumericLiterals: + Enabled: false + +OneLineConditional: + Enabled: false + +OpMethod: + Enabled: false + +ParameterLists: + Enabled: false + +PercentLiteralDelimiters: + Enabled: false + +PerlBackrefs: + Enabled: false + +PredicateName: + NamePrefixBlacklist: + - is_ + +Proc: + Enabled: false + +RaiseArgs: + Enabled: false + +RegexpLiteral: + Enabled: false + +SelfAssignment: + Enabled: false + +SingleLineBlockParams: + Enabled: false + +SingleLineMethods: + Enabled: false + +SignalException: + Enabled: false + +SpecialGlobalVars: + Enabled: false + +StringLiterals: + EnforcedStyle: single_quotes + +VariableInterpolation: + Enabled: false + +TrailingComma: + Enabled: false + +TrivialAccessors: + Enabled: false + +VariableInterpolation: + Enabled: false + +WhenThen: + Enabled: false + +WhileUntilModifier: + Enabled: false + +WordArray: + Enabled: false + +# Lint + +AmbiguousOperator: + Enabled: false + +AmbiguousRegexpLiteral: + Enabled: false + +AssignmentInCondition: + Enabled: false + +ConditionPosition: + Enabled: false + +DeprecatedClassMethods: + Enabled: false + +ElseLayout: + Enabled: false + +HandleExceptions: + Enabled: false + +InvalidCharacterLiteral: + Enabled: false + +LiteralInCondition: + Enabled: false + +LiteralInInterpolation: + Enabled: false + +Loop: + Enabled: false + +ParenthesesAsGroupedExpression: + Enabled: false + +RequireParentheses: + Enabled: false + +UnderscorePrefixedVariableName: + Enabled: false + +Void: + Enabled: false diff --git a/lib/pushbullet.rb b/lib/pushbullet.rb index 990bf46..3f3dacd 100644 --- a/lib/pushbullet.rb +++ b/lib/pushbullet.rb @@ -1,5 +1,6 @@ require 'json' require 'logger' +require 'rest-client' require 'pushbullet/pushable' require 'pushbullet/client' require 'pushbullet/resource' @@ -12,6 +13,7 @@ require 'core_ext/string.rb' module Pushbullet + def self.api_token=(api_token) @api_token = api_token end diff --git a/lib/pushbullet/channel.rb b/lib/pushbullet/channel.rb index 62f52bf..e9af90b 100644 --- a/lib/pushbullet/channel.rb +++ b/lib/pushbullet/channel.rb @@ -1,24 +1,22 @@ -module Pushbullet - class Channel < Resource - include Pushable +class Pushbullet::Channel < Pushbullet::Resource + include Pushbullet::Pushable - def self.subscribe(name) - create(channel_tag: name) - end + def self.subscribe(name) + create(channel_tag: name) + end - def self.unsubscribe(idn) - Pushbullet.client.delete "#{path}/#{idn}" - true - end + def self.unsubscribe(idn) + Pushbullet.client.delete "#{path}/#{idn}" + true + end - def self.get_info(tag) - channel = new Pushbullet.client.get("channel-info?tag=#{tag}") - channel.recent_pushes.map! { |push| Pushbullet::Push.new push } - channel - end + def self.get_info(tag) + channel = new Pushbullet.client.get("channel-info?tag=#{tag}") + channel.recent_pushes.map! { |push| Pushbullet::Push.new push } + channel + end - def self.path - 'subscriptions' - end + def self.path + 'subscriptions' end end diff --git a/lib/pushbullet/client.rb b/lib/pushbullet/client.rb index a3225c9..f9f615d 100644 --- a/lib/pushbullet/client.rb +++ b/lib/pushbullet/client.rb @@ -1,39 +1,35 @@ -module Pushbullet - class Client - require 'rest_client' +class Pushbullet::Client + API_VERSION = 2 + END_POINT = "https://api.pushbullet.com/v#{API_VERSION}/" - API_VERSION = 2 - END_POINT = "https://api.pushbullet.com/v#{API_VERSION}/" - - def get(path) - JSON.parse(generic_request(path).get(&handle_error)) - end + def get(path) + JSON.parse(generic_request(path).get(&handle_error)) + end - def post(path, params = {}) - JSON.parse(generic_request(path).post params, &handle_error) - end + def post(path, params = {}) + JSON.parse(generic_request(path).post params, &handle_error) + end - def put(path, params = {}) - JSON.parse(generic_request(path).put params, &handle_error) - end + def put(path, params = {}) + JSON.parse(generic_request(path).put params, &handle_error) + end - def delete(path, params = {}) - JSON.parse(generic_request(path).delete params, &handle_error) - end + def delete(path, params = {}) + JSON.parse(generic_request(path).delete params, &handle_error) + end - def generic_request(path) - RestClient::Resource.new("#{END_POINT}#{path}", Pushbullet.api_token, '') - end + def generic_request(path) + RestClient::Resource.new("#{END_POINT}#{path}", Pushbullet.api_token, '') + end - private + private - def handle_error - proc do |response, request, result, &block| - if response.code >= 400 && response.code <= 500 - fail Pushbullet::Error, JSON.parse(response)['error']['message'] - else - response.return!(request, result, &block) - end + def handle_error + proc do |response, request, result, &block| + if response.code >= 400 && response.code <= 500 + fail Pushbullet::Error, JSON.parse(response)['error']['message'] + else + response.return!(request, result, &block) end end end diff --git a/lib/pushbullet/contact.rb b/lib/pushbullet/contact.rb index 58d41b8..da6fd6e 100644 --- a/lib/pushbullet/contact.rb +++ b/lib/pushbullet/contact.rb @@ -1,21 +1,19 @@ -module Pushbullet - class Contact < Resource - include Pushable +class Pushbullet::Contact < Pushbullet::Resource + include Pushbullet::Pushable - def self.me - new Pushbullet.client.get('users/me') - end + def self.me + new Pushbullet.client.get('users/me') + end - def self.create(name, email) - super(name: name, email: email) - end + def self.create(name, email) + super(name: name, email: email) + end - def save - super(name: name) - end + def save + super(name: name) + end - def target_id - email - end + def target_id + email end end diff --git a/lib/pushbullet/device.rb b/lib/pushbullet/device.rb index e2eb7a6..7367c76 100644 --- a/lib/pushbullet/device.rb +++ b/lib/pushbullet/device.rb @@ -1,17 +1,15 @@ -module Pushbullet - class Device < Resource - include Pushable +class Pushbullet::Device < Pushbullet::Resource + include Pushbullet::Pushable - def self.create(nickname, type) - super(nickname: nickname, type: type) - end + def self.create(nickname, type) + super(nickname: nickname, type: type) + end - def save - super(nickname: nickname) - end + def save + super(nickname: nickname) + end - def target_id - iden - end + def target_id + iden end end diff --git a/lib/pushbullet/error.rb b/lib/pushbullet/error.rb index da5d3a7..d0f0122 100644 --- a/lib/pushbullet/error.rb +++ b/lib/pushbullet/error.rb @@ -1,5 +1,2 @@ -module Pushbullet - class Error < StandardError - - end +class Pushbullet::Error < StandardError end diff --git a/lib/pushbullet/push.rb b/lib/pushbullet/push.rb index dde2b3e..843273b 100644 --- a/lib/pushbullet/push.rb +++ b/lib/pushbullet/push.rb @@ -1,29 +1,27 @@ -module Pushbullet - class Push < Resource +class Pushbullet::Push < Pushbullet::Resource - def self.create_note(id, title, body) - create :note, id, title: title, body: body - end + def self.create_note(id, title, body) + create :note, id, title: title, body: body + end - def self.create_link(id, title, url, body) - create :link, id, title: title, url: url, body: body - end + def self.create_link(id, title, url, body) + create :link, id, title: title, url: url, body: body + end - def self.create_address(id, name, address) - create :address, id, name: name, address: address - end + def self.create_address(id, name, address) + create :address, id, name: name, address: address + end - def self.create_list(id, title, items) - create :list, id, title: title, items: items - end + def self.create_list(id, title, items) + create :list, id, title: title, items: items + end - def self.create(type, iden, payload) - id = iden[/.@.?/].nil? ? :device_iden : :email - super(payload.merge(type: type, id => iden)) - end + def self.create(type, iden, payload) + id = iden[/.@.?/].nil? ? :device_iden : :email + super(payload.merge(type: type, id => iden)) + end - def self.path - 'pushes' - end + def self.path + 'pushes' end end diff --git a/lib/pushbullet/pushable.rb b/lib/pushbullet/pushable.rb index 5f9ca56..4aa3e68 100644 --- a/lib/pushbullet/pushable.rb +++ b/lib/pushbullet/pushable.rb @@ -1,6 +1,5 @@ module Pushbullet module Pushable - def push_note(title, body) Push.create_note target_id, title, body end diff --git a/lib/pushbullet/resource.rb b/lib/pushbullet/resource.rb index ee55c72..58578e8 100644 --- a/lib/pushbullet/resource.rb +++ b/lib/pushbullet/resource.rb @@ -1,28 +1,26 @@ -module Pushbullet - class Resource < OpenStruct - def self.create(params) - new Pushbullet.client.post(path, params) - end +class Pushbullet::Resource < OpenStruct + def self.create(params) + new Pushbullet.client.post(path, params) + end - def self.all - Pushbullet.client.get(path)[path].map do |model| - new model - end + def self.all + Pushbullet.client.get(path)[path].map do |model| + new model end + end - def save(params) - Pushbullet.client.post "#{self.class.path}/#{iden}", params - true - end + def save(params) + Pushbullet.client.post "#{self.class.path}/#{iden}", params + true + end - def destroy - Pushbullet.client.delete "#{self.class.path}/#{iden}" - true - end + def destroy + Pushbullet.client.delete "#{self.class.path}/#{iden}" + true + end - def self.path - klass = self.is_a?(Class) ? self : self.class - @path ||= "#{klass.to_s.demodulize.downcase}s" - end + def self.path + klass = self.is_a?(Class) ? self : self.class + @path ||= "#{klass.to_s.demodulize.downcase}s" end end From 86ae363e86a80c81fbb42d4c613d7e9fa58d2a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Leit=C3=A3o?= Date: Wed, 16 Dec 2015 00:05:39 +0000 Subject: [PATCH 2/3] Add base structure of ruby-pushbullet v1.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo Leitão --- Gemfile.lock | 18 +++++------ README.md | 23 +++++++------- Rakefile | 10 ++++++ lib/pushbullet.rb | 26 ++++++++++------ lib/pushbullet/channel.rb | 22 ++++--------- lib/pushbullet/chat.rb | 14 +++++++++ lib/pushbullet/client.rb | 8 ++++- lib/pushbullet/configuration.rb | 13 ++++++++ lib/pushbullet/contact.rb | 21 ++++--------- lib/pushbullet/device.rb | 15 +++------ lib/pushbullet/push.rb | 8 +++++ lib/pushbullet/pushable.rb | 8 ++--- lib/pushbullet/resource.rb | 54 ++++++++++++++++++++++++++++++-- lib/pushbullet/subscription.rb | 16 ++++++++++ lib/pushbullet/user.rb | 9 ++++++ ruby-pushbullet.gemspec | 3 +- spec/pushbullet/channel_spec.rb | 47 ---------------------------- spec/pushbullet/client_spec.rb | 27 ---------------- spec/pushbullet/contact_spec.rb | 55 --------------------------------- spec/pushbullet/devices_spec.rb | 55 --------------------------------- spec/pushbullet/push_spec.rb | 27 ---------------- 21 files changed, 186 insertions(+), 293 deletions(-) create mode 100644 lib/pushbullet/chat.rb create mode 100644 lib/pushbullet/configuration.rb create mode 100644 lib/pushbullet/subscription.rb create mode 100644 lib/pushbullet/user.rb delete mode 100644 spec/pushbullet/channel_spec.rb delete mode 100644 spec/pushbullet/client_spec.rb delete mode 100644 spec/pushbullet/contact_spec.rb delete mode 100644 spec/pushbullet/devices_spec.rb delete mode 100644 spec/pushbullet/push_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 94e0856..d2dc958 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -18,11 +18,10 @@ GEM ast (2.0.0) astrolabe (1.3.0) parser (>= 2.2.0.pre.3, < 3.0) - awesome_print (1.2.0) + coderay (1.1.0) crack (0.4.2) safe_yaml (~> 1.0.0) diff-lcs (1.2.5) - docile (1.1.5) domain_name (0.5.25) unf (>= 0.0.5, < 1.0.0) factory_girl (4.5.0) @@ -32,13 +31,17 @@ GEM i18n (0.7.0) json (1.8.2) json (1.8.2-java) + method_source (0.8.2) mime-types (2.99) minitest (5.5.1) - multi_json (1.10.1) netrc (0.11.0) parser (2.2.0.2) ast (>= 1.1, < 3.0) powerpack (0.0.9) + pry (0.10.1) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) rainbow (2.0.0) rake (10.4.2) rest-client (1.8.0) @@ -66,11 +69,7 @@ GEM rubocop-rspec (1.2.1) ruby-progressbar (1.7.1) safe_yaml (1.0.4) - simplecov (0.9.1) - docile (~> 1.1.0) - multi_json (~> 1.0) - simplecov-html (~> 0.8.0) - simplecov-html (0.8.0) + slop (3.6.0) thread_safe (0.3.4) thread_safe (0.3.4-java) tzinfo (1.2.2) @@ -88,15 +87,14 @@ PLATFORMS ruby DEPENDENCIES - awesome_print bundler factory_girl + pry rake rspec (~> 3.0.0) rubocop rubocop-rspec ruby-pushbullet! - simplecov vcr webmock diff --git a/README.md b/README.md index 43e801c..c45a8f3 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,9 @@ gem install ruby-pushbullet You can get your api token from [Account Settings](https://www.pushbullet.com/account) ```ruby -Pushbullet.api_token = 'YOUR_API_TOKEN' +Pushbullet.configure do |config| + config.api_token = 'YOUR_API_TOKEN' +end ``` ## Usage @@ -32,14 +34,11 @@ Pushbullet.api_token = 'YOUR_API_TOKEN' ### Contact ```ruby -# Get all contacts -Pushbullet::Contact.all +# Get all Chats +Pushbullet::Chat.all # create contact -contact = Pushbullet::Contact.create('Name', 'example@mail.com') -contact.name = 'Another name' -contact.save # update - +contact = Pushbullet::Chat.create('example@mail.com') ``` ### Device @@ -69,19 +68,21 @@ Pushbullet::Push.create_list(id, title, items) **Or directly from a device or a contact** ```ruby -me = Pushbullet::Contact.me +me = Pushbullet::User.me me.push_note(title, body) # or... device = Pushbullet::Device.all.first device.push_link(title, link, body) - ``` -### Channel +### Subscription -TODO... +```ruby +# Get all subscriptions +Pushbullet::Subscrition.all +``` ## Contributing diff --git a/Rakefile b/Rakefile index 79e660a..140e108 100644 --- a/Rakefile +++ b/Rakefile @@ -5,3 +5,13 @@ require 'rspec/core/rake_task' RSpec::Core::RakeTask.new task :default => :spec + +desc 'Open an irb session preloaded with this library' +task :console do + require 'pry' + require 'pushbullet' + Pushbullet.configure do |config| + config.api_token = 'nice_try' + end + Pry.start +end diff --git a/lib/pushbullet.rb b/lib/pushbullet.rb index 3f3dacd..ba7bcf1 100644 --- a/lib/pushbullet.rb +++ b/lib/pushbullet.rb @@ -1,25 +1,31 @@ +# libs require 'json' require 'logger' require 'rest-client' +# external +require 'core_ext/string' +# pushbullet require 'pushbullet/pushable' +require 'pushbullet/configuration' require 'pushbullet/client' require 'pushbullet/resource' -require 'pushbullet/contact' +require 'pushbullet/error' +require 'pushbullet/user' require 'pushbullet/device' require 'pushbullet/push' require 'pushbullet/channel' -require 'pushbullet/error' - -require 'core_ext/string.rb' +require 'pushbullet/subscription' +require 'pushbullet/chat' +require 'pushbullet/contact' module Pushbullet + @configuration = Configuration.new - def self.api_token=(api_token) - @api_token = api_token - end - - def self.api_token - @api_token + def self.configure + if block_given? + yield @configuration + client.api_token = @configuration.api_token + end end def self.client diff --git a/lib/pushbullet/channel.rb b/lib/pushbullet/channel.rb index e9af90b..ac618cd 100644 --- a/lib/pushbullet/channel.rb +++ b/lib/pushbullet/channel.rb @@ -1,22 +1,12 @@ class Pushbullet::Channel < Pushbullet::Resource - include Pushbullet::Pushable + register_attributes :iden, :description, :image_url, :name, :tag, + :subscriber_count, :recent_pushes - def self.subscribe(name) - create(channel_tag: name) - end - - def self.unsubscribe(idn) - Pushbullet.client.delete "#{path}/#{idn}" - true - end - - def self.get_info(tag) - channel = new Pushbullet.client.get("channel-info?tag=#{tag}") + def self.get_info(tag, no_recent_pushes = nil) + query = "channel-info?tag=#{tag}" + query += "&no_recent_pushes=#{no_recent_pushes}" unless no_recent_pushes.nil? + channel = new Pushbullet.client.get(query) channel.recent_pushes.map! { |push| Pushbullet::Push.new push } channel end - - def self.path - 'subscriptions' - end end diff --git a/lib/pushbullet/chat.rb b/lib/pushbullet/chat.rb new file mode 100644 index 0000000..c89cf72 --- /dev/null +++ b/lib/pushbullet/chat.rb @@ -0,0 +1,14 @@ +class Pushbullet::Chat < Pushbullet::Resource + register_attributes :iden, :active, :created, :modified, :muted + + register_updatable_attributes :muted + + def initialize(attributes) + attributes['with'] = Pushbullet::Contact.new(attributes['with']) + super attributes + end + + def self.create(email) + super(email: email) + end +end diff --git a/lib/pushbullet/client.rb b/lib/pushbullet/client.rb index f9f615d..5eb45ea 100644 --- a/lib/pushbullet/client.rb +++ b/lib/pushbullet/client.rb @@ -2,6 +2,12 @@ class Pushbullet::Client API_VERSION = 2 END_POINT = "https://api.pushbullet.com/v#{API_VERSION}/" + attr_accessor :api_token + + def initialize(api_token = nil) + @api_token = api_token + end + def get(path) JSON.parse(generic_request(path).get(&handle_error)) end @@ -19,7 +25,7 @@ def delete(path, params = {}) end def generic_request(path) - RestClient::Resource.new("#{END_POINT}#{path}", Pushbullet.api_token, '') + RestClient::Resource.new("#{END_POINT}#{path}", api_token, '') end private diff --git a/lib/pushbullet/configuration.rb b/lib/pushbullet/configuration.rb new file mode 100644 index 0000000..e42b0f7 --- /dev/null +++ b/lib/pushbullet/configuration.rb @@ -0,0 +1,13 @@ +class Pushbullet::Configuration + CONFIGURABLE_ATTRIBUTES = %i(api_token) + + attr_accessor *CONFIGURABLE_ATTRIBUTES + + def initialize(attrs = {}) + self.attributes = attrs + end + + def attributes=(attrs = {}) + attrs.each { |key, value| instance_variable_set("@#{key}", value) } + end +end diff --git a/lib/pushbullet/contact.rb b/lib/pushbullet/contact.rb index da6fd6e..1d86324 100644 --- a/lib/pushbullet/contact.rb +++ b/lib/pushbullet/contact.rb @@ -1,19 +1,10 @@ -class Pushbullet::Contact < Pushbullet::Resource - include Pushbullet::Pushable +class Pushbullet::Contact - def self.me - new Pushbullet.client.get('users/me') - end - - def self.create(name, email) - super(name: name, email: email) - end - - def save - super(name: name) - end + attr_reader :type, :iden, :name, :email, :email_normalized, :image_url - def target_id - email + def initialize(attributes) + attributes.each do |key, value| + instance_variable_set(:"@#{key}", value) + end end end diff --git a/lib/pushbullet/device.rb b/lib/pushbullet/device.rb index 7367c76..88c6d32 100644 --- a/lib/pushbullet/device.rb +++ b/lib/pushbullet/device.rb @@ -1,15 +1,10 @@ class Pushbullet::Device < Pushbullet::Resource include Pushbullet::Pushable - def self.create(nickname, type) - super(nickname: nickname, type: type) - end + register_attributes :iden, :active, :created, :modified, :icon, :nickname, + :generated_nickname, :manufacturer, :model, :app_version, + :fingerprint, :key_fingerprint, :push_token, :has_sms - def save - super(nickname: nickname) - end - - def target_id - iden - end + register_updatable_attributes :nickname, :model, :manufacturer, :push_token, + :app_version, :icon, :has_sms end diff --git a/lib/pushbullet/push.rb b/lib/pushbullet/push.rb index 843273b..4d3d702 100644 --- a/lib/pushbullet/push.rb +++ b/lib/pushbullet/push.rb @@ -1,4 +1,12 @@ class Pushbullet::Push < Pushbullet::Resource + register_attributes :iden, :active, :created, :modified, :type, :dismissed, + :guid, :direction, :sender_iden, :sender_email, + :sender_email_normalized, :sender_name, :receiver_iden, + :receiver_email, :receiver_email_normalized, + :target_device_iden, :source_device_iden, :client_iden, + :channel_iden, :awake_app_guids, :title, :body, :url, + :file_name, :file_type, :file_url, :image_url, + :image_width, :image_height def self.create_note(id, title, body) create :note, id, title: title, body: body diff --git a/lib/pushbullet/pushable.rb b/lib/pushbullet/pushable.rb index 4aa3e68..38fd6fe 100644 --- a/lib/pushbullet/pushable.rb +++ b/lib/pushbullet/pushable.rb @@ -1,19 +1,19 @@ module Pushbullet module Pushable def push_note(title, body) - Push.create_note target_id, title, body + Push.create_note iden, title, body end def push_link(title, url, body) - Push.create_link target_id, title, url, body + Push.create_link iden, title, url, body end def push_address(name, address) - Push.create_address target_id, name, address + Push.create_address iden, name, address end def push_list(title, items) - Push.create_list target_id, title, items + Push.create_list iden, title, items end end end diff --git a/lib/pushbullet/resource.rb b/lib/pushbullet/resource.rb index 58578e8..e794fc6 100644 --- a/lib/pushbullet/resource.rb +++ b/lib/pushbullet/resource.rb @@ -1,4 +1,11 @@ -class Pushbullet::Resource < OpenStruct +class Pushbullet::Resource + + def initialize(attributes) + attributes.each do |key, value| + instance_variable_set(:"@#{key}", value) + end + end + def self.create(params) new Pushbullet.client.post(path, params) end @@ -9,11 +16,22 @@ def self.all end end - def save(params) - Pushbullet.client.post "#{self.class.path}/#{iden}", params + def save + attributes_to_update = attributes.select do |key, _| + self.class.updatable_attributes.include? key.to_sym + end + Pushbullet.client.post "#{self.class.path}/#{iden}", attributes_to_update true end + def attributes + attrs = {} + self.class.attributes.each do |key| + attrs[key.to_s] = send(key) + end + attrs + end + def destroy Pushbullet.client.delete "#{self.class.path}/#{iden}" true @@ -23,4 +41,34 @@ def self.path klass = self.is_a?(Class) ? self : self.class @path ||= "#{klass.to_s.demodulize.downcase}s" end + + def self.register_attributes(*attributes) + @attributes ||= attributes + class_eval do + attributes.each do |attribute| + define_method(attribute) do + instance_variable_get(:"@#{attribute}") + end + end + end + end + + def self.attributes + @attributes + end + + def self.updatable_attributes + @updatable_attributes + end + + def self.register_updatable_attributes(*attributes) + @updatable_attributes ||= attributes + class_eval do + attributes.each do |attribute| + define_method(:"#{attribute}=") do |argument| + instance_variable_set(:"@#{attribute}", argument) + end + end + end + end end diff --git a/lib/pushbullet/subscription.rb b/lib/pushbullet/subscription.rb new file mode 100644 index 0000000..b880905 --- /dev/null +++ b/lib/pushbullet/subscription.rb @@ -0,0 +1,16 @@ +class Pushbullet::Subscription < Pushbullet::Resource + register_attributes :iden, :active, :created, :modified, :muted, :channel + + register_updatable_attributes :muted + + def initialize(attributes) + unless attributes['channel'].nil? + attributes['channel'] = Pushbullet::Channel.new(attributes['channel']) + end + super attributes + end + + def self.create(name) + super(channel_tag: name) + end +end diff --git a/lib/pushbullet/user.rb b/lib/pushbullet/user.rb new file mode 100644 index 0000000..a76619e --- /dev/null +++ b/lib/pushbullet/user.rb @@ -0,0 +1,9 @@ +class Pushbullet::User < Pushbullet::Resource + register_attributes :iden, :created, :modified, :email, :email_normalized, :name, + :image_url, :max_upload_size, :referred_count, :referrer_iden, + :active + + def self.me + new Pushbullet.client.get 'users/me' + end +end diff --git a/ruby-pushbullet.gemspec b/ruby-pushbullet.gemspec index 948c880..4e4bde1 100644 --- a/ruby-pushbullet.gemspec +++ b/ruby-pushbullet.gemspec @@ -19,9 +19,8 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'webmock' gem.add_development_dependency 'rubocop' gem.add_development_dependency 'rubocop-rspec' - gem.add_development_dependency 'awesome_print' gem.add_development_dependency 'vcr' - gem.add_development_dependency 'simplecov' + gem.add_development_dependency 'pry' gem.add_dependency 'rest-client', '~> 1.8.0' gem.add_dependency 'json' diff --git a/spec/pushbullet/channel_spec.rb b/spec/pushbullet/channel_spec.rb deleted file mode 100644 index b5a12c4..0000000 --- a/spec/pushbullet/channel_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'spec_helper' - -describe Pushbullet::Channel do - describe '::get_info' do - context 'given a channel tag' do - let(:tag) { 'jblow'} - - it 'returns channel info' do - VCR.use_cassette('channel_info') do - expect(described_class.get_info tag).to be_a described_class - end - end - - it 'returns channel with recent pushes' do - VCR.use_cassette('channel_info') do - described_class.get_info(tag).recent_pushes.each do |push| - expect(push).to be_a Pushbullet::Push - end - end - end - end - end - - describe '::subscribe' do - context 'given a channel tag' do - let(:tag) { 'jblow' } - - it 'returns a channel' do - VCR.use_cassette('channel_subscribe') do - expect(described_class.subscribe tag).to be_a described_class - end - end - end - end - - describe '::unsubscribe' do - context 'given a channel idn' do - let(:idn) { 'ujx7XdJ9W8qsjAlOnVVSqy' } - - it 'unsubscribes a channel' do - VCR.use_cassette('channel_unsubscribe') do - expect(described_class.unsubscribe idn).to be_truthy - end - end - end - end -end diff --git a/spec/pushbullet/client_spec.rb b/spec/pushbullet/client_spec.rb deleted file mode 100644 index 1ff5c14..0000000 --- a/spec/pushbullet/client_spec.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'spec_helper' - -describe Pushbullet::Client do - describe '#get' do - it_behaves_like('a better HTTP response handling') do - let(:request) { described_class.new.get '' } - end - end - - describe '#post' do - it_behaves_like('a better HTTP response handling') do - let(:request) { described_class.new.post '' } - end - end - - describe '#put' do - it_behaves_like('a better HTTP response handling') do - let(:request) { described_class.new.put '' } - end - end - - describe '#delete' do - it_behaves_like('a better HTTP response handling') do - let(:request) { described_class.new.delete '' } - end - end -end diff --git a/spec/pushbullet/contact_spec.rb b/spec/pushbullet/contact_spec.rb deleted file mode 100644 index 4f597ea..0000000 --- a/spec/pushbullet/contact_spec.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'spec_helper' - -describe Pushbullet::Contact do - describe '::all' do - it 'returns all contacts' do - VCR.use_cassette('contacts_all') do - expect(described_class.all).to be_a Array - end - end - - it 'returns iterable of Contacts' do - VCR.use_cassette('contacts_all') do - described_class.all.each do |cont| - expect(cont).to be_a described_class - end - end - end - end - - describe '::create' do - context 'given an name and a email' do - let(:name) { 'Letz' } - let(:email) { 'letzdevelopment@gmail.com' } - - it 'returns a contact' do - VCR.use_cassette('contacts_create') do - expect(described_class.create name, email).to be_a described_class - end - end - end - end - - describe '#destroy' do - context 'given a contact' do - let(:contact) { build(:contact) } - - it 'removes a contact' do - VCR.use_cassette('contacts_remove') do - expect(contact.destroy).to be_truthy - end - end - end - end - - describe '#save' do - let(:contact) { build(:contact) } - before { contact.name = 'Another name' } - - it 'updates the name of the contact' do - VCR.use_cassette('contacts_update') do - expect(contact.save).to be_truthy - end - end - end -end diff --git a/spec/pushbullet/devices_spec.rb b/spec/pushbullet/devices_spec.rb deleted file mode 100644 index bbb47c0..0000000 --- a/spec/pushbullet/devices_spec.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'spec_helper' - -describe Pushbullet::Device do - describe '::all' do - it 'returns all devices' do - VCR.use_cassette('devices_all') do - expect(described_class.all).to be_a Array - end - end - - it 'returns iterable of Devices' do - VCR.use_cassette('devices_all') do - described_class.all.each do |cont| - expect(cont).to be_a described_class - end - end - end - end - - describe '::create' do - context 'given an nickname and a type' do - let(:nickname) { 'Letz Device' } - let(:type) { 'stream' } - - it 'returns a device' do - VCR.use_cassette('devices_create') do - expect(described_class.create nickname, type).to be_a described_class - end - end - end - end - - describe '#destroy' do - context 'given a device' do - let(:device) { build(:device) } - - it 'removes a device' do - VCR.use_cassette('devices_remove') do - expect(device.destroy).to be_truthy - end - end - end - end - - describe '#save' do - let(:device) { build(:device) } - before { device.nickname = 'Another name' } - - it 'updates the name of the device' do - VCR.use_cassette('devices_update') do - expect(device.save).to be_truthy - end - end - end -end diff --git a/spec/pushbullet/push_spec.rb b/spec/pushbullet/push_spec.rb deleted file mode 100644 index 82b63c4..0000000 --- a/spec/pushbullet/push_spec.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'spec_helper' - -describe Pushbullet::Push do - describe '::create_note' do - context 'given an id, a title and a body' do - let(:id) { 'letzdevelopment@gmail.com' } - let(:title) { 'Le title' } - let(:body) { 'Le body' } - - it 'returns a Push' do - VCR.use_cassette('push_create_note') do - expect(described_class.create_note id, title, body).to be_a described_class - end - end - end - end - - describe '::create_link' do - - end - - describe '::create_address' do - - end - - describe '::create_list' -end From 6486f8f2f70d6a45a2b80ba9685c99a1c4858aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Leit=C3=A3o?= Date: Wed, 16 Dec 2015 00:11:52 +0000 Subject: [PATCH 3/3] Remove awesome_print from spec_helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo Leitão --- Gemfile.lock | 2 +- lib/pushbullet.rb | 1 + ruby-pushbullet.gemspec | 19 ++++++++++++++----- spec/pushbullet_spec.rb | 20 ++------------------ spec/spec_helper.rb | 5 +++-- 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index d2dc958..b5e0f29 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - ruby-pushbullet (0.1.4) + ruby-pushbullet (1.0.0) json rest-client (~> 1.8.0) diff --git a/lib/pushbullet.rb b/lib/pushbullet.rb index ba7bcf1..73f4805 100644 --- a/lib/pushbullet.rb +++ b/lib/pushbullet.rb @@ -5,6 +5,7 @@ # external require 'core_ext/string' # pushbullet +require 'pushbullet/version' require 'pushbullet/pushable' require 'pushbullet/configuration' require 'pushbullet/client' diff --git a/ruby-pushbullet.gemspec b/ruby-pushbullet.gemspec index 4e4bde1..c8ac3e7 100644 --- a/ruby-pushbullet.gemspec +++ b/ruby-pushbullet.gemspec @@ -1,15 +1,24 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'pushbullet/version' + Gem::Specification.new do |gem| - gem.name = %q{ruby-pushbullet} - gem.version = '0.1.4' + gem.name = 'ruby-pushbullet' + gem.version = Pushbullet::VERSION gem.authors = ['Ricardo Leitao'] gem.email = ['letzdevelopment@gmail.com'] + gem.licenses = ['MIT'] + gem.summary = %q{Ruby client of Pushbullet API.} gem.description = %q{Ruby client of Pushbullet API.} gem.homepage = %q{https://github.com/letz/ruby-pushbullet} - gem.license = 'MIT' - gem.files = Dir.glob("lib/**/*") + %w(README.md) - gem.test_files = Dir.glob("spec/**/*") + gem.files = `git ls-files -z`.split("\x0").reject do |f| + f.match(%r{^(test|spec|features)/}) + end + gem.bindir = 'exe' + gem.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } gem.require_paths = ['lib'] gem.add_development_dependency 'bundler' diff --git a/spec/pushbullet_spec.rb b/spec/pushbullet_spec.rb index 510c136..1a5b3e2 100644 --- a/spec/pushbullet_spec.rb +++ b/spec/pushbullet_spec.rb @@ -1,23 +1,7 @@ require 'spec_helper' describe Pushbullet do - describe '::api_token=' do - before { described_class.api_token = nil } - after { described_class.api_token = 'DhOX5Q8Fps9mq4g90yfrfioRPyo1qQRd' } - context 'given an api_token' do - let(:token) { 'nice_try' } - - it 'sets the Pushbullet api token' do - expect { - described_class.api_token = token - }.to change { described_class.api_token }.from(nil).to(token) - end - end - end - - describe '::client' do - it 'returns a Pushbullet::Client' do - expect(described_class.client).to be_a Pushbullet::Client - end + it 'has a version number' do + expect(described_class::VERSION).not_to be_nil end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 04bf080..63ff0ec 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,7 +4,6 @@ puts 'required simplecov' end require 'webmock/rspec' -require 'awesome_print' require 'factory_girl' require 'vcr' require 'support/factories' @@ -30,4 +29,6 @@ Dir['./spec/shared/**/*.rb'].each { |f| require f } end -Pushbullet.api_token = 'DhOX5Q8Fps9mq4g90yfrfioRPyo1qQRd' +Pushbullet.configure do |config| + config.api_token = 'DhOX5Q8Fps9mq4g90yfrfioRPyo1qQRd' +end