diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..37c8190 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,157 @@ +inherit_from: .rubocop_todo.yml + +AllCops: + TargetRubyVersion: 2.5.8 + +Gemspec/OrderedDependencies: + Enabled: true + +Gemspec/DuplicatedAssignment: + Enabled: true + +Gemspec/RubyVersionGlobalsUsage: + Enabled: true + +Layout/ArgumentAlignment: + EnforcedStyle: with_first_argument + Enabled: true + +Layout/EmptyLineAfterMagicComment: + Enabled: true + +Layout/EmptyLineBetweenDefs: + AllowAdjacentOneLineDefs: false + Enabled: true + +Layout/EmptyLines: + Enabled: true + +Layout/EmptyLinesAroundBlockBody: + EnforcedStyle: no_empty_lines + Enabled: true + +Layout/EmptyLinesAroundModuleBody: + EnforcedStyle: no_empty_lines + Enabled: true + +Layout/FirstArgumentIndentation: + Enabled: true + +Layout/SpaceAfterComma: + Enabled: true + +Layout/SpaceAroundOperators: + AllowForAlignment: true + EnforcedStyleForExponentOperator: no_space + Enabled: true + +Layout/SpaceBeforeBlockBraces: + EnforcedStyle: space + EnforcedStyleForEmptyBraces: space + Enabled: true + +Layout/SpaceBeforeFirstArg: + AllowForAlignment: true + Enabled: true + +Layout/SpaceInsideBlockBraces: + EnforcedStyle: space + EnforcedStyleForEmptyBraces: no_space + SpaceBeforeBlockParameters: true + Enabled: true + +Layout/SpaceInsideHashLiteralBraces: + EnforcedStyle: compact + EnforcedStyleForEmptyBraces: no_space + Enabled: true + +Layout/TrailingEmptyLines: + EnforcedStyle: final_newline + Enabled: true + +Layout/TrailingWhitespace: + AllowInHeredoc: false + Enabled: true + +Layout/IndentationWidth: + Enabled: true + +Layout/LeadingCommentSpace: + Enabled: true + +Layout/LineLength: + Enabled: true + Max: 168 + +Layout/RescueEnsureAlignment: + Enabled: true + +Lint/UnusedBlockArgument: + Enabled: true + +Metrics/AbcSize: + Enabled: true + Max: 17 + +Metrics/BlockLength: + Enabled: true + Max: 272 + +Naming/AccessorMethodName: + Enabled: false + +Style/BlockDelimiters: + Enabled: true + +Style/FrozenStringLiteralComment: + Enabled: true + +Style/Lambda: + EnforcedStyle: line_count_dependent + Enabled: true + +Style/StringLiterals: + EnforcedStyle: single_quotes + Enabled: true + +Style/ClassAndModuleChildren: + Enabled: false + +Style/Documentation: + Enabled: false + +Style/RedundantInterpolation: + Enabled: false + +Style/RedundantParentheses: + Enabled: true + +Style/RedundantPercentQ: + Enabled: true + +Style/RedundantReturn: + Enabled: true + +Style/RedundantSelf: + Enabled: true + +Style/MutableConstant: + EnforcedStyle: literals + Enabled: true + +Style/PercentLiteralDelimiters: + Enabled: true + +Style/PreferredHashMethods: + EnforcedStyle: short + Enabled: true + +Style/RaiseArgs: + Enabled: true + EnforcedStyle: compact + +Style/Encoding: + Enabled: true + +Style/ExpandPathArguments: + Enabled: true \ No newline at end of file diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..50f1b1d --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,8 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2020-06-24 06:31:38 +0300 using RuboCop version 0.85.1. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + \ No newline at end of file diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..ecd7ee5 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.5.8 diff --git a/Gemfile b/Gemfile index 9fbc3b4..1c4b182 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,6 @@ +# frozen_string_literal: true + source 'https://rubygems.org' # Specify your gem's dependencies in beyonic.gemspec -gemspec \ No newline at end of file +gemspec diff --git a/Rakefile b/Rakefile index 809eb56..7398a90 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,3 @@ -require "bundler/gem_tasks" +# frozen_string_literal: true +require 'bundler/gem_tasks' diff --git a/beyonic.gemspec b/beyonic.gemspec index eb53bdf..e264fac 100644 --- a/beyonic.gemspec +++ b/beyonic.gemspec @@ -1,32 +1,35 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'beyonic/version' Gem::Specification.new do |spec| - spec.name = "beyonic" + spec.name = 'beyonic' spec.version = Beyonic::VERSION + spec.required_ruby_version = '2.5.8' spec.authors = ['Oleg German', 'Luke Kyohere'] spec.email = ['oleg.german@gmail.com', 'luke@beyonic.com'] + spec.summary = %q{Ruby library for the onafriq.com api} spec.description = %q{onafriq.com makes enterprise payments to mobile easy. Details: http://onafriq.com} spec.homepage = "http://support.onafriq.com/api/" spec.license = "MIT" - spec.files = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md"] + spec.files = Dir['{lib}/**/*.rb', 'bin/*', 'LICENSE', '*.md'] spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] - spec.add_runtime_dependency "rest-client" - spec.add_runtime_dependency "oj", "~> 2.11" - spec.add_runtime_dependency "addressable" + spec.add_runtime_dependency 'addressable', '~> 2.7' + spec.add_runtime_dependency 'oj', '~> 2.11' + spec.add_runtime_dependency 'rest-client', '~> 2.1.0' - spec.add_development_dependency "bundler", "~> 1.7" - spec.add_development_dependency "rake", "~> 12.3.3" - spec.add_development_dependency "rspec" - spec.add_development_dependency "rspec-collection_matchers" - spec.add_development_dependency "webmock" - spec.add_development_dependency "simplecov" - spec.add_development_dependency "vcr" + spec.add_development_dependency 'bundler', '~> 2.1.4' + spec.add_development_dependency 'rake', '~> 13.0.1' + spec.add_development_dependency 'rspec', '~> 3.9' + spec.add_development_dependency 'rspec-collection_matchers', '~> 1.2.0' + spec.add_development_dependency 'simplecov', '~> 0.18.5' + spec.add_development_dependency 'vcr', '~> 6.0.0' + spec.add_development_dependency 'webmock', '~> 3.8.3' end diff --git a/lib/beyonic.rb b/lib/beyonic.rb index e25f672..b2768cb 100644 --- a/lib/beyonic.rb +++ b/lib/beyonic.rb @@ -1,7 +1,8 @@ -module Beyonic +# frozen_string_literal: true - require "rest-client" - require "oj" +module Beyonic + require 'rest-client' + require 'oj' # Uncomment this if you are testing on your server and you want to bypass SSL checks. # OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE @@ -25,17 +26,16 @@ def self.api_version def self.endpoint_base "https://api.onafriq.com/api/" end - end -require "beyonic/version" -require "beyonic/abstract_api" -require "beyonic/payment" -require "beyonic/webhook" -require "beyonic/collection" -require "beyonic/collection_request" -require "beyonic/account" -require "beyonic/contact" -require "beyonic/transaction" -require "beyonic/currency" -require "beyonic/network" +require 'beyonic/version' +require 'beyonic/abstract_api' +require 'beyonic/payment' +require 'beyonic/webhook' +require 'beyonic/collection' +require 'beyonic/collection_request' +require 'beyonic/account' +require 'beyonic/contact' +require 'beyonic/transaction' +require 'beyonic/currency' +require 'beyonic/network' diff --git a/lib/beyonic/abstract_api.rb b/lib/beyonic/abstract_api.rb index f4eaa24..4796e74 100644 --- a/lib/beyonic/abstract_api.rb +++ b/lib/beyonic/abstract_api.rb @@ -1,72 +1,71 @@ +# frozen_string_literal: true + module Beyonic::AbstractApi class ApiError < StandardError end module ClassMethods - def set_endpoint_resource(resource) @endpoint_url = Beyonic.endpoint_base + resource end def create(payload = {}, header_overrides = {}) # transform metadata from hash notation to dot notation - if (payload.has_key?:metadata) && (!payload[:metadata].empty?) + if (payload.key? :metadata) && !payload[:metadata].empty? payload[:metadata].each do |key, value| payload["metadata.#{key}"] = value end - payload.delete:metadata + payload.delete :metadata end resp = RestClient.post(@endpoint_url, payload, headers(header_overrides)) - self.new(Oj.load(resp)) + new(Oj.load(resp)) rescue RestClient::BadRequest => e raise ApiError.new(Oj.load(e.response.body)) end def list(payload = {}) # Turn payload into query parameters - require "addressable/uri" + require 'addressable/uri' uri = Addressable::URI.new uri.query_values = payload resp = RestClient.get(@endpoint_url + '?' + uri.query, headers) - ret = self.new(Oj.load(resp)) - ret.results = ret.results.map { |obj_attrs| self.new(obj_attrs)} - return ret + ret = new(Oj.load(resp)) + ret.results = ret.results.map { |obj_attrs| new(obj_attrs) } + ret end def get(id) resp = RestClient.get("#{@endpoint_url}/#{id}", headers) - self.new(Oj.load(resp)) + new(Oj.load(resp)) end def update(id, payload, header_overrides = {}) resp = RestClient.patch("#{@endpoint_url}/#{id}", payload, headers(header_overrides)) - self.new(Oj.load(resp)) - rescue RestClient::BadRequest => e - raise ApiError.new(Oj.load(e.response.body)) + new(Oj.load(resp)) + rescue RestClient::BadRequest => e + raise ApiError.new(Oj.load(e.response.body)) end def delete(id) resp = RestClient.delete("#{@endpoint_url}/#{id}", headers) - return true if resp == "" + return true if resp == '' end private def headers(header_overrides = {}) headers_hash = {} - headers_hash.merge!({"Authorization" => "Token #{Beyonic.api_key}"}) if Beyonic.api_key - headers_hash.merge!({"Beyonic-Version" => Beyonic.api_version}) if Beyonic.api_version - headers_hash.merge!({"Beyonic-Client" => "Ruby"}) - headers_hash.merge!({"Beyonic-Client-Version" => Beyonic::VERSION}) + headers_hash.merge!({ 'Authorization' => "Token #{Beyonic.api_key}" }) if Beyonic.api_key + headers_hash.merge!({ 'Beyonic-Version' => Beyonic.api_version }) if Beyonic.api_version + headers_hash.merge!({ 'Beyonic-Client' => 'Ruby' }) + headers_hash.merge!({ 'Beyonic-Client-Version' => Beyonic::VERSION }) headers_hash.merge!(header_overrides) headers_hash end - end module InstanceMethods - def save if respond_to?(:id) && !id.nil? self.class.update(id, to_h) @@ -75,22 +74,20 @@ def save end end - def []=(name, value) if name.to_sym == :id - self.id=(value) + self.id = value else - super(name,value) + super(name, value) end end - end module Initializer def initialize(*args) super(*args) - #We should define it after Object initialization - define_singleton_method(:id=) do |val| + # We should define it after Object initialization + define_singleton_method(:id=) do |_val| raise "Can't change id of existing #{self.class}" end end @@ -101,5 +98,4 @@ def self.included(receiver) receiver.send :include, InstanceMethods receiver.send :prepend, Initializer end - end diff --git a/lib/beyonic/account.rb b/lib/beyonic/account.rb index 4511e15..7dfcdd7 100644 --- a/lib/beyonic/account.rb +++ b/lib/beyonic/account.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::Account < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "accounts" + set_endpoint_resource 'accounts' end diff --git a/lib/beyonic/collection.rb b/lib/beyonic/collection.rb index a7d8d53..d663136 100644 --- a/lib/beyonic/collection.rb +++ b/lib/beyonic/collection.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::Collection < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "collections" + set_endpoint_resource 'collections' end diff --git a/lib/beyonic/collection_request.rb b/lib/beyonic/collection_request.rb index 73e9ea9..812529d 100644 --- a/lib/beyonic/collection_request.rb +++ b/lib/beyonic/collection_request.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::CollectionRequest < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "collectionrequests" -end \ No newline at end of file + set_endpoint_resource 'collectionrequests' +end diff --git a/lib/beyonic/contact.rb b/lib/beyonic/contact.rb index 99a06e0..e41f6bd 100644 --- a/lib/beyonic/contact.rb +++ b/lib/beyonic/contact.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::Contact < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "contacts" -end \ No newline at end of file + set_endpoint_resource 'contacts' +end diff --git a/lib/beyonic/currency.rb b/lib/beyonic/currency.rb index 6ca359b..12f8702 100644 --- a/lib/beyonic/currency.rb +++ b/lib/beyonic/currency.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::Currency < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "currencies" + set_endpoint_resource 'currencies' end diff --git a/lib/beyonic/network.rb b/lib/beyonic/network.rb index 56b5a3a..43a1dfb 100644 --- a/lib/beyonic/network.rb +++ b/lib/beyonic/network.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::Network < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "networks" + set_endpoint_resource 'networks' end diff --git a/lib/beyonic/payment.rb b/lib/beyonic/payment.rb index 580475f..0f365ac 100644 --- a/lib/beyonic/payment.rb +++ b/lib/beyonic/payment.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::Payment < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "payments" -end \ No newline at end of file + set_endpoint_resource 'payments' +end diff --git a/lib/beyonic/transaction.rb b/lib/beyonic/transaction.rb index bed5873..80f9252 100644 --- a/lib/beyonic/transaction.rb +++ b/lib/beyonic/transaction.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::Transaction < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "transactions" + set_endpoint_resource 'transactions' end diff --git a/lib/beyonic/version.rb b/lib/beyonic/version.rb index d210918..54b4cad 100644 --- a/lib/beyonic/version.rb +++ b/lib/beyonic/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Beyonic - VERSION = "0.0.15" + VERSION = '0.0.15' end diff --git a/lib/beyonic/webhook.rb b/lib/beyonic/webhook.rb index 5c71481..f1361d2 100644 --- a/lib/beyonic/webhook.rb +++ b/lib/beyonic/webhook.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'ostruct' class Beyonic::Webhook < OpenStruct include Beyonic::AbstractApi - set_endpoint_resource "webhooks" -end \ No newline at end of file + set_endpoint_resource 'webhooks' +end diff --git a/spec/beyonic/accounts_spec.rb b/spec/beyonic/accounts_spec.rb index d7c9d75..8cb4717 100644 --- a/spec/beyonic/accounts_spec.rb +++ b/spec/beyonic/accounts_spec.rb @@ -1,25 +1,27 @@ +# frozen_string_literal: true + require 'spec_helper' describe Beyonic::Account do - before { - Beyonic.api_key = "d349087313cc7a6627d77ab61163d4dab6449b4c" - Beyonic.api_version = "v1" - API_ENDPOINT = "https://staging.beyonic.com/api/accounts" + before do + Beyonic.api_key = 'd349087313cc7a6627d77ab61163d4dab6449b4c' + Beyonic.api_version = 'v1' + API_ENDPOINT = 'https://staging.beyonic.com/api/accounts' Beyonic::Account.instance_variable_set(:@endpoint_url, API_ENDPOINT) - } + end - describe ".list" do + describe '.list' do context 'Success response' do - subject { + subject do VCR.use_cassette('accounts_list') do Beyonic::Account.list end - } + end it { is_expected.to have_requested(:get, "#{API_ENDPOINT}").with( - headers: {"Authorization" => "Token #{Beyonic.api_key}", "Beyonic-Version" => Beyonic.api_version} - ) + headers: { 'Authorization' => "Token #{Beyonic.api_key}", 'Beyonic-Version' => Beyonic.api_version } + ) } it { is_expected.to be_an(Array) } @@ -27,17 +29,17 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('accounts_invalid_token_list') do Beyonic::Account.list end } - } + end it { is_expected.to raise_error diff --git a/spec/beyonic/collection_request_spec.rb b/spec/beyonic/collection_request_spec.rb index f6983c1..616b77f 100644 --- a/spec/beyonic/collection_request_spec.rb +++ b/spec/beyonic/collection_request_spec.rb @@ -1,84 +1,85 @@ +# frozen_string_literal: true + describe Beyonic::CollectionRequest do - before { - Beyonic.api_key = "d349087313cc7a6627d77ab61163d4dab6449b4c" - Beyonic.api_version = "v1" - Beyonic::CollectionRequest.instance_variable_set(:@endpoint_url, "https://staging.beyonic.com/api/collectionrequests") - } + before do + Beyonic.api_key = 'd349087313cc7a6627d77ab61163d4dab6449b4c' + Beyonic.api_version = 'v1' + Beyonic::CollectionRequest.instance_variable_set(:@endpoint_url, 'https://staging.beyonic.com/api/collectionrequests') + end - let(:payload) { + let(:payload) do { phonenumber: '+256772781923', currency: 'UGX', amount: 3000 } - } + end - let!(:create_collection_requests) { + let!(:create_collection_requests) do VCR.use_cassette('collection_requests_create') do Beyonic::CollectionRequest.create(payload) end - } - - describe ".crate" do + end + + describe '.crate' do context 'Success response' do - subject { + subject do create_collection_requests - } + end - it { - is_expected.to have_requested(:post, "https://staging.beyonic.com/api/collectionrequests").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:post, 'https://staging.beyonic.com/api/collectionrequests').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::CollectionRequest) } - it { is_expected.to have_attributes(id: create_collection_requests.id, currency: "UGX") } + it { is_expected.to have_attributes(id: create_collection_requests.id, currency: 'UGX') } end context 'Bad request' do - subject { - -> { + subject do + lambda { VCR.use_cassette('collection_requests_invalid_create') do Beyonic::CollectionRequest.create(invalid_payload: true) end } - } - it { + end + it { is_expected.to raise_error(Beyonic::AbstractApi::ApiError) } end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('collection_requests_invalid_token_create') do Beyonic::CollectionRequest.create(payload) end } - } - it { + end + it { is_expected.to raise_error } end - end - describe ".list" do + describe '.list' do context 'Success response' do - subject { + subject do VCR.use_cassette('collection_requests_list') do Beyonic::CollectionRequest.list end - } + end - it { - is_expected.to have_requested(:get, "https://staging.beyonic.com/api/collectionrequests").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:get, 'https://staging.beyonic.com/api/collectionrequests').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Array) } @@ -86,69 +87,69 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('collection_requests_invalid_token_list') do Beyonic::CollectionRequest.list end } - } + end - it { + it { is_expected.to raise_error } end end - describe ".get" do + describe '.get' do context 'Success response' do - subject { + subject do VCR.use_cassette('collection_requests_get') do Beyonic::CollectionRequest.get(create_collection_requests.id) end - } + end - it { + it { is_expected.to have_requested(:get, "https://staging.beyonic.com/api/collectionrequests/#{create_collection_requests.id}").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::CollectionRequest) } - it { is_expected.to have_attributes(id: create_collection_requests.id, currency: "UGX") } + it { is_expected.to have_attributes(id: create_collection_requests.id, currency: 'UGX') } end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('collection_requests_invalid_token_get') do Beyonic::CollectionRequest.get(create_collection_requests.id) end } - } + end - it { + it { is_expected.to raise_error } end context 'Unauthorized' do - subject { - -> { + subject do + lambda { VCR.use_cassette('collection_requests_no_permissions_get') do Beyonic::CollectionRequest.get(666) end } - } - it { + end + it { is_expected.to raise_error } end diff --git a/spec/beyonic/collection_spec.rb b/spec/beyonic/collection_spec.rb index 65418ec..bdcbd22 100644 --- a/spec/beyonic/collection_spec.rb +++ b/spec/beyonic/collection_spec.rb @@ -1,25 +1,26 @@ +# frozen_string_literal: true + require 'spec_helper' describe Beyonic::Collection do - before { - Beyonic.api_key = "d349087313cc7a6627d77ab61163d4dab6449b4c" - Beyonic.api_version = "v1" - Beyonic::Collection.instance_variable_set(:@endpoint_url, "https://staging.beyonic.com/api/collections") - } - - - describe ".list" do + before do + Beyonic.api_key = 'd349087313cc7a6627d77ab61163d4dab6449b4c' + Beyonic.api_version = 'v1' + Beyonic::Collection.instance_variable_set(:@endpoint_url, 'https://staging.beyonic.com/api/collections') + end + + describe '.list' do context 'Success response' do - subject { + subject do VCR.use_cassette('collections_list') do Beyonic::Collection.list end - } + end - it { - is_expected.to have_requested(:get, "https://staging.beyonic.com/api/collections").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:get, 'https://staging.beyonic.com/api/collections').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Array) } it { is_expected.to have(1).items } @@ -28,16 +29,16 @@ end context 'Phonenumber search' do - subject { + subject do VCR.use_cassette('collections_search') do Beyonic::Collection.list(phonenumber: '+254727843600') end - } + end - it { - is_expected.to have_requested(:get, "https://staging.beyonic.com/api/collections?phonenumber=%2B254727843600").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:get, 'https://staging.beyonic.com/api/collections?phonenumber=%2B254727843600').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Array) } @@ -47,36 +48,36 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('collections_invalid_token_list') do Beyonic::Collection.list end } - } + end - it { + it { is_expected.to raise_error } end end - describe ".get" do + describe '.get' do context 'Success response' do - subject { + subject do VCR.use_cassette('collections_get') do Beyonic::Collection.get(1) end - } + end - it { - is_expected.to have_requested(:get, "https://staging.beyonic.com/api/collections/1").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:get, 'https://staging.beyonic.com/api/collections/1').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::Collection) } @@ -84,50 +85,49 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('collections_invalid_token_get') do Beyonic::Collection.get(1) end } - } + end - it { + it { is_expected.to raise_error } end context 'Unauthorized' do - subject { - -> { + subject do + lambda { VCR.use_cassette('collections_no_permissions_get') do Beyonic::Collection.get(666) end } - } - it { + end + it { is_expected.to raise_error } end end - describe ".claim" do - + describe '.claim' do context 'by Phonenumber' do - subject { + subject do VCR.use_cassette('collections_claim') do Beyonic::Collection.claim(200, '+254727843600', nil) end - } + end - it { - is_expected.to have_requested(:get, "https://staging.beyonic.com/api/collections?amount=200&claim=true&phonenumber=%2B254727843600&remote_transaction_id").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:get, 'https://staging.beyonic.com/api/collections?amount=200&claim=true&phonenumber=%2B254727843600&remote_transaction_id').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Array) } @@ -137,19 +137,19 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('collections_invalid_token_claim') do Beyonic::Collection.claim(200, '+254727843600', nil) end } - } + end - it { + it { is_expected.to raise_error } end diff --git a/spec/beyonic/metadata_spec.rb b/spec/beyonic/metadata_spec.rb index 02ac126..55108b0 100644 --- a/spec/beyonic/metadata_spec.rb +++ b/spec/beyonic/metadata_spec.rb @@ -1,71 +1,71 @@ +# frozen_string_literal: true + require 'spec_helper' describe Beyonic::CollectionRequest do - before { - Beyonic.api_key = "c4269a8029e8a7c5659d3cb7829ce1d7319b4689" - Beyonic.api_version = "v1" - Beyonic::CollectionRequest.instance_variable_set(:@endpoint_url, "https://staging.beyonic.com/api/collectionrequests") - } + before do + Beyonic.api_key = 'c4269a8029e8a7c5659d3cb7829ce1d7319b4689' + Beyonic.api_version = 'v1' + Beyonic::CollectionRequest.instance_variable_set(:@endpoint_url, 'https://staging.beyonic.com/api/collectionrequests') + end - let(:array_notation_payload) { + let(:array_notation_payload) do { phonenumber: '+40715441309', currency: 'BXC', amount: 3000, - metadata: {"my_id": "123ASDAsd123"} + metadata: { "my_id": '123ASDAsd123' } } - } + end - let(:dot_notation_payload) { + let(:dot_notation_payload) do { phonenumber: '+40715441309', currency: 'BXC', amount: 3000, - 'metadata.my_id': "123ASDAsd123" + 'metadata.my_id': '123ASDAsd123' } - } + end - describe ".create_array_notation" do - let!(:metadata_array_notation_request) { + describe '.create_array_notation' do + let!(:metadata_array_notation_request) do VCR.use_cassette('metadata_create_requests_array_notation') do Beyonic::CollectionRequest.create(array_notation_payload) end - } + end context 'Success response' do - subject { + subject do metadata_array_notation_request - } + end it { - is_expected.to have_requested(:post, "https://staging.beyonic.com/api/collectionrequests").with( - headers: {"Authorization" => "Token c4269a8029e8a7c5659d3cb7829ce1d7319b4689", "Beyonic-Version" => "v1"} - ) + is_expected.to have_requested(:post, 'https://staging.beyonic.com/api/collectionrequests').with( + headers: { 'Authorization' => 'Token c4269a8029e8a7c5659d3cb7829ce1d7319b4689', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::CollectionRequest) } - it { is_expected.to have_attributes('metadata': metadata_array_notation_request.metadata, currency: "BXC") } + it { is_expected.to have_attributes('metadata': metadata_array_notation_request.metadata, currency: 'BXC') } end end - describe ".create_dot_notation" do - let!(:metadata_dot_notation_request) { + describe '.create_dot_notation' do + let!(:metadata_dot_notation_request) do VCR.use_cassette('metadata_create_requests_dot_notation') do Beyonic::CollectionRequest.create(dot_notation_payload) end - } + end context 'Success response' do - subject { + subject do metadata_dot_notation_request - } + end it { - is_expected.to have_requested(:post, "https://staging.beyonic.com/api/collectionrequests").with( - headers: {"Authorization" => "Token c4269a8029e8a7c5659d3cb7829ce1d7319b4689", "Beyonic-Version" => "v1"} - ) + is_expected.to have_requested(:post, 'https://staging.beyonic.com/api/collectionrequests').with( + headers: { 'Authorization' => 'Token c4269a8029e8a7c5659d3cb7829ce1d7319b4689', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::CollectionRequest) } - it { is_expected.to have_attributes('metadata': metadata_dot_notation_request.metadata, currency: "BXC") } + it { is_expected.to have_attributes('metadata': metadata_dot_notation_request.metadata, currency: 'BXC') } end end - - end diff --git a/spec/beyonic/payment_spec.rb b/spec/beyonic/payment_spec.rb index b2a31d8..cd86c50 100644 --- a/spec/beyonic/payment_spec.rb +++ b/spec/beyonic/payment_spec.rb @@ -1,40 +1,42 @@ +# frozen_string_literal: true + require 'spec_helper' describe Beyonic::Payment do - before { - Beyonic.api_key = "d349087313cc7a6627d77ab61163d4dab6449b4c" - Beyonic.api_version = "v1" - Beyonic::Payment.instance_variable_set(:@endpoint_url, "https://staging.beyonic.com/api/payments") - } + before do + Beyonic.api_key = 'd349087313cc7a6627d77ab61163d4dab6449b4c' + Beyonic.api_version = 'v1' + Beyonic::Payment.instance_variable_set(:@endpoint_url, 'https://staging.beyonic.com/api/payments') + end - let(:payload) { + let(:payload) do { - phonenumber: "+256773712831", - amount: "100.2", - currency: "UGX", - description: "Per diem payment", - payment_type: "money", - callback_url: "https://my.website/payments/callback", + phonenumber: '+256773712831', + amount: '100.2', + currency: 'UGX', + description: 'Per diem payment', + payment_type: 'money', + callback_url: 'https://my.website/payments/callback', metadata: "{'id': '1234', 'name': 'Lucy'}" } - } + end - let!(:create_payment) { + let!(:create_payment) do VCR.use_cassette('payments_create') do Beyonic::Payment.create(payload) end - } - - describe ".crate" do + end + + describe '.crate' do context 'Success response' do - subject { + subject do create_payment - } + end - it { - is_expected.to have_requested(:post, "https://staging.beyonic.com/api/payments").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:post, 'https://staging.beyonic.com/api/payments').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::Payment) } @@ -42,49 +44,48 @@ end context 'Bad request' do - subject { - -> { + subject do + lambda { VCR.use_cassette('payments_invalid_create') do Beyonic::Payment.create(invalid_payload: true) end } - } - it { + end + it { is_expected.to raise_error(Beyonic::AbstractApi::ApiError) } end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('payments_invalid_token_create') do Beyonic::Payment.create(payload) end } - } - it { + end + it { is_expected.to raise_error } end - end - describe ".list" do + describe '.list' do context 'Success response' do - subject { + subject do VCR.use_cassette('payments_list') do Beyonic::Payment.list end - } + end - it { - is_expected.to have_requested(:get, "https://staging.beyonic.com/api/payments").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:get, 'https://staging.beyonic.com/api/payments').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Array) } @@ -92,36 +93,36 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('payments_invalid_token_list') do Beyonic::Payment.list end } - } + end - it { + it { is_expected.to raise_error } end end - describe ".get" do + describe '.get' do context 'Success response' do - subject { + subject do VCR.use_cassette('payments_get') do Beyonic::Payment.get(create_payment.id) end - } + end - it { + it { is_expected.to have_requested(:get, "https://staging.beyonic.com/api/payments/#{create_payment.id}").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::Payment) } @@ -129,63 +130,62 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('payments_invalid_token_get') do Beyonic::Payment.get(create_payment.id) end } - } + end - it { + it { is_expected.to raise_error } end context 'Unauthorized' do - subject { - -> { + subject do + lambda { VCR.use_cassette('payments_no_permissions_get') do Beyonic::Payment.get(666) end } - } - it { + end + it { is_expected.to raise_error } end end - describe "#save" do + describe '#save' do context 'new object' do subject { Beyonic::Payment } - before { + before do allow(subject).to receive(:create) subject.new(payload).save - } + end - it { + it { is_expected.to have_received(:create).with(payload) } end context 'loaded object' do subject { Beyonic::Payment } - before { - + before do allow(subject).to receive(:update) - create_payment.description = "foo" + create_payment.description = 'foo' create_payment.save - } + end - it { - is_expected.to have_received(:update).with(create_payment.id, hash_including(description: "foo")) + it { + is_expected.to have_received(:update).with(create_payment.id, hash_including(description: 'foo')) } end end diff --git a/spec/beyonic/webhook_spec.rb b/spec/beyonic/webhook_spec.rb index 4cf9a3c..17ff2c8 100644 --- a/spec/beyonic/webhook_spec.rb +++ b/spec/beyonic/webhook_spec.rb @@ -1,35 +1,37 @@ +# frozen_string_literal: true + require 'spec_helper' describe Beyonic::Webhook do - before { - Beyonic.api_key = "d349087313cc7a6627d77ab61163d4dab6449b4c" - Beyonic.api_version = "v1" - Beyonic::Webhook.instance_variable_set(:@endpoint_url, "https://staging.beyonic.com/api/webhooks") - } + before do + Beyonic.api_key = 'd349087313cc7a6627d77ab61163d4dab6449b4c' + Beyonic.api_version = 'v1' + Beyonic::Webhook.instance_variable_set(:@endpoint_url, 'https://staging.beyonic.com/api/webhooks') + end - let(:payload) { + let(:payload) do { - event: "payment.status.changed", - target: "https://my.callback.url/" + event: 'payment.status.changed', + target: 'https://my.callback.url/' } - } + end - let!(:create_webhook) { + let!(:create_webhook) do VCR.use_cassette('webhooks_create') do Beyonic::Webhook.create(payload) end - } - - describe ".crate" do + end + + describe '.crate' do context 'Success response' do - subject { + subject do create_webhook - } + end - it { - is_expected.to have_requested(:post, "https://staging.beyonic.com/api/webhooks").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:post, 'https://staging.beyonic.com/api/webhooks').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::Webhook) } @@ -37,48 +39,47 @@ end context 'Bad request' do - subject { - -> { + subject do + lambda { VCR.use_cassette('webhooks_invalid_create') do Beyonic::Webhook.create(invalid_payload: true) end } - } - it { + end + it { is_expected.to raise_error(Beyonic::AbstractApi::ApiError) } end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } - subject { - -> { + before do + Beyonic.api_key = 'invalid_key' + end + subject do + lambda { VCR.use_cassette('webhooks_invalid_token_create') do Beyonic::Webhook.create(payload) end } - } - it { + end + it { is_expected.to raise_error } end - end - describe ".list" do + describe '.list' do context 'Success response' do - subject { + subject do VCR.use_cassette('webhooks_list') do Beyonic::Webhook.list end - } + end - it { - is_expected.to have_requested(:get, "https://staging.beyonic.com/api/webhooks").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + it { + is_expected.to have_requested(:get, 'https://staging.beyonic.com/api/webhooks').with( + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Array) } it { is_expected.to have(2).items } @@ -87,35 +88,35 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } - - subject { - -> { - VCR.use_cassette('webhooks_invalid_token_list') do - Beyonic::Webhook.list - end + before do + Beyonic.api_key = 'invalid_key' + end + + subject do + lambda { + VCR.use_cassette('webhooks_invalid_token_list') do + Beyonic::Webhook.list + end } - } - it { + end + it { is_expected.to raise_error } end end - describe ".get" do + describe '.get' do context 'Success response' do - subject { + subject do VCR.use_cassette('webhooks_get') do Beyonic::Webhook.get(create_webhook.id) end - } + end - it { + it { is_expected.to have_requested(:get, "https://staging.beyonic.com/api/webhooks/#{create_webhook.id}").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::Webhook) } @@ -123,196 +124,195 @@ end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('webhooks_invalid_token_get') do Beyonic::Webhook.get(create_webhook.id) end } - } - it { + end + it { is_expected.to raise_error } end context 'Forbidden' do - subject { - -> { + subject do + lambda { VCR.use_cassette('webhooks_no_permissions_get') do Beyonic::Webhook.get(666) end } - } - it { + end + it { is_expected.to raise_error } end end - describe ".update" do + describe '.update' do context 'Success response' do - subject { + subject do VCR.use_cassette('webhooks_update') do - Beyonic::Webhook.update(create_webhook.id, target: "https://my.callback2.url/") + Beyonic::Webhook.update(create_webhook.id, target: 'https://my.callback2.url/') end - } + end - it { + it { is_expected.to have_requested(:patch, "https://staging.beyonic.com/api/webhooks/#{create_webhook.id}").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_an(Beyonic::Webhook) } - it { is_expected.to have_attributes(id: create_webhook.id, target: "https://my.callback2.url/") } + it { is_expected.to have_attributes(id: create_webhook.id, target: 'https://my.callback2.url/') } end context 'Bad request' do - subject { - -> { + subject do + lambda { VCR.use_cassette('webhooks_invalid_update') do - Beyonic::Webhook.update(create_webhook.id, event: "wrongevent") + Beyonic::Webhook.update(create_webhook.id, event: 'wrongevent') end } - } - it { + end + it { is_expected.to raise_error(Beyonic::AbstractApi::ApiError) } end context 'Forbidden' do - subject { - -> { + subject do + lambda { VCR.use_cassette('webhooks_no_permissions_update') do - Beyonic::Webhook.update(666, target: "https://my.callback2.url/") + Beyonic::Webhook.update(666, target: 'https://my.callback2.url/') end } - } + end - it { + it { is_expected.to raise_error } end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" + before do + Beyonic.api_key = 'invalid_key' create_webhook - } + end - subject { - -> { + subject do + lambda { VCR.use_cassette('webhooks_invalid_token_update') do - Beyonic::Webhook.update(create_webhook.id, target: "https://my.callback2.url/") + Beyonic::Webhook.update(create_webhook.id, target: 'https://my.callback2.url/') end } - } - it { + end + it { is_expected.to raise_error } end end - describe "#save" do + describe '#save' do context 'new object' do subject { Beyonic::Webhook } - before { + before do allow(subject).to receive(:create) subject.new(payload).save - } + end - it { + it { is_expected.to have_received(:create).with(payload) } end context 'loaded object' do - subject { + subject do Beyonic::Webhook - } + end - before { + before do allow(subject).to receive(:update) - create_webhook.target = "https://google.com/" + create_webhook.target = 'https://google.com/' create_webhook.save - } + end - it { - is_expected.to have_received(:update).with(create_webhook.id, hash_including(target: "https://google.com/")) + it { + is_expected.to have_received(:update).with(create_webhook.id, hash_including(target: 'https://google.com/')) } end end - describe "#id=" do - it { - expect{ - create_webhook.id=(4) - }.to raise_error "Can't change id of existing Beyonic::Webhook" + describe '#id=' do + it { + expect do + create_webhook.id = 4 + end.to raise_error "Can't change id of existing Beyonic::Webhook" } it { - expect { - create_webhook[:id]=(4) - }.to raise_error "Can't change id of existing Beyonic::Webhook" + expect do + create_webhook[:id] = 4 + end.to raise_error "Can't change id of existing Beyonic::Webhook" } it { - expect { - create_webhook[:target]="foo" - }.to_not raise_error + expect do + create_webhook[:target] = 'foo' + end.to_not raise_error } end - describe ".delete" do + describe '.delete' do context 'Success response' do - subject { + subject do VCR.use_cassette('webhooks_delete') do Beyonic::Webhook.delete(create_webhook.id) end - } + end - it { + it { is_expected.to have_requested(:delete, "https://staging.beyonic.com/api/webhooks/#{create_webhook.id}").with( - headers: {"Authorization" => "Token d349087313cc7a6627d77ab61163d4dab6449b4c", "Beyonic-Version" => "v1"} - ) + headers: { 'Authorization' => 'Token d349087313cc7a6627d77ab61163d4dab6449b4c', 'Beyonic-Version' => 'v1' } + ) } it { is_expected.to be_truthy } - end context 'Forbidden' do - subject { - -> { + subject do + lambda { VCR.use_cassette('webhooks_no_permissions_delete') do Beyonic::Webhook.delete(666) end } - } - it { + end + it { is_expected.to raise_error } end context 'Unauthorized' do - before { - Beyonic.api_key = "invalid_key" - } + before do + Beyonic.api_key = 'invalid_key' + end - subject { - -> { + subject do + lambda { VCR.use_cassette('webhooks_invalid_token_delete') do Beyonic::Webhook.delete(create_webhook.id) end } - } - it { + end + it { is_expected.to raise_error } end end -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 58d7019..fe26036 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: true + require 'simplecov' -SimpleCov.start do - add_filter "/spec/" +SimpleCov.start do + add_filter '/spec/' end - require 'rubygems' require 'bundler/setup' @@ -33,7 +34,7 @@ c.cassette_library_dir = 'fixtures/vcr_cassettes' c.ignore_hosts '127.0.0.1', 'localhost' c.hook_into :webmock - c.before_record do |i| + c.before_record do |_i| OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE end end