gratitude 0.0.12 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.hound.yml +34 -0
  3. data/.rubocop.yml +34 -0
  4. data/.travis.yml +4 -1
  5. data/CHANGELOG.md +2 -2
  6. data/CONTRIBUTING.md +97 -0
  7. data/Gemfile +11 -6
  8. data/Guardfile +4 -0
  9. data/README.md +3 -0
  10. data/Rakefile +12 -10
  11. data/gratitude.gemspec +4 -4
  12. data/lib/gratitude/chart.rb +5 -5
  13. data/lib/gratitude/client.rb +2 -1
  14. data/lib/gratitude/connection.rb +4 -2
  15. data/lib/gratitude/error.rb +28 -0
  16. data/lib/gratitude/payday.rb +12 -10
  17. data/lib/gratitude/profile.rb +4 -1
  18. data/lib/gratitude/statistics.rb +18 -16
  19. data/lib/gratitude/tips.rb +42 -10
  20. data/lib/gratitude/user_chart.rb +9 -7
  21. data/lib/gratitude/version.rb +3 -1
  22. data/lib/gratitude.rb +3 -1
  23. data/spec/cassettes/charts.json +8 -5
  24. data/spec/cassettes/current_tips.json +74 -5
  25. data/spec/cassettes/current_tips_not_authenticated.json +76 -0
  26. data/spec/cassettes/paydays.json +8 -5
  27. data/spec/cassettes/profiles.json +73 -17
  28. data/spec/cassettes/prune_tips_not_authenticated.json +76 -0
  29. data/spec/cassettes/statistics.json +8 -70
  30. data/spec/cassettes/update_and_prune.json +15 -15
  31. data/spec/cassettes/update_and_prune_bad_request.json +74 -0
  32. data/spec/cassettes/update_bad_request.json +74 -0
  33. data/spec/cassettes/update_tips.json +5 -5
  34. data/spec/cassettes/update_tips_not_authenticated.json +76 -0
  35. data/spec/cassettes/user_chart_not_found.json +76 -0
  36. data/spec/cassettes/user_charts.json +147 -1
  37. data/spec/gratitude/chart_spec.rb +11 -11
  38. data/spec/gratitude/client_spec.rb +5 -3
  39. data/spec/gratitude/connection_spec.rb +2 -0
  40. data/spec/gratitude/payday_spec.rb +9 -5
  41. data/spec/gratitude/profile_spec.rb +22 -13
  42. data/spec/gratitude/statistics_spec.rb +25 -23
  43. data/spec/gratitude/tips_spec.rb +177 -40
  44. data/spec/gratitude/user_chart_spec.rb +30 -17
  45. data/spec/gratitude/version_spec.rb +2 -0
  46. data/spec/spec_helper.rb +6 -4
  47. metadata +18 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ffc3713c76d9bbaf8b996d658097995633479774
4
- data.tar.gz: 6eaa284ea1160a9b40828c5c6d5bed069949f192
3
+ metadata.gz: 86d7979838448df774b9bd29f73c6b11e468c914
4
+ data.tar.gz: cceea1e613fe4789536d8e91865edbc42e7efbed
5
5
  SHA512:
6
- metadata.gz: 8acf19c5e719aec6737e74726ef77e60b5c635d5c81906603f8e708db4a04a0e60848547e5c59f6d640a7bd26e061bec81bed112e88109647d4097d5a3aa836e
7
- data.tar.gz: 5ae98a6fe09778768cfd6d1ff498b71958421e0a842324781ea9f56f4f80146ee61cfcf0f832f2c057cfd7fab08efbd7f1f8ec992df90d7e2a34468d74c6b7e5
6
+ metadata.gz: de3f92649c3322d05cbcd21218815e59c804cc850fdbf17536b19bcaf92bf7b0630f1ff0c536fa59dfbc45d668eb3b1facd568ce9ddc4678c5a5a7ea79eff4f9
7
+ data.tar.gz: 3f13439f0b3f06ee18aba8438fc953d1cb7695af872c339fcc8221c0341aaeb32bd3b13f7a0762362106c1916e611854e25bef45f7d5cce0ec38841b7e91d5ca
data/.hound.yml ADDED
@@ -0,0 +1,34 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/**
4
+ - bin/**
5
+ - gratitude.gemspec
6
+
7
+ CollectionMethods:
8
+ # Mapping from undesired method to desired_method
9
+ # e.g. to use `detect` over `find`:
10
+ #
11
+ # CollectionMethods:
12
+ # PreferredMethods:
13
+ # find: detect
14
+ PreferredMethods:
15
+ map: 'collect'
16
+ map!: 'collect!'
17
+ reduce: 'inject'
18
+ find: 'detect'
19
+ find_all: 'select'
20
+
21
+ Documentation:
22
+ Enabled: false
23
+
24
+ MethodLength:
25
+ Enabled: false
26
+
27
+ SignalException:
28
+ Enabled: false
29
+
30
+ SingleLineBlockParams:
31
+ Enabled: false
32
+
33
+ StringLiterals:
34
+ EnforcedStyle: double_quotes
data/.rubocop.yml ADDED
@@ -0,0 +1,34 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/**
4
+ - bin/**
5
+ - gratitude.gemspec
6
+
7
+ CollectionMethods:
8
+ # Mapping from undesired method to desired_method
9
+ # e.g. to use `detect` over `find`:
10
+ #
11
+ # CollectionMethods:
12
+ # PreferredMethods:
13
+ # find: detect
14
+ PreferredMethods:
15
+ map: 'collect'
16
+ map!: 'collect!'
17
+ reduce: 'inject'
18
+ find: 'detect'
19
+ find_all: 'select'
20
+
21
+ Documentation:
22
+ Enabled: false
23
+
24
+ MethodLength:
25
+ Enabled: false
26
+
27
+ SignalException:
28
+ Enabled: false
29
+
30
+ SingleLineBlockParams:
31
+ Enabled: false
32
+
33
+ StringLiterals:
34
+ EnforcedStyle: double_quotes
data/.travis.yml CHANGED
@@ -7,4 +7,7 @@ rvm:
7
7
  - 2.0.0
8
8
  - 1.9.3
9
9
  - jruby-19mode
10
- - rbx
10
+ - rbx-2
11
+ script:
12
+ - bundle exec rspec
13
+ - bundle exec rubocop
data/CHANGELOG.md CHANGED
@@ -1,5 +1,5 @@
1
- ### 0.1.0 (Upcoming Stable Release)
2
- * Implement all aspects of the Gittip API.
1
+ ### 0.1.0 (May 16, 2014)
2
+ * Add custom errors for authentication issues, incorrect usernames and tip update errors.
3
3
 
4
4
  ### 0.0.12 (March 30, 2014)
5
5
  * Add support for Chart API endpoint
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,97 @@
1
+ Contributing to Gratitude
2
+ =====================
3
+
4
+ If you'd like to contribute to Gratitude, you're encouraged to submit [pull requests](https://github.com/JohnKellyFerguson/gratitude/pulls), [propose features and discuss issues](https://github.com/JohnKellyFerguson/gratitude/issues).
5
+
6
+ #### Fork the Project
7
+
8
+ Fork the [project on Github](https://github.com/JohnKellyFerguson/gratitude) and check out your copy.
9
+
10
+ ```
11
+ git clone https://github.com/contributor/gratitude.git
12
+ cd gratitude
13
+ git remote add upstream https://github.com/JohnKellyFerguson/gratitude.git
14
+ ```
15
+
16
+ #### Create a Topic Branch
17
+
18
+ Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
19
+
20
+ ```
21
+ git checkout master
22
+ git pull upstream master
23
+ git checkout -b my-feature-branch
24
+ ```
25
+
26
+ #### Bundle Install and Test
27
+
28
+ Ensure that you can build the project and run tests.
29
+
30
+ ```
31
+ bundle install
32
+ bundle exec rake
33
+ ```
34
+
35
+ #### Write Tests
36
+
37
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add to [spec/gratitude](spec/gratitude).
38
+
39
+ We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
40
+
41
+ #### Write Code
42
+
43
+ Implement your feature or bug fix.
44
+
45
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop), run `bundle exec rubocop` and fix any style issues highlighted.
46
+
47
+ Make sure that `bundle exec rake` completes without errors.
48
+
49
+ #### Write Documentation
50
+
51
+ Document any external behavior in the [README](README.md).
52
+
53
+ #### Commit Changes
54
+
55
+ Make sure git knows your name and email address:
56
+
57
+ ```
58
+ git config --global user.name "Your Name"
59
+ git config --global user.email "contributor@example.com"
60
+ ```
61
+
62
+ Writing good commit logs is important. A commit log should describe what changed and why.
63
+
64
+ ```
65
+ git add ...
66
+ git commit
67
+ ```
68
+
69
+ #### Push
70
+
71
+ ```
72
+ git push origin my-feature-branch
73
+ ```
74
+
75
+ #### Make a Pull Request
76
+
77
+ Go to [Gratitude's Github Project Page](https://github.com/contributor/gratitude) and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
78
+
79
+ #### Rebase
80
+
81
+ If you've been working on a change for a while, rebase with upstream/master.
82
+
83
+ ```
84
+ git fetch upstream
85
+ git rebase upstream/master
86
+ git push origin my-feature-branch -f
87
+ ```
88
+
89
+
90
+ #### Check on Your Pull Request
91
+
92
+ Go back to your pull request after a few minutes and check to see whether everything passed on Travis-CI. Everything should look green, otherwise fix any remaining issues.
93
+
94
+
95
+ #### Thank You
96
+
97
+ Your contributions are greatly appreciated. Thanks for helping out!
data/Gemfile CHANGED
@@ -3,16 +3,21 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  group :development do
6
- gem 'jazz_hands', github: 'nixme/jazz_hands', branch: 'bring-your-own-debugger'
7
- gem 'pry-byebug'
8
- gem 'pry-coolline'
9
- gem "guard-rspec"
6
+ gem "jazz_hands", github: "nixme/jazz_hands", branch: "bring-your-own-debugger"
7
+ gem "pry-byebug"
8
+ gem "pry-coolline"
9
+ gem "guard-rubocop", require: false
10
+ gem "guard-rspec", require: false
10
11
  gem "terminal-notifier-guard", require: false
11
12
  end
12
13
 
14
+ group :development, :test do
15
+ gem "rubocop"
16
+ end
17
+
13
18
  group :test do
14
- gem "rspec", '~> 3.0.0.beta2'
19
+ gem "rspec", "~> 3.0.0.beta2"
15
20
  gem "webmock"
16
21
  gem "vcr"
17
- gem 'coveralls', require: false
22
+ gem "coveralls", require: false
18
23
  end
data/Guardfile CHANGED
@@ -4,3 +4,7 @@ guard :rspec do
4
4
  watch('spec/spec_helper.rb') { "spec" }
5
5
  end
6
6
 
7
+ guard :rubocop do
8
+ watch(%r{.+\.rb$})
9
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
10
+ end
data/README.md CHANGED
@@ -371,6 +371,9 @@ client.current_tips
371
371
  ```
372
372
 
373
373
 
374
+ ### Contributing
375
+
376
+ If you'd like to contribute to Gratitude, please check out our [CONTRIBUTING guidelines](CONTRIBUTING.md).
374
377
 
375
378
  ### Copyright and License
376
379
 
data/Rakefile CHANGED
@@ -1,28 +1,30 @@
1
- require 'rubygems'
2
- require 'bundler/gem_tasks'
1
+ # encoding: utf-8
2
+
3
+ require "rubygems"
4
+ require "bundler/gem_tasks"
3
5
 
4
6
  Bundler.setup :default, :development
5
7
 
6
- require 'rspec/core'
7
- require 'rspec/core/rake_task'
8
+ require "rspec/core"
9
+ require "rspec/core/rake_task"
8
10
 
9
11
  RSpec::Core::RakeTask.new(:spec) do |spec|
10
12
  spec.pattern = FileList["spec/**/*_spec.rb"]
11
13
  end
12
14
 
13
- task :default => :spec
15
+ task default: :spec
14
16
 
15
17
  task :console do
16
- require 'irb'
17
- require 'irb/completion'
18
- require 'gratitude'
18
+ require "irb"
19
+ require "irb/completion"
20
+ require "gratitude"
19
21
  ARGV.clear
20
22
  IRB.start
21
23
  end
22
24
 
23
25
  task :pry do
24
- require 'pry'
25
- require 'gratitude'
26
+ require "pry"
27
+ require "gratitude"
26
28
  ARGV.clear
27
29
  Pry.start
28
30
  end
data/gratitude.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'gratitude/version'
4
+ require "gratitude/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "gratitude"
8
8
  spec.version = Gratitude::VERSION
9
9
  spec.authors = ["John Kelly Ferguson"]
10
10
  spec.email = ["hello@johnkellyferguson.com"]
11
- spec.description = %q{A simple Ruby wrapper for the Gittip API.}
12
- spec.summary = %q{A simple Ruby wrapper for the Gittip API.}
11
+ spec.description = %q(A simple Ruby wrapper for the Gittip API.)
12
+ spec.summary = %q(A simple Ruby wrapper for the Gittip API.)
13
13
  spec.homepage = "https://github.com/JohnKellyFerguson/gratitude"
14
14
  spec.license = "MIT"
15
15
 
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Gratitude
2
4
  class Chart
3
5
  extend Connection
@@ -34,14 +36,12 @@ module Gratitude
34
36
  all.sort_by { |chart| chart.date }.reverse
35
37
  end
36
38
 
37
- private
38
-
39
- def self.get_charts_from_gittip
40
- faraday.get('/about/charts.json').body.to_a
39
+ def self.charts_from_gittip
40
+ faraday.get("/about/charts.json").body.to_a
41
41
  end
42
42
 
43
43
  def self.collect_charts
44
- get_charts_from_gittip.each do |chart_hash|
44
+ charts_from_gittip.each do |chart_hash|
45
45
  Chart.new(chart_hash)
46
46
  end
47
47
  end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require "gratitude/connection"
2
4
  require "gratitude/tips"
3
5
 
@@ -13,6 +15,5 @@ module Gratitude
13
15
  @api_key = options[:api_key]
14
16
  faraday.basic_auth(api_key, "")
15
17
  end
16
-
17
18
  end # Payday
18
19
  end # Gratitude
@@ -1,8 +1,10 @@
1
+ # encoding: utf-8
2
+
1
3
  module Gratitude
2
4
  module Connection
3
5
  def faraday
4
- @faraday ||= Faraday.new(:url => "https://www.gittip.com/") do |faraday|
5
- faraday.response :json, :content_type => /\bjson$/
6
+ @faraday ||= Faraday.new(url: "https://www.gittip.com/") do |faraday|
7
+ faraday.response :json, content_type: /\bjson$/
6
8
  faraday.adapter Faraday.default_adapter
7
9
  end
8
10
  end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ module Gratitude
4
+ class UsernameNotFoundError < StandardError
5
+ def initialize(username)
6
+ super(
7
+ "The requested username, '#{username}', could not be found."\
8
+ "Please note that Gittip API requests are case sensitive.")
9
+ end
10
+ end
11
+
12
+ class AuthenticationError < StandardError
13
+ def initialize
14
+ super(
15
+ "The supplied username and api_key could not properly authenticate "\
16
+ "with Gittip.")
17
+ end
18
+ end
19
+
20
+ class TipUpdateError < StandardError
21
+ def initialize(usernames)
22
+ combined_usernames = usernames.join(", ")
23
+ super(
24
+ "There was an error updating the tip(s) for the following user(s): "\
25
+ "#{combined_usernames}.")
26
+ end
27
+ end
28
+ end
@@ -1,12 +1,15 @@
1
+ # encoding: utf-8
2
+
1
3
  module Gratitude
2
4
  class Payday
3
5
  extend Connection
4
6
  PAYDAYS = []
5
7
 
6
8
  attr_reader :ach_fees_volume, :ach_volume, :charge_fees_volume,
7
- :charge_volume, :number_of_ach_credits, :number_of_active_users,
8
- :number_of_failing_credit_cards, :number_of_missing_credit_cards,
9
- :number_of_charges, :number_of_participants, :number_of_tippers,
9
+ :charge_volume, :number_of_ach_credits,
10
+ :number_of_active_users, :number_of_failing_credit_cards,
11
+ :number_of_missing_credit_cards, :number_of_charges,
12
+ :number_of_participants, :number_of_tippers,
10
13
  :number_of_transfers, :transfer_volume, :transfer_end_time,
11
14
  :transfer_start_time
12
15
 
@@ -39,7 +42,9 @@ module Gratitude
39
42
  @number_of_tippers = options["ntippers"]
40
43
  @number_of_transfers = options["ntransfers"]
41
44
  @transfer_volume = options["transfer_volume"]
42
- @transfer_end_time = DateTime.parse(options["ts_end"]) if options["ts_end"]
45
+ if options["ts_end"]
46
+ @transfer_end_time = DateTime.parse(options["ts_end"])
47
+ end
43
48
  if options["ts_start"]
44
49
  @transfer_start_time = DateTime.parse(options["ts_start"])
45
50
  end
@@ -64,17 +69,14 @@ module Gratitude
64
69
  all.sort_by { |p| p.ts_end }.reverse
65
70
  end
66
71
 
67
- private
68
-
69
- def self.get_paydays_from_gittip
70
- faraday.get('/about/paydays.json').body.to_a
72
+ def self.paydays_from_gittip
73
+ faraday.get("/about/paydays.json").body.to_a
71
74
  end
72
75
 
73
76
  def self.collect_paydays
74
- get_paydays_from_gittip.each do |payday_hash|
77
+ paydays_from_gittip.each do |payday_hash|
75
78
  Payday.new(payday_hash)
76
79
  end
77
80
  end
78
-
79
81
  end # Payday
80
82
  end # Gratitude
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Gratitude
2
4
  class Profile
3
5
  include Connection
@@ -100,11 +102,12 @@ module Gratitude
100
102
 
101
103
  def response
102
104
  @response ||= faraday.get("/#{username}/public.json")
105
+ raise UsernameNotFoundError, username if @response.status == 406
106
+ @response
103
107
  end
104
108
 
105
109
  def response_body
106
110
  @response_body ||= response.body
107
111
  end
108
-
109
112
  end # Profile
110
113
  end # Gratitude
@@ -1,25 +1,27 @@
1
+ # encoding: utf-8
2
+
1
3
  module Gratitude
2
4
  class Statistics
3
5
  include Connection
4
6
 
5
7
  class << self
6
- alias :current :new
8
+ alias_method :current, :new
7
9
  end
8
10
 
9
11
  def average_tip_amount
10
12
  response_body["average_tip"]
11
13
  end
12
- alias :average_tip :average_tip_amount
14
+ alias_method :average_tip, :average_tip_amount
13
15
 
14
16
  def average_number_of_tippees
15
17
  response_body["average_tippees"]
16
18
  end
17
- alias :average_tippees :average_number_of_tippees
19
+ alias_method :average_tippees, :average_number_of_tippees
18
20
 
19
21
  def amount_in_escrow
20
22
  response_body["escrow"]
21
23
  end
22
- alias :escrow :amount_in_escrow
24
+ alias_method :escrow, :amount_in_escrow
23
25
 
24
26
  def last_thursday
25
27
  response_body["last_thursday"]
@@ -28,33 +30,33 @@ module Gratitude
28
30
  def number_of_ach_credits
29
31
  response_body["nach"].to_i
30
32
  end
31
- alias :nach :number_of_ach_credits
32
- alias :number_of_achs :number_of_ach_credits
33
+ alias_method :nach, :number_of_ach_credits
34
+ alias_method :number_of_achs, :number_of_ach_credits
33
35
 
34
36
  def number_of_active_users
35
37
  response_body["nactive"]
36
38
  end
37
- alias :nactive :number_of_active_users
39
+ alias_method :nactive, :number_of_active_users
38
40
 
39
41
  def number_of_credit_cards
40
42
  response_body["ncc"]
41
43
  end
42
- alias :ncc :number_of_credit_cards
44
+ alias_method :ncc, :number_of_credit_cards
43
45
 
44
46
  def number_of_givers
45
47
  response_body["ngivers"]
46
48
  end
47
- alias :ngivers :number_of_givers
49
+ alias_method :ngivers, :number_of_givers
48
50
 
49
51
  def number_who_give_and_receive
50
52
  response_body["noverlap"]
51
53
  end
52
- alias :noverlap :number_who_give_and_receive
54
+ alias_method :noverlap, :number_who_give_and_receive
53
55
 
54
56
  def number_of_receivers
55
57
  response_body["nreceivers"]
56
58
  end
57
- alias :nreceivers :number_of_receivers
59
+ alias_method :nreceivers, :number_of_receivers
58
60
 
59
61
  def other_people
60
62
  response_body["other_people"]
@@ -63,12 +65,12 @@ module Gratitude
63
65
  def percentage_of_users_with_credit_cards
64
66
  response_body["pcc"].strip
65
67
  end
66
- alias :pcc :percentage_of_users_with_credit_cards
68
+ alias_method :pcc, :percentage_of_users_with_credit_cards
67
69
 
68
70
  def punctuation
69
71
  response_body["punc"]
70
72
  end
71
- alias :punc :punctuation
73
+ alias_method :punc, :punctuation
72
74
 
73
75
  def statements
74
76
  response_body["statements"]
@@ -85,12 +87,12 @@ module Gratitude
85
87
  def number_of_tips
86
88
  response_body["tip_n"]
87
89
  end
88
- alias :tip_n :number_of_tips
90
+ alias_method :tip_n, :number_of_tips
89
91
 
90
92
  def value_of_total_backed_tips
91
93
  response_body["total_backed_tips"]
92
94
  end
93
- alias :total_backed_tips :value_of_total_backed_tips
95
+ alias_method :total_backed_tips, :value_of_total_backed_tips
94
96
 
95
97
  def transfer_volume
96
98
  response_body["transfer_volume"]
@@ -99,7 +101,7 @@ module Gratitude
99
101
  private
100
102
 
101
103
  def response
102
- @response ||= faraday.get('/about/stats.json')
104
+ @response ||= faraday.get("/about/stats.json")
103
105
  end
104
106
 
105
107
  def response_body
@@ -1,9 +1,15 @@
1
+ # encoding: utf-8
2
+
1
3
  module Gratitude
2
4
  class Client
3
5
  module Tips
4
-
5
6
  def current_tips
6
- faraday.get(tips_url).body
7
+ begin
8
+ response = faraday.get(tips_url)
9
+ rescue Faraday::Error::ParsingError
10
+ raise AuthenticationError
11
+ end
12
+ response.body
7
13
  end
8
14
 
9
15
  def current_tips_total
@@ -11,24 +17,35 @@ module Gratitude
11
17
  end
12
18
 
13
19
  def update_tips(array_of_hashes_with_usernames_and_amounts)
14
- faraday.post do |request|
15
- payload_for(request, array_of_hashes_with_usernames_and_amounts)
16
- end.body
20
+ post_tips_to_gittip(array_of_hashes_with_usernames_and_amounts)
17
21
  end
18
22
 
19
23
  def update_tips_and_prune(array_of_hashes_with_usernames_and_amounts)
20
- faraday.post do |request|
21
- payload_for(request, array_of_hashes_with_usernames_and_amounts)
22
- request.params = { :also_prune => "true"}
23
- end.body
24
+ post_tips_to_gittip(
25
+ array_of_hashes_with_usernames_and_amounts, prune: true
26
+ )
24
27
  end
25
28
 
26
- private
29
+ private
27
30
 
28
31
  def tips_url
29
32
  "/#{username}/tips.json"
30
33
  end
31
34
 
35
+ def post_tips_to_gittip(array_of_hashes, options = {})
36
+ response = faraday_post_response(array_of_hashes, options)
37
+ return_response_body_or_raise_update_error(response)
38
+ end
39
+
40
+ def faraday_post_response(array_of_hashes, options = {})
41
+ faraday.post do |request|
42
+ payload_for(request, array_of_hashes)
43
+ request.params = { also_prune: "true" } if options[:prune] == true
44
+ end
45
+ rescue
46
+ raise AuthenticationError
47
+ end
48
+
32
49
  def payload_for(request, array_of_hashes)
33
50
  request.url(tips_url)
34
51
  request.body = prepared_tips_array(array_of_hashes).to_json
@@ -50,6 +67,21 @@ module Gratitude
50
67
  }
51
68
  end
52
69
 
70
+ def return_response_body_or_raise_update_error(response)
71
+ if usernames_with_errors(response.body).size > 0
72
+ raise TipUpdateError, usernames_with_errors(response.body)
73
+ else
74
+ response.body
75
+ end
76
+ end
77
+
78
+ def usernames_with_errors(response_body)
79
+ response_body.each_with_object([]) do |user_tip_response, array|
80
+ if user_tip_response.key?("error")
81
+ array << user_tip_response["username"]
82
+ end
83
+ end
84
+ end
53
85
  end # Tips
54
86
  end # Client
55
87
  end # Gratitude