mbsy 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55400f2d1afda10b24c6174770194ff84a3030ab
4
- data.tar.gz: c1c98554bbfe9a9597367d6b43d4cf36c0e0e731
3
+ metadata.gz: 4f1f8275167e9967a1cad50454e034743065d6c1
4
+ data.tar.gz: 1a9f59b52bb3f4c4bd378840077e2b7794726d62
5
5
  SHA512:
6
- metadata.gz: 953a40b70cb6fb328bc92c4549ea47dfd8b6b9ad2f8b6fd3c055469c21e0a88146334ab390fe60c37d1ed00d93bfcae56309728842907e55b09220ab6467d493
7
- data.tar.gz: 156977a400f638f63fc516f0e5ff56d3146cacaa0bb7208a4a9cfd23c056e9cf69d670b6d8ae177d11d2c4a994cae039687c0ab7e49cb4662c4ee727ed14b779
6
+ metadata.gz: d2e6c864174d070fc0cc532c8ff76f124aba6bcc6d460c21791ee08842bb81264fd05677b251bf2b5736f2e9f80890b327fc44000290c36f12a68f91eef18f02
7
+ data.tar.gz: 26777be50499700a53b554bbc30179e38277fb633a4ecda46146beceeeb557504578ab2d5e285cd0740a2e6291e750baa2b77cd8aada6059fbf360304e8d28e6
data/.gitignore CHANGED
@@ -12,6 +12,10 @@ doc
12
12
  # bundler
13
13
  .bundle
14
14
 
15
+ # ruby config
16
+ .ruby-version
17
+ .ruby-gemset
18
+
15
19
  # jeweler generated
16
20
  pkg
17
21
 
data/Gemfile CHANGED
@@ -1,3 +1,8 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gemspec
4
+
5
+ group :debug do
6
+ gem 'debugger', :platforms => [:mri_19]
7
+ gem 'byebug', :platforms => [:mri_20, :mri_21]
8
+ end
data/Gemfile.lock CHANGED
@@ -1,63 +1,71 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mbsy (1.0.1)
4
+ mbsy (1.0.4)
5
+ httparty (>= 0.8.3)
6
+ json (>= 1.7.3)
5
7
 
6
8
  GEM
7
- remote: http://rubygems.org/
9
+ remote: https://rubygems.org/
8
10
  specs:
9
- activesupport (3.2.6)
10
- i18n (~> 0.6)
11
- multi_json (~> 1.0)
12
- columnize (0.3.6)
13
- debugger (1.5.0)
11
+ activesupport (4.1.1)
12
+ i18n (~> 0.6, >= 0.6.9)
13
+ json (~> 1.7, >= 1.7.7)
14
+ minitest (~> 5.1)
15
+ thread_safe (~> 0.1)
16
+ tzinfo (~> 1.1)
17
+ addressable (2.3.6)
18
+ byebug (3.1.2)
19
+ columnize (~> 0.8)
20
+ debugger-linecache (~> 1.2)
21
+ columnize (0.8.9)
22
+ crack (0.4.2)
23
+ safe_yaml (~> 1.0.0)
24
+ debugger (1.6.6)
14
25
  columnize (>= 0.3.1)
15
26
  debugger-linecache (~> 1.2.0)
16
- debugger-ruby_core_source (~> 1.2.0)
27
+ debugger-ruby_core_source (~> 1.3.2)
17
28
  debugger-linecache (1.2.0)
18
- debugger-ruby_core_source (1.2.0)
19
- diff-lcs (1.1.3)
20
- factory_girl (2.1.2)
21
- activesupport
22
- faker (1.0.1)
23
- i18n (~> 0.4)
24
- fakeweb (1.3.0)
25
- httparty (0.8.3)
26
- multi_json (~> 1.0)
27
- multi_xml
28
- i18n (0.6.0)
29
- json (1.7.3)
30
- multi_json (1.3.6)
31
- multi_xml (0.5.1)
32
- rake (0.9.2.2)
33
- rb-fsevent (0.9.3)
34
- rspec (2.7.0)
35
- rspec-core (~> 2.7.0)
36
- rspec-expectations (~> 2.7.0)
37
- rspec-mocks (~> 2.7.0)
38
- rspec-core (2.7.1)
39
- rspec-expectations (2.7.0)
40
- diff-lcs (~> 1.1.2)
41
- rspec-mocks (2.7.0)
42
- shoulda (3.0.1)
43
- shoulda-context (~> 1.0.0)
44
- shoulda-matchers (~> 1.0.0)
45
- shoulda-context (1.0.0)
46
- shoulda-matchers (1.0.0)
29
+ debugger-ruby_core_source (1.3.4)
30
+ diff-lcs (1.2.5)
31
+ faker (1.3.0)
32
+ i18n (~> 0.5)
33
+ httparty (0.13.1)
34
+ json (~> 1.8)
35
+ multi_xml (>= 0.5.2)
36
+ i18n (0.6.9)
37
+ json (1.8.1)
38
+ minitest (5.3.3)
39
+ multi_xml (0.5.5)
40
+ rake (10.3.1)
41
+ rspec (2.14.1)
42
+ rspec-core (~> 2.14.0)
43
+ rspec-expectations (~> 2.14.0)
44
+ rspec-mocks (~> 2.14.0)
45
+ rspec-core (2.14.8)
46
+ rspec-expectations (2.14.5)
47
+ diff-lcs (>= 1.1.3, < 2.0)
48
+ rspec-mocks (2.14.6)
49
+ safe_yaml (1.0.3)
50
+ shoulda-matchers (2.6.1)
51
+ activesupport (>= 3.0.0)
52
+ thread_safe (0.3.3)
53
+ tzinfo (1.1.0)
54
+ thread_safe (~> 0.1)
55
+ webmock (1.17.4)
56
+ addressable (>= 2.2.7)
57
+ crack (>= 0.3.2)
47
58
 
48
59
  PLATFORMS
49
60
  ruby
50
61
 
51
62
  DEPENDENCIES
52
- bundler (~> 1.1.1)
63
+ activesupport (>= 3.2.18)
64
+ byebug
53
65
  debugger
54
- factory_girl (~> 2.1.0)
55
- faker (~> 1.0.1)
56
- fakeweb (~> 1.3.0)
57
- httparty (~> 0.8.3)
58
- json (~> 1.7.3)
66
+ faker (~> 1.3.0)
59
67
  mbsy!
60
- rake (~> 0.9.2)
61
- rb-fsevent (~> 0.9.3)
62
- rspec (~> 2.7.0)
63
- shoulda
68
+ rake (>= 0.9.2)
69
+ rspec (~> 2.14.1)
70
+ shoulda-matchers (= 2.6.1)
71
+ webmock (~> 1.17.4)
data/README.md CHANGED
@@ -1,55 +1,51 @@
1
- # mbsy
1
+ # mbsy [![Gem Version](https://badge.fury.io/rb/mbsy.svg)](http://badge.fury.io/rb/mbsy)
2
2
 
3
3
  Ambassador API wrapper for Ruby.
4
4
 
5
5
  This is a lightweight Ruby wrapper for the [Ambassador](http://getambassador.com) API.
6
6
 
7
- ### Installation
7
+ ## Installation
8
8
 
9
- This library can be installed as a gem. It is hosted on [Rubygems](https://rubygems.org/gems/mbsy).
9
+ Add this line to your application's Gemfile:
10
10
 
11
- You can install this library as a gem using the following command:
12
-
13
- $ gem install mbsy
14
-
15
- ### Usage
11
+ ```
12
+ gem 'mbsy'
13
+ ```
16
14
 
17
- Simply require this library before you use it:
15
+ And then execute:
18
16
 
19
- ```ruby
20
- require 'mbsy'
17
+ ```
18
+ $ bundle
21
19
  ```
22
20
 
23
- If you're using Rails 3.x, you could include this gem in your Gemfile.
21
+ Or install it yourself as:
24
22
 
25
- ```ruby
26
- gem 'mbsy'
27
23
  ```
28
-
29
- Install the gem using the following command:
30
- $ bundle install
24
+ $ gem install mbsy
25
+ ```
31
26
 
32
27
  If you're using Rails 2 you could include this gem in your configuration, i.e. in `environment.rb`
33
28
 
34
29
  ```ruby
35
30
  config.gem 'mbsy'
36
31
  ```
37
- ### Initialise
32
+
33
+ ## Usage
38
34
 
39
35
  Make sure you include your api credentials in an initialiser.
40
36
 
41
37
  ```ruby
42
38
  Mbsy.configure do |c|
43
- c.api_key = 'MY_API_KEY'
44
- c.user_name = 'MY_USER_NAME'
39
+ c.api_key = 'MY_API_KEY'
40
+ c.user_name = 'MY_USER_NAME'
45
41
  end
46
42
  ```
47
43
 
48
44
  The mbsy gem will give you access to classes such as:
49
45
 
50
- * `Mbsy::Company`
51
- * `Mbsy::Ambassador`
52
- * `Mbsy::Shortcode`
46
+ - `Mbsy::Company`
47
+ - `Mbsy::Ambassador`
48
+ - `Mbsy::Shortcode`
53
49
 
54
50
  Some examples:
55
51
 
@@ -80,26 +76,23 @@ Mbsy::Shortcode.find({ :short_code => YOURCAMPAIGNUID })
80
76
 
81
77
  # Shortcode in a sandbox campaign
82
78
  Mbsy::Shortcode.find({ :short_code => YOURCAMPAIGNUID, :sandbox => 1 })
83
-
84
-
85
79
  ```
86
80
 
87
- ### Contributing to mbsy
81
+ ## Contributing to mbsy
88
82
 
89
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
90
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
91
- * Fork the project.
92
- * Start a feature/bugfix branch.
93
- * Commit and push until you are happy with your contribution.
94
- * Make sure to add tests for it. This is important so we don't break it in a future version unintentionally.
95
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
83
+ - Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
84
+ - Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
85
+ - Fork the project.
86
+ - Start a feature/bugfix branch.
87
+ - Commit and push until you are happy with your contribution.
88
+ - Make sure to add tests for it. This is important so we don't break it in a future version unintentionally.
89
+ - Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
96
90
 
97
- ### To Do
91
+ ## To Do
98
92
 
99
- Finish of test suite.
93
+ - [ ] Finish off test suite.
100
94
 
101
- ### Copyright
95
+ ## Copyright
102
96
 
103
- Copyright (c) 2012 Tom Mullen. See LICENSE.txt for
104
- further details.
97
+ Copyright (c) 2014 ZFERRAL, INC. See LICENSE for further details.
105
98
 
data/Rakefile CHANGED
@@ -1,4 +1,6 @@
1
1
  # encoding: utf-8
2
- require 'bundler'
2
+ require "bundler/gem_tasks"
3
+
3
4
  require 'rspec/core/rake_task'
4
- RSpec::Core::RakeTask.new('spec')
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
@@ -1,19 +1,17 @@
1
1
  module Mbsy
2
2
  class Ambassador < Base
3
-
4
3
  def self.find(params={})
5
4
  raise ArgumentError, "You must include :email" unless params[:email]
6
5
  call('get', params)
7
6
  end
8
-
7
+
9
8
  def self.all(params={})
10
9
  call('all', params)
11
10
  end
12
-
11
+
13
12
  def self.stats(params={})
14
13
  raise ArgumentError, "You must include :email" unless params[:email]
15
14
  call('stats', params)
16
15
  end
17
-
18
16
  end
19
- end
17
+ end
@@ -1,16 +1,26 @@
1
1
  module Mbsy
2
2
  class Balance < Base
3
-
4
3
  def self.update(mode, params={})
5
- raise ArgumentError, "You must include :email" unless params[:email]
6
- raise ArgumentError, "You must include :amount" unless params[:amount]
7
4
  case mode
8
5
  when :add
9
- call('add', params)
6
+ add(params)
10
7
  when :deduct
11
- call('deduct', params)
8
+ deduct(params)
12
9
  end
13
10
  end
14
-
11
+
12
+ def self.add(params = {})
13
+ raise ArgumentError, "You must include :email" unless params[:email]
14
+ raise ArgumentError, "You must include :amount" unless params[:amount]
15
+
16
+ call('add', params)
17
+ end
18
+
19
+ def self.deduct(params = {})
20
+ raise ArgumentError, "You must include :email" unless params[:email]
21
+ raise ArgumentError, "You must include :amount" unless params[:amount]
22
+
23
+ call('deduct', params)
24
+ end
15
25
  end
16
- end
26
+ end
@@ -1,24 +1,37 @@
1
1
  module Mbsy
2
- class RecordNotFound < StandardError; end
3
- class BadResponse < StandardError; end
2
+ class MbsyError < StandardError; end
3
+ class BadRequestError < MbsyError; end
4
+ class UnauthorizedError < MbsyError; end
5
+ class RecordNotFound < MbsyError; end
6
+ class ServerError < MbsyError; end
7
+ class BadResponse < MbsyError; end
8
+
4
9
  class Base
5
10
  include HTTParty
6
11
  format :json
7
12
  default_timeout 30
8
13
  default_params :output => 'json'
9
-
14
+
10
15
  def self.element_name
11
16
  name.split(/::/).last.underscore
12
17
  end
18
+
19
+ def self.api_url(method)
20
+ Mbsy.site_uri + self.element_name + '/' + method
21
+ end
13
22
 
14
23
  def self.call(method, params = {})
15
- api_url = Mbsy.site_uri + self.element_name + '/' + method
16
-
17
- response = JSON.parse(self.get(api_url, :query => params).body)['response']
24
+ response = JSON.parse(self.get(api_url(method), :query => params).body)['response']
18
25
  case response['code']
19
- when "200" # Nothing to do here...
20
- when "404"
21
- raise RecordNotFound.new(response["errors"]["error"])
26
+ when '200' # Nothing to do here...
27
+ when '400'
28
+ raise BadRequestError.new(response['errors']['error'])
29
+ when '401'
30
+ raise UnauthorizedError.new(response['errors']['error'])
31
+ when '404'
32
+ raise RecordNotFound.new(response['errors']['error'])
33
+ when '500'
34
+ raise ServerError.new(response['errors']['error'])
22
35
  else
23
36
  raise BadResponse.new(response: response)
24
37
  end
@@ -26,4 +39,4 @@ module Mbsy
26
39
  response['data']
27
40
  end
28
41
  end
29
- end
42
+ end
@@ -1,10 +1,8 @@
1
1
  module Mbsy
2
2
  class Commission < Base
3
-
4
3
  def self.update(params={})
5
4
  raise ArgumentError, "You must include either :commission_id or :transaction_uid" unless params[:commission_id] or params[:transaction_uid]
6
5
  call('update', params)
7
6
  end
8
-
9
7
  end
10
- end
8
+ end
@@ -1,13 +1,11 @@
1
1
  module Mbsy
2
2
  class Company < Base
3
-
4
3
  def self.get_details
5
4
  call('get')
6
5
  end
7
-
6
+
8
7
  def self.stats
9
8
  call('stats')
10
9
  end
11
-
12
10
  end
13
- end
11
+ end
@@ -1,11 +1,9 @@
1
1
  module Mbsy
2
2
  class Event < Base
3
-
4
3
  def self.create(params={})
5
4
  raise ArgumentError, "You must include :campaign_uid" unless params[:campaign_uid]
6
5
  raise ArgumentError, "You must include :email" unless params[:email]
7
6
  call('record', params)
8
7
  end
9
-
10
8
  end
11
- end
9
+ end
@@ -1,14 +1,12 @@
1
1
  module Mbsy
2
2
  class Group < Base
3
-
4
3
  def self.find(params={})
5
4
  raise ArgumentError, "You must include :group_id" unless params[:group_id]
6
5
  call('get', params)
7
6
  end
8
-
7
+
9
8
  def self.all(params={})
10
9
  call('all', params)
11
10
  end
12
-
13
11
  end
14
12
  end
@@ -1,10 +1,8 @@
1
1
  module Mbsy
2
2
  class Shortcode < Base
3
-
4
3
  def self.find(params={})
5
4
  raise ArgumentError, "You must include :short_code" unless params[:short_code]
6
5
  call('get', params)
7
6
  end
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module Mbsy
2
2
  class Social < Base
3
-
4
3
  def self.find(params={})
5
4
  raise ArgumentError, "You must include :email" unless params[:email]
6
5
  call('addthis', params)
7
6
  end
8
-
9
7
  end
10
- end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Mbsy
2
+ VERSION = '1.1.0'
3
+ end
data/lib/mbsy.rb CHANGED
@@ -12,12 +12,18 @@ require 'mbsy/resources/social'
12
12
 
13
13
  module Mbsy
14
14
  class << self
15
+ BASE_URI = "https://getambassador.com/api/v2"
16
+
15
17
  attr_accessor :api_key, :user_name
18
+
19
+ def site_uri
20
+ raise 'Mbsy not configured!' if user_name.nil? || api_key.nil?
21
+
22
+ "#{BASE_URI}/#{user_name}/#{api_key}/json/"
23
+ end
24
+
16
25
  def configure
17
26
  yield self
18
- def self.site_uri
19
- "https://getambassador.com/api/v2/#{user_name}/#{api_key}/json/"
20
- end
21
27
  end
22
28
  end
23
29
  end
data/mbsy.gemspec CHANGED
@@ -1,14 +1,18 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mbsy/version'
5
+
1
6
  Gem::Specification.new do |s|
2
7
  s.name = 'mbsy'
3
- s.version = '1.0.6'
4
- s.date = '2014-05-14'
8
+ s.version = Mbsy::VERSION
5
9
  s.summary = 'A Ruby wrapper for the Ambassador API'
6
10
  s.description = ''
7
11
  s.author = 'Tom Mullen, Chase Lee'
8
12
  s.email = 'support@getambassador.com'
9
13
  s.homepage = 'http://github.com/GetAmbassador/mbsy'
10
14
  s.license = 'MIT'
11
-
15
+
12
16
  s.files = `git ls-files`.split("\n")
13
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
18
  s.require_paths = ["lib"]
@@ -18,13 +22,10 @@ Gem::Specification.new do |s|
18
22
  s.add_runtime_dependency('httparty', '>= 0.8.3')
19
23
  s.add_runtime_dependency('json', '>= 1.7.3')
20
24
 
21
- s.add_development_dependency('rake', '~> 0.9.2')
22
- s.add_development_dependency('rspec', '~> 2.7.0')
23
- s.add_development_dependency('factory_girl', '~> 2.1.0')
24
- s.add_development_dependency('fakeweb', '~> 1.3.0')
25
- s.add_development_dependency('faker', '~> 1.0.1')
26
- s.add_development_dependency('rb-fsevent', '~> 0.9.3')
27
- s.add_development_dependency("shoulda", ">= 0")
28
- s.add_development_dependency("bundler", "~> 1.1.1")
29
- s.add_development_dependency('debugger', '~> 1.5.0')
25
+ s.add_development_dependency('rake', '>= 0.9.2')
26
+ s.add_development_dependency('rspec', '~> 2.14.1')
27
+ s.add_development_dependency('webmock', '~> 1.17.4')
28
+ s.add_development_dependency('faker', '~> 1.3.0')
29
+ s.add_development_dependency('shoulda-matchers', '2.6.1')
30
+ s.add_development_dependency('activesupport', '>= 3.2.18')
30
31
  end
data/spec/mbsy_spec.rb CHANGED
@@ -2,15 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe Mbsy do
4
4
  context 'configuration setup' do
5
-
6
- it "builds the url correctly" do
5
+ after { configure_mbsy! }
6
+
7
+ it 'builds the url correctly' do
7
8
  expect {
8
9
  Mbsy.configure do |c|
9
10
  c.api_key = "API-123"
10
11
  c.user_name = "tester"
11
12
  end
12
- }.to change { Mbsy.site_uri.to_s }.to("https://getambassador.com/api/v2/tester/API-123/json/")
13
+ }.to change { Mbsy.site_uri.to_s }.to('https://getambassador.com/api/v2/tester/API-123/json/')
13
14
  end
14
-
15
15
  end
16
- end
16
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mbsy::Ambassador do
4
+ let(:response) { double(:response) }
5
+ before { allow(Mbsy::Ambassador).to receive(:call).and_return(response) }
6
+
7
+ describe '.find' do
8
+ it 'raises ArgumentError when missing email param' do
9
+ expect { Mbsy::Ambassador.find }.to raise_error(ArgumentError, 'You must include :email')
10
+ expect(Mbsy::Ambassador).to_not have_received(:call)
11
+ end
12
+
13
+ it 'calls #call' do
14
+ expect(Mbsy::Ambassador.find(email: 'a@example.com')).to eq response
15
+ expect(Mbsy::Ambassador).to have_received(:call).with('get', email: 'a@example.com')
16
+ end
17
+ end
18
+
19
+ describe '.all' do
20
+ it 'calls #call' do
21
+ expect(Mbsy::Ambassador.all(is_active: 1)).to eq response
22
+ expect(Mbsy::Ambassador).to have_received(:call).with('all', is_active: 1)
23
+ end
24
+ end
25
+
26
+ describe '.stats' do
27
+ it 'raises ArgumentError when missing email' do
28
+ expect { Mbsy::Ambassador.stats }.to raise_error(ArgumentError, 'You must include :email')
29
+ expect(Mbsy::Ambassador).to_not have_received(:call)
30
+ end
31
+
32
+ it 'calls #call' do
33
+ expect(Mbsy::Ambassador.stats(email: 'a@example.com')).to eq response
34
+ expect(Mbsy::Ambassador).to have_received(:call).with('stats', email: 'a@example.com')
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mbsy::Balance do
4
+ let(:response) { double(:response) }
5
+
6
+ describe '.add' do
7
+ before { allow(Mbsy::Balance).to receive(:call).and_return(response) }
8
+
9
+ it 'raises ArgumentError when missing email' do
10
+ expect { Mbsy::Balance.add }.to raise_error(ArgumentError, 'You must include :email')
11
+ expect(Mbsy::Balance).to_not have_received(:call)
12
+ end
13
+
14
+ it 'raises ArgumentError when missing amount' do
15
+ expect { Mbsy::Balance.add(email: 'a@example.com') }.to raise_error(ArgumentError, 'You must include :amount')
16
+ expect(Mbsy::Balance).to_not have_received(:call)
17
+ end
18
+
19
+ it 'calls #call' do
20
+ expect(Mbsy::Balance.add(email: 'a@example.com', amount: 1)).to eq response
21
+ expect(Mbsy::Balance).to have_received(:call).with('add', email: 'a@example.com', amount: 1)
22
+ end
23
+ end
24
+
25
+ describe '.deduct' do
26
+ before { allow(Mbsy::Balance).to receive(:call).and_return(response) }
27
+
28
+ it 'raises ArgumentError when missing email' do
29
+ expect { Mbsy::Balance.deduct }.to raise_error(ArgumentError, 'You must include :email')
30
+ expect(Mbsy::Balance).to_not have_received(:call)
31
+ end
32
+
33
+ it 'raises ArgumentError when missing amount' do
34
+ expect { Mbsy::Balance.deduct(email: 'a@example.com') }.to raise_error(ArgumentError, 'You must include :amount')
35
+ expect(Mbsy::Balance).to_not have_received(:call)
36
+ end
37
+
38
+ it 'calls #call' do
39
+ expect(Mbsy::Balance.deduct(email: 'a@example.com', amount: 1)).to eq response
40
+ expect(Mbsy::Balance).to have_received(:call).with('deduct', email: 'a@example.com', amount: 1)
41
+ end
42
+ end
43
+
44
+ describe '.update' do
45
+ let(:params) { double(:params) }
46
+ it 'calls .add' do
47
+ expect(Mbsy::Balance).to receive(:add).with(params).and_return(response)
48
+ expect(Mbsy::Balance.update(:add, params)).to eq response
49
+ end
50
+
51
+ it 'calls .deduct' do
52
+ expect(Mbsy::Balance).to receive(:deduct).with(params).and_return(response)
53
+ expect(Mbsy::Balance.update(:deduct, params)).to eq response
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mbsy::Base do
4
+ describe '.element_name' do
5
+ it 'returns element names' do
6
+ allow(Mbsy::Base).to receive(:name).and_return("Test::Namespace::ElementName")
7
+ expect(Mbsy::Base.element_name).to eql('element_name')
8
+ end
9
+ end
10
+
11
+ describe '.api_url' do
12
+ it 'returns a fully formed api url' do
13
+ allow(Mbsy::Base).to receive(:element_name).and_return('test_element')
14
+ expect(Mbsy::Base.api_url('get')).to eq "https://getambassador.com/api/v2/testuser/testapikey/json/test_element/get"
15
+ end
16
+ end
17
+
18
+ describe '.call' do
19
+ context 'with a 200 response' do
20
+ let(:response) do
21
+ {
22
+ response: {
23
+ code: '200',
24
+ message: 'OK',
25
+ data: {
26
+ test: {
27
+ foo: :bar
28
+ }
29
+ }
30
+ }
31
+ }.to_json
32
+ end
33
+
34
+ it 'returns response data' do
35
+ allow(Mbsy::Base).to receive(:element_name).and_return('test_element')
36
+ stub_request(:get, Mbsy::Base.api_url('get')).
37
+ with(query: {campaign_uid: 123, email: 'a@example.com', output: :json}).
38
+ to_return(body: response)
39
+ expect(Mbsy::Base.call('get', {campaign_uid: 123, email: 'a@example.com'})).
40
+ to eq({'test' => {'foo' => 'bar'}})
41
+ end
42
+ end
43
+
44
+ def error_response(code)
45
+ message = case code
46
+ when '400'
47
+ 'BAD REQUEST'
48
+ when '401'
49
+ 'UNAUTHORIZED'
50
+ when '404'
51
+ 'NOT FOUND'
52
+ when '500'
53
+ 'SERVER ERROR'
54
+ end
55
+
56
+ {
57
+ response: {
58
+ code: code,
59
+ message: message,
60
+ errors: {
61
+ error: [
62
+ 'boom.'
63
+ ]
64
+ }
65
+ }
66
+ }.to_json
67
+ end
68
+
69
+ context 'error responses' do
70
+ before do
71
+ stub_request(:get, Mbsy::Base.api_url('get')).
72
+ with(query: {output: :json}).
73
+ to_return(body: response)
74
+ end
75
+
76
+ context 'with a 400 response' do
77
+ let(:response) { error_response('400') }
78
+
79
+ it 'raises BadRequestError' do
80
+ expect { Mbsy::Base.call('get') }.to raise_error(Mbsy::BadRequestError)
81
+ end
82
+ end
83
+
84
+ context 'with a 401 response' do
85
+ let(:response) { error_response('401') }
86
+
87
+ it 'raises UnauthorizedError' do
88
+ expect { Mbsy::Base.call('get') }.to raise_error(Mbsy::UnauthorizedError)
89
+ end
90
+ end
91
+
92
+ context 'with a 404 response' do
93
+ let(:response) { error_response('404') }
94
+
95
+ it 'raises a RecordNotFound' do
96
+ expect { Mbsy::Base.call('get') }.to raise_error(Mbsy::RecordNotFound)
97
+ end
98
+ end
99
+
100
+ context 'with a 500 response' do
101
+ let(:response) { error_response('500') }
102
+
103
+ it 'raises ServerError' do
104
+ expect { Mbsy::Base.call('get') }.to raise_error(Mbsy::ServerError)
105
+ end
106
+ end
107
+
108
+ context 'with another response' do
109
+ let(:response) { error_response('300') }
110
+
111
+ it 'raises a BadResponse' do
112
+ expect { Mbsy::Base.call('get') }.to raise_error(Mbsy::BadResponse)
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,30 +1,31 @@
1
1
  # This file was generated by the `rspec --init` command. Conventionally, all
2
2
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
- # Require this file using `require "spec_helper.rb"` to ensure that it is only
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
4
  # loaded once.
5
5
  #
6
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
6
 
8
7
  require 'mbsy'
9
- require 'rspec'
10
- require 'fakeweb'
11
- require 'factory_girl'
12
- require 'faker'
13
- require 'factories'
14
-
15
- FakeWeb.allow_net_connect = false
8
+ require 'active_support/core_ext/string/inflections'
9
+ require 'webmock/rspec'
10
+ WebMock.disable_net_connect! allow_localhost: true
16
11
 
17
- Mbsy.configure do |c|
18
- c.api_key = 'test'
19
- c.user_name = 'test'
12
+ def configure_mbsy!
13
+ Mbsy.configure do |c|
14
+ c.api_key = 'testapikey'
15
+ c.user_name = 'testuser'
16
+ end
20
17
  end
18
+ configure_mbsy!
21
19
 
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
22
21
  RSpec.configure do |config|
23
22
  config.treat_symbols_as_metadata_keys_with_true_values = true
24
23
  config.run_all_when_everything_filtered = true
25
24
  config.filter_run :focus
26
- end
27
25
 
28
- def fake_domain
29
- "#{Mbsy.site_uri}/"
30
- end
26
+ # Run specs in random order to surface order dependencies. If you find an
27
+ # order dependency and want to debug it, you can fix the order by providing
28
+ # the seed, which is printed after each run.
29
+ # --seed 1234
30
+ config.order = 'random'
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Mullen, Chase Lee
@@ -42,14 +42,14 @@ dependencies:
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.9.2
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.9.2
55
55
  - !ruby/object:Gem::Dependency
@@ -58,30 +58,30 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 2.7.0
61
+ version: 2.14.1
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: 2.7.0
68
+ version: 2.14.1
69
69
  - !ruby/object:Gem::Dependency
70
- name: factory_girl
70
+ name: webmock
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ~>
74
74
  - !ruby/object:Gem::Version
75
- version: 2.1.0
75
+ version: 1.17.4
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ~>
81
81
  - !ruby/object:Gem::Version
82
- version: 2.1.0
82
+ version: 1.17.4
83
83
  - !ruby/object:Gem::Dependency
84
- name: fakeweb
84
+ name: faker
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ~>
@@ -95,75 +95,33 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: 1.3.0
97
97
  - !ruby/object:Gem::Dependency
98
- name: faker
98
+ name: shoulda-matchers
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ~>
101
+ - - '='
102
102
  - !ruby/object:Gem::Version
103
- version: 1.0.1
103
+ version: 2.6.1
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ~>
108
+ - - '='
109
109
  - !ruby/object:Gem::Version
110
- version: 1.0.1
110
+ version: 2.6.1
111
111
  - !ruby/object:Gem::Dependency
112
- name: rb-fsevent
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ~>
116
- - !ruby/object:Gem::Version
117
- version: 0.9.3
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ~>
123
- - !ruby/object:Gem::Version
124
- version: 0.9.3
125
- - !ruby/object:Gem::Dependency
126
- name: shoulda
112
+ name: activesupport
127
113
  requirement: !ruby/object:Gem::Requirement
128
114
  requirements:
129
115
  - - '>='
130
116
  - !ruby/object:Gem::Version
131
- version: '0'
117
+ version: 3.2.18
132
118
  type: :development
133
119
  prerelease: false
134
120
  version_requirements: !ruby/object:Gem::Requirement
135
121
  requirements:
136
122
  - - '>='
137
123
  - !ruby/object:Gem::Version
138
- version: '0'
139
- - !ruby/object:Gem::Dependency
140
- name: bundler
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ~>
144
- - !ruby/object:Gem::Version
145
- version: 1.1.1
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ~>
151
- - !ruby/object:Gem::Version
152
- version: 1.1.1
153
- - !ruby/object:Gem::Dependency
154
- name: debugger
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ~>
158
- - !ruby/object:Gem::Version
159
- version: 1.5.0
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ~>
165
- - !ruby/object:Gem::Version
166
- version: 1.5.0
124
+ version: 3.2.18
167
125
  description: ''
168
126
  email: support@getambassador.com
169
127
  executables: []
@@ -189,11 +147,12 @@ files:
189
147
  - lib/mbsy/resources/group.rb
190
148
  - lib/mbsy/resources/shortcode.rb
191
149
  - lib/mbsy/resources/social.rb
150
+ - lib/mbsy/version.rb
192
151
  - mbsy.gemspec
193
- - spec/ambassador_spec.rb
194
- - spec/base_spec.rb
195
- - spec/factories.rb
196
152
  - spec/mbsy_spec.rb
153
+ - spec/resources/ambassador_spec.rb
154
+ - spec/resources/balance_spec.rb
155
+ - spec/resources/base_spec.rb
197
156
  - spec/spec_helper.rb
198
157
  homepage: http://github.com/GetAmbassador/mbsy
199
158
  licenses:
@@ -220,8 +179,8 @@ signing_key:
220
179
  specification_version: 4
221
180
  summary: A Ruby wrapper for the Ambassador API
222
181
  test_files:
223
- - spec/ambassador_spec.rb
224
- - spec/base_spec.rb
225
- - spec/factories.rb
226
182
  - spec/mbsy_spec.rb
183
+ - spec/resources/ambassador_spec.rb
184
+ - spec/resources/balance_spec.rb
185
+ - spec/resources/base_spec.rb
227
186
  - spec/spec_helper.rb
@@ -1,57 +0,0 @@
1
- require 'spec_helper'
2
- require 'ruby-debug'
3
-
4
- describe Mbsy::Ambassador do
5
-
6
- let(:resource_prefix) { "#{fake_domain}/ambassadors" }
7
- before do
8
- @existing_ambassador_response = Mbsy::Ambassador.find(email: 'ambassador@mbsy.co', first_name: 'sigma')
9
- end
10
-
11
- context 'find existing ambassador' do
12
- subject { Mbsy::Ambassador.find(email: 'ambassador@mbsy.co') }
13
- pending 'is an instance of Mbsy::Ambassador' do
14
- ambassador.should be_instance_of(Mbsy::Ambassador)
15
- end
16
- it { should be_instance_of(Hash) }
17
- it { should have_key('ambassador') }
18
- it { should == @existing_ambassador_response }
19
-
20
- it 'should return an ambassador with the same email' do
21
- subject['ambassador']['email'].should == @existing_ambassador_response['ambassador']['email']
22
- end
23
- end
24
- context 'existing ambassador not found' do
25
- before do
26
- # Stub out creation call since this is hitting a live service
27
- FakeWeb.register_uri(:get, "#{resource_prefix}/get/", query: {email: 'new_ambassador@mbsy.co', first_name: 'tester'}, response: File.expand_path('spec/fixtures/ambassador_response_v2.json') )
28
- end
29
- pending 'creates the ambassador if you cannot find one' do
30
- # Need a proper stubbed response before this can work
31
- ambassador = Mbsy::Ambassador.find(email: 'new_ambassador@mbsy.co', first_name: 'tester')
32
- ambassador['ambassador']['first_name'].should eq('tester')
33
- end
34
- context 'failed lookup without auto-create' do
35
- it "should raise a RecordNotFound error " do
36
- lambda{
37
- Mbsy::Ambassador.find(email: 'not_found@mbsy.co', auto_create: 0)
38
- }.should raise_error(Mbsy::RecordNotFound)
39
- end
40
- end
41
- end
42
-
43
- it 'returns a list of all ambassadors' do
44
- all_response = Mbsy::Ambassador.all
45
- ambassadors = all_response['ambassadors']
46
- ambassadors.length.should > 0
47
- existing_ambassador_in_response = ambassadors.detect{|a| a['email'] == @existing_ambassador_response['ambassador']['email'] }
48
- existing_ambassador_in_response.should_not be_nil
49
- existing_ambassador_in_response['first_name'].should == 'sigma'
50
- end
51
-
52
- it 'returns stats on an existing ambassador' do
53
- ambassador = Mbsy::Ambassador.stats(email: 'ambassador@mbsy.co')
54
- ambassador['ambassador']['email'].should eq(@existing_ambassador_response['ambassador']['email'])
55
- end
56
-
57
- end
data/spec/base_spec.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mbsy::Base do
4
-
5
- it 'parses element names' do
6
- Mbsy::Base.stub!(:name).and_return("Test::Namespace::ElementName")
7
- Mbsy::Base.element_name.should eql('element_name')
8
- end
9
-
10
- end
data/spec/factories.rb DELETED
File without changes