47 Commits

Author SHA1 Message Date
Ilija Eftimov
9e05b8f8c1
Fix broken URIs in gemspec (#1023) 2021-12-23 13:58:34 -05:00
Brandur
3aad27f6e4 Correct use of match 2021-04-05 15:05:50 -07:00
Brandur
6a9ca59d06 Use Rubocop-preferred regular expression style 2021-04-05 15:03:07 -07:00
Brandur
410fc18fc3 Check start of input on matches 2021-04-05 14:57:51 -07:00
Masafumi Koba
ec31858e07
Reduce packed gem size (#973)
This change aims to reduce the packed gem size by removing needless files from `s.files`.

Also, `s.test_files` is also removed because it is not defined in the gemspec reference.
(see <https://guides.rubygems.org/specification-reference/>)

- Size changed: 215K -> 264K
- Files changed: 221 files -> 120 files
2021-04-05 12:14:35 -07:00
Brandur
44766516d9 stripe-ruby V5 (#815)
* Convert library to use built-in `Net::HTTP`

Moves the library off of Faraday and over onto the standard library's
built-in `Net::HTTP` module. The upside of the transition is that we
break away from a few dependencies that have caused us a fair bit of
trouble in the past, the downside is that we need more of our own code
to do things (although surprisingly, not that much more).

The biggest new pieces are:

* `ConnectionManager`: A per-thread class that manages a connection to
  each Stripe infrastructure URL (like `api.stripe.com`,
  `connect.stripe.com`, etc.) so that we can reuse them between
  requests. It's also responsible for setting up and configuring new
  `Net::HTTP` connections, which is a little more heavyweight
  code-wise compared to other libraries. All of this could have lived in
  `StripeClient`, but I extracted it because that class has gotten so
  big.

* `MultipartEncoder`: A class that does multipart form encoding for file
  uploads. Unfortunately, Ruby doesn't bundle anything like this. I
  built this by referencing the Go implementation because the original
  RFC is not very detailed or well-written. I also made sure that it was
  behaving similarly to our other custom implementations like
  stripe-node, and that it can really upload a file outside the test
  suite.

There's some risk here in that it's easy to miss something across one of
these big transitions. I've tried to test out various error cases
through tests, but also by leaving scripts running as I terminate my
network connection and bring it back. That said, we'd certainly release
on a major version bump because some of the interface (like setting
`Stripe.default_client`) changes.

* Drop support for old versions of Ruby

Drops support for Ruby 2.1 (EOL March 31, 2017) and 2.2 (EOL March 31,
2018). They're removed from `.travis.yml` and the gemspec and RuboCop
configuration have also been updated to the new lower bound.

Most of the diff here are minor updates to styling as required by
RuboCop:

* String literals are frozen by default, so the `.freeze` we had
  everywhere is now considered redundant.

* We can now use Ruby 1.9 style hash syntax with string keys like `{
  "foo": "bar" }`.

* Converted a few heredocs over to use squiggly (leading whitespace
  removed) syntax.

As discussed in Slack, I didn't drop support for Ruby 2.3 (EOL March 31,
2019) as we still have quite a few users on it. As far as I know
dropping it doesn't get us access to any major syntax improvements or
anything, so it's probably not a big deal.

* Make `CardError`'s `code` parameter named instead of positional (#816)

Makes the `code` parameter on `CardError` named instead of positional.
This makes it more consistent with the rest of the constructor's
parameters and makes instantiating `CardError` from `StripeClient`
cleaner.

This is a minor breaking change so we're aiming to release it for the
next major version of stripe-ruby.

* Bump Rubocop to latest version (#818)

* Ruby minimum version increase followup (#819)

* Remove old deprecated methods (#820)

* Remove all alias for list methods (#823)

* Remove UsageRecord.create method (#826)

* Remove IssuerFraudRecord (#827)

* Add ErrorObject to StripeError exceptions (#811)

* Tweak retry logic to be a little more like stripe-node (#828)

Tweaks the retry logic to be a little more like stripe-node's. In
particular, we also retry under these conditions:

* If we receive a 500 on a non-`POST` request.
* If we receive a 503.

I made it slightly different from stripe-node which checks for a 500
with `>= 500`. I don't really like that -- if we want to retry specific
status codes we should be explicit about it.

We're actively re-examining ways on how to make it easier for clients to
figure out when to retry right now, but I figure V5 is a good time to
tweak this because the modifications change the method signature of
`should_retry?` slightly, and it's technically a public method.

* Fix inverted sign for 500 retries (#830)

I messed up in #828 by (1) accidentally flipping the comparison against
`:post` when checking whether to retry on 500, and (2) forgetting to
write new tests for the condition, which is how (1) got through.

This patch fixes both those problems.

* Remove a few more very old deprecated methods (#831)

I noticed that we had a couple of other deprecated methods on `Stripe`
and `StripeObject` that have been around for a long time. May as well
get rid of them too -- luckily they were using `Gem::Deprecate` so
they've been producing annoying deprecated warnings for quite a while
now.

* Remove extraneous slash at the end of the line

* Reset connections when connection-changing configuration changes (#829)

Adds a few basic features around connection and connection manager
management:

* `clear` on connection manager, which calls `finish` on each active
  connection and then disposes of it.

* A centralized cross-thread tracking system for connection managers in
  `StripeClient` and `clear_all_connection_managers` which clears all
  known connection managers across all threads in a thread-safe way.

The addition of these allow us to modify the implementation of some of
our configuration on `Stripe` so that it can reset all currently open
connections when its value changes.

This fixes a currently problem with the library whereby certain
configuration must be set before the first request or it remains fixed
on any open connections. For example, if `Stripe.proxy` is set after a
request is made from the library, it has no effect because the proxy
must have been set when the connection was originally being initialized.

The impetus for getting this out is that I noticed that we will need
this internally in a few places when we're upgrading to stripe-ruby V5.
Those spots used to be able to hack around the unavailability of this
feature by just accessing the Faraday connection directly and resetting
state on it, but in V5 `StripeClient#conn` is gone, and that's no longer
possible.

* Minor cleanup in `StripeClient` (#832)

I ended up having to relax the maximum method line length in a few
previous PRs, so I wanted to try one more cleanup pass in
`execute_request` to see if I could get it back at all.

The answer was "not by much" (without reducing clarity), but I found a
few places that could be tweaked. Unfortunately, ~50 lines is probably
the "right" length for this method in that you _could_ extract it
further, but you'd end up passing huge amounts of state all over the
place in method parameters, and it really wouldn't look that good.

* Do better bookkeeping when tracking state in `Thread.current` (#833)

This is largely just another cleanup patch, but does a couple main
things:

* Hoists the `last_response` value into thread state. This is a very
  minor nicety, but effectively makes `StripeClient` fully thread-safe,
  which seems like a minor nicety. Two calls to `#request` to the same
  `StripeObject` can now be executed on two different threads and their
  results won't interfere with each other.

* Moves state off one-off `Thread.current` keys and into a single one
  for the whole client which stores a new simple type of record called
  `ThreadContext`. Again, this doesn't change much, but adds some minor
  type safety and lets us document each field we expect to have in a
  thread's context.

* Add Invoice.list_upcoming_line_items method (#834)
2019-08-20 11:35:24 -07:00
Olivier Bellone
50748b1189
Add gem metadata (#807) 2019-07-04 15:47:45 -07:00
Olivier Bellone
ec91de6849
Upgrade Rubocop and fix a bunch of issues (#786)
* Bump Rubocop to 0.57.2

* Style/StderrPuts: Use warn instead of .puts

* Style/ExpandPathArguments: Use expand_path('../test_helper', __dir__) instead of expand_path('../../test_helper', __FILE__)

* Style/Encoding: Unnecessary utf-8 encoding comment

* Style/StringLiterals: Prefer double-quoted strings

* Style/AccessModifierDeclarations

* Style/FormatStringToken: Prefer annotated tokens

* Naming/UncommunicativeMethodParamName

* Metrics/LineLength: set maximum line length to 100 characters

* Style/IfUnlessModifier: Favor modifier if usage when having a single-line body

* Style/ClassVars

* Metrics/LineLength: set maximum line length to 80 characters (default)

* Style/AccessModifierDeclarations: EnforcedStyle: inline
2019-05-24 10:43:42 -07:00
Olivier Bellone
c80a491e03
Bump Faraday minimum version to 0.13.0 2018-11-16 13:54:26 +01:00
Brandur
85013c9770 Use Faraday's Net::HTTP::Persistent adapter
This changes the library's default connection over to use the adapter
for `Net::HTTP::Persistent`, which is a connection pooling library for
Ruby.

In the long run, I think we should probably just drop Faraday ... the
amount of value it's getting us is extremely tenuous and its API is
difficult to work with. I hate to do it at this point though because
technically people could be writing custom middleware for it.
2018-11-15 08:55:28 -08:00
Olivier Bellone
21db64fe0e
Use ::File instead of File 2018-08-27 15:32:10 +02:00
Brandur
863da48398 Add frozen_string_literal to every file and enforce Rubocop rule
Adds the magic `frozen_string_literal: true` comment to every file and
enables a Rubocop rule to make sure that it's always going to be there
going forward as well.

See here for more background [1], but the basic idea is that unlike many
other languages, static strings in code are mutable by default. This has
since been acknowledged as not a particularly good idea, and the
intention is to rectify the mistake when Ruby 3 comes out, where all
string literals will be frozen. The `frozen_string_literal` magic
comment was introduced in Ruby 2.3 as a way of easing the transition,
and allows libraries and projects to freeze their literals in advance.

I don't think this is breaking in any way: it's possible that users
might've been pulling out one of are literals somehow and mutating it,
but that would probably not have been useful for anything and would
certainly not be recommended, so I'm quite comfortable pushing this
change through as a minor version.

As discussed in #641.

[1] https://stackoverflow.com/a/37799399
2018-05-10 14:56:14 -07:00
Olivier Bellone
cd7e607258
Require Faraday 0.10 for proper nested array encoding 2017-10-05 17:56:39 +02:00
Brandur
7f85eea3ee Fix low hanging Rubocop TODOs
I wanted to see what fixing Rubocop TODOs was like, so I tried to
eliminate all the easy ones. Most of these were pretty easy, and the
changes required are relatively minimal.

Some of the stuff left is harder. Pretty much everything under
`Metrics/*` is going to be a pretty big yak shave. A few of the others
are just going to need a little more work (e.g. `Style/ClassVars` and
`Style/GuardClause`). Going to stop here for now.
2017-09-27 15:07:18 -07:00
Olivier Bellone
e02ff7f849
Start using RuboCop for linting 2017-09-27 21:28:25 +02:00
Gareth Rees
bc3abe663f Add correct required_ruby_version
The `stripe` gem installs on 1.9.3, but is unusable due to the use of keyword args.

    vagrant@vagrant-ubuntu-trusty-64: ~
    $ ruby -v
    ruby 1.9.3p551 (2014-11-13) [x86_64-linux] Brightbox

    vagrant@vagrant-ubuntu-trusty-64: ~
    $ sudo gem install stripe
    Fetching: multipart-post-2.0.0.gem (100%)
    Fetching: faraday-0.13.1.gem (100%)
    Fetching: stripe-3.3.1.gem (100%)
    Successfully installed multipart-post-2.0.0
    Successfully installed faraday-0.13.1
    Successfully installed stripe-3.3.1
    3 gems installed

    vagrant@vagrant-ubuntu-trusty-64: ~
    $ irb
    irb(main):001:0> require 'stripe'
    SyntaxError: /var/lib/gems/1.9.1/gems/stripe-3.3.1/lib/stripe.rb:207: syntax error, unexpected tLABEL
      def self.set_app_info(name, version: nil, url: nil)
                                          ^
    /var/lib/gems/1.9.1/gems/stripe-3.3.1/lib/stripe.rb:207: Can't assign to nil
      def self.set_app_info(name, version: nil, url: nil)
                                               ^
    /var/lib/gems/1.9.1/gems/stripe-3.3.1/lib/stripe.rb:225: syntax error, unexpected keyword_end, expecting $end
            from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
            from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
            from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
            from (irb):1
            from /usr/bin/irb:12:in `<main>'
2017-09-20 19:43:55 +01:00
Olivier Bellone
2c2180fc5c Fix Faraday minimum version 2017-04-07 09:40:43 -07:00
Brandur
1886d9a625 Move to an alternative system based on StripeClient 2017-02-14 12:17:37 -08:00
Brandur
aa12f7e621 Relax rest-client requirements
Our fairly old requirements for rest-client (and therefore mime-types)
are starting to cause some dependency hell problems for some customers.
Try relaxing these constraints and locking 1.9 specifically into
compatible versions.
2016-08-31 06:56:17 -07:00
Mike Chlipala
65bbee4bf8 Relax rest-client version requirements 2016-07-11 16:36:23 -07:00
JuanitoFatas
f4f8d38643 Fix homepage for Ruby docs in gemspec 2016-06-13 18:07:11 +08:00
Brandur
6920d9db68 Update authors/email in Gemspec
Unfortunately neither of these people work for Stripe anymore. Let's put
a valid contact email address in here instead.
2016-05-25 10:55:29 -07:00
Brandur
dd2bae2057 Lock Byebug to just the MRI
Here we predicate the installation of Byebug on being on the MRI. This
allows us to `bundle install` on alternate platforms like JRuby.

Also performs some Gemfile management: add latest MRI and JRuby versions
and remove special case Gemfiles.
2016-03-14 08:59:13 -07:00
Ara Hacopian
c0ef16ad1d Remove json gem dependency
All required rubies include JSON as part of the stdlib.
2016-02-09 14:12:39 -05:00
Kyle Conroy
7bf660d124 Only support Ruby >= 1.9.3
Update the README, Gemfiles, and gemspec to remove all references to
Rubies < 1.9.3.
2015-11-04 14:52:56 -08:00
Brandur
a084df78ef Add byebug/pry as a gem development dependency
I find myself using these quite a bit when looking into problems, and
currently have to manually re-add them to the Gemfile/gemspec to get
them in the bundle and make them available in tests.

Here we gate the debugger to only come in for Ruby > 2 so as to avoid
problems with various compatibility problems between debuggers and
versions of Ruby. If there's a demand for a pre-Ruby 2 debugger, we can
add that separately.

Any major objections to this one? Thanks.
2015-10-12 12:10:39 -07:00
Andy Brody
af7efeff98 Remove unnecessary dependency on mime-types.
Let rest-client depend on mime-types directly.
2015-04-14 14:49:27 -07:00
Nick Gauthier
660befd6c3 update mime-types requirement to allow 1.x and 2.x 2014-07-08 09:41:00 -04:00
Andrew Thorp
9ead81b352 replace multi_json with json, fixes #101 2014-01-24 23:27:25 -08:00
Ryan Biesemeyer
1e8062b83b Add license to gemspec. Fixes #89 2013-12-21 22:19:43 +00:00
Amber Feng
30c263bc59 Fix mime-types at 1.25 so we don't break Ruby 1.8. 2013-11-14 13:51:37 -08:00
Danny Whalen
bb9b4ecf97 Remove duplicate gemspec entries 2013-07-15 12:04:18 -07:00
Tim Craft
9ae705f845 Add version specifiers for activesupport/shoulda/mocha dev dependencies 2013-07-05 17:36:36 +01:00
Evan Broder
3354d9dddd Relax version constraint on multi_json (fixes #44)
We still need >= 1.0.4 because that's when support for pretty-printing
was introduced.
2012-11-21 13:59:14 -08:00
Greg Brockman
5caf8ece54 Update gemspec to dynamically select files 2012-06-19 23:16:02 -07:00
Evan Broder
eee7ab5c44 Add a Rakefile for running tests 2012-05-04 03:06:38 -07:00
Evan Broder
69b1e4b0bd Bump the multi_json dependency
multi_json 1.0 did not support pretty encoding
2012-05-01 16:32:56 -07:00
Evan Broder
f1d7ab6123 Drop old stripe-json vendor files from gemspec.
Fixes #24
2012-04-30 16:32:15 -07:00
Evan Broder
f8073e132a Add our own JSON abstraction to deal with MultiJson backwards compatibility
Thanks to Kevin Menard for the tip on how to approach it.
2012-04-26 13:58:20 -07:00
Evan Broder
32e619c04a Switch from deprecated MultiJson.{encode,decode} to .{dump,load}
Bump gem dependency accordingly
2012-04-23 10:18:57 -07:00
Brian Collins
a6de6e3be7 Use multi_json 2012-04-01 13:44:22 -07:00
Patrick Collison
332b0caaf4 1.4.0 -> 1.4; add rest-client declaration
Allow >= 1.4.0, < 2
Bump version to 1.5.23
2011-11-12 13:43:30 -08:00
Patrick Collison
87d3c60509 Depend on rest-client ~> 1.4.0
Fixes #7
2011-11-12 09:44:15 -08:00
Greg Brockman
6d96afa97c Add vendored JSON to gemspec 2011-08-08 19:32:59 -07:00
Greg Brockman
42f20ff6c9 Remove JSON as an explicit dependency
TODO: bundle pure Ruby JSON implementation for Ruby 1.8
2011-07-12 21:46:31 -07:00
Bradley Grzesiak
b03a4165df Remove circular dependency in gemspec 2011-07-08 17:28:50 -07:00
Greg Brockman
0813418b74 1.5.0 release 2011-05-26 11:47:01 -07:00