363 Commits

Author SHA1 Message Date
Jamu Kakar
74dfbe57d6 Apply review feedback from @ob-stripe. 2018-02-15 16:10:10 -08:00
Jamu Kakar
0b6a45af2e Fix broken test. 2018-02-15 16:05:02 -08:00
Olivier Bellone
5f1ddf2a96
Skip calling to_hash for nil 2018-02-12 11:20:19 -08:00
Olivier Bellone
56430ad405
Add support for coveralls.io 2017-12-08 17:48:40 -08:00
Brandur
3fc5e5b351 Introduce new IdempotencyError type
A few weeks back a new error type `idempotency_error` was introduced in
the API. I put it in to respond to #503, but then forgot to add support
for it in this library. This patch introduces a new exception class that
represents it.
2017-12-07 17:39:14 -08:00
Brandur
2bc471d501 Fix encoding of arrays that are sent in query strings
As discussed in #608, we currently have a problem where Faraday
deconstructs a query string that we encode and strips out any of the
array index numbers that we added. It's not too clear on why it does
this, but it appears to be built in at a pretty low level and hard to
change.

I spent a little time on this and it turns out that we can avoid the bad
code by depending on Faraday's `params` accessor on a request instead of
trying to do the encoding ourselves. Webmock has a pretty hard time
detecting the difference, but you can see some before and after encoding
here.

Before:

```
I, [2017-12-06T17:41:23.083942 #36737]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B%5D%5Bplan%5D=gold&subscription_items%5B%5D%5Bquantity%5D=2
```

After:

```
I, [2017-12-06T17:42:12.727752 #37158]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B0%5D%5Bplan%5D=gold&subscription_items%5B0%5D%5Bquantity%5D=2
```

Honestly, some of this can still use a lot of cleanup: it's weird that
we manually encode parameters ourselves for bodies, but not for queries;
but I think this'll fix the problem for now.

Fixes #608.
2017-12-07 10:51:07 -08:00
Olivier Bellone
949efb017d
Support Tempfiles in file_upload creation requests 2017-12-05 16:52:37 -08:00
Brandur
85c811a18f Remove manual exchange rate stubbing
stripe-mock 0.4.0 comes with the up-to-date exchange rates API. Here we
bump the required version and remove the manual exchange rate stubbing
in stripe-ruby's test suite.
2017-10-31 09:17:51 -07:00
Olivier Bellone
b153b39203
Add support for exchange_rates API requests 2017-10-31 10:25:18 +01:00
Olivier Bellone
c455be74d4
Add support for listing source_transactions 2017-10-26 15:43:34 +02:00
Brandur
2a087595a9 Add explicit operations list everywhere
Makes the `operations` argument to `nested_resource_class_methods`
required and adds explicit lists to any invocations that were missing
one.

The impetus here is that I think it's more easily digestible if each
call site is explicit about what operations it supports and therefore
which methods it's about to create on the class.
2017-10-17 18:02:36 -07:00
Brandur
7699033d90 Merge pull request #597 from stripe/ob-nested-resource-class-methods
Nested resource class methods
2017-10-17 17:50:52 -07:00
Brandur
5c3b741a2c Merge pull request #599 from stripe/brandur-dont-persist-idempotency-key
Don't persist `idempotency_key` option between API requests
2017-10-16 13:09:52 -07:00
Brandur
b4e64969cc Don't persist idempotency_key option between API requests
Excludes `idempotency_key` from opts to persist between API requests.
Obviously the same idempotency key is not something that we ever want to
use again.

Fixes #598.
2017-10-16 13:00:32 -07:00
Brandur
e4af9ad820 Merge pull request #596 from stripe/ob-fix-554
Encode arrays as hashes when needed
2017-10-16 12:03:59 -07:00
Brandur
6a87b9ce68 Fix wording in test case name 2017-10-16 12:00:48 -07:00
Brandur
cfa6c2b8ae Merge pull request #595 from stripe/ob-fix-524
Call Object#method if method accessor is called with arguments
2017-10-16 12:01:07 -07:00
Olivier Bellone
7f866aab5c
Nested resource class methods 2017-10-15 18:49:13 +02:00
Olivier Bellone
4406f8e258
Call Object#method if method accessor is called with arguments 2017-10-14 23:00:40 +02:00
Olivier Bellone
de21302f4e
Encode arrays as hashes when needed 2017-10-14 17:48:20 +02:00
Olivier Bellone
c920a7c649
Maintain class in deep_copy 2017-10-14 01:05:06 +02:00
Brandur
91099f9ee5 Implement custom Marshal encoder/decoder for StripeObject
Backtracks a little bit #586 by bringing back custom `StripeObject`
encoding and decoding methods for Ruby's `Marshal`. These work by just
persisting values and some opts, and skipping everything else. It's
mostly the same as what we had before, but implemented a little more
cleanly so that we don't actually need to invoke `Marshal` anywhere
ourselves.

In #586 we still managed to remove all the uses of `Marshal` in our own
codebase to make the linter happy. Even though we wouldn't recommend the
use of `Marshal`, this code at least enables it for anyone using a Rails
cache or similar mechanism.

Addresses #90.
2017-10-13 09:31:08 -07:00
Olivier Bellone
a210c5cd76
Ensure that each thread has its own client 2017-10-12 18:20:13 +02:00
Olivier Bellone
92e216634c
detach method for detaching sources from customers 2017-10-11 11:15:38 +02:00
Brandur
8ea866616b Log query string as well as body on log debug
This patch modifies the debugging-level logging logic slightly so that
if it's a `GET` request that includes a query string, we log that string
just like we would've for a request body on a `POST` or like.

This especially comes in handy when looking when trying to resolve
something like a problem with the upcoming invoices endpoint like we saw
in #576, but will be useful in a number of situations.
2017-10-04 13:00:35 -07:00
Brandur
3f454495bf Merge pull request #586 from stripe/brandur-remove-marshal
Implement deep copy for StripeObject and remove marshal/unmarshal
2017-09-29 07:13:32 -07:00
Brandur
80d85a522c Implement deep copy for StripeObject and remove marshal/unmarshal
We were previously using a bit of a hack to get a free deep copy
implementation through Ruby's marshaling framework. Lint call this out
as a security problem though, and rightfully so: when combined with
unsanitized user input, unmarshaling can result in very serious security
breaches involving arbitrary code execution.

This patch removes all uses of marshal/unmarshal in favor of
implementing a deep copy method for `StripeObject`. I also reworked some
of the constants around what keys are available for `opts`. I'm still
not completely happy with the results, but I think it's going to need a
slightly larger refactor in order to get somewhere truly good.

There is what could be a breaking change for people doing non-standard
stuff with the library: the opts that we copy with an object are now
whitelisted, so if they were being used to pass around extraneous data,
that might not work as expected anymore. But because this is a contract
that we never committed to, I don't think I'd bump the major version for
change.
2017-09-28 11:02:20 -07:00
Brandur
cb198baaa3 Remove Rubocop TODO around guard clauses
Removes Rubocop TODO around guard clauses and fixes the outstanding
offenses.

This is starting to get into territory that feels of more dubious value
to me, but at least it did get me writing a couple more tests, so let's
see how it goes by keeping this on.
2017-09-28 09:32:44 -07: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
Brandur
ed6d7a7cf9 Upgrade stripe-mock to 0.2.0
This pulls us onto the new version of stripe-mock which should be
checking parameters more accurately now that it's on OpenAPI 3.0.
2017-09-22 12:01:30 -07:00
Tim Maxwell
f4412eec1b Work around idiosyncrasies of stripe-mock 2017-09-20 14:11:11 -07:00
Tim Maxwell
0b4ac62eae Remove recipient card tests 2017-09-20 14:03:00 -07:00
Tim Maxwell
d12fcdb862 Remove tests for legacy Bitcoin API 2017-09-20 13:48:05 -07:00
Brandur
1ca67cb954 Correct behavior for error logging
A few changes:

* Add a new `Util.log_error` method which will forward to the equivalent
  of `#error` on a logger.
* Move errors produced by `StripeClient` to use `Util.log_error`.
* Change standard stdout logging behavior to log to stderr in the case
  of `Util.log_error.
* Change `Stripe.log_level` values to be an enum in a similar fashion as
  the standard library's built in `Logger`.
2017-08-11 11:45:43 -07:00
Brandur
cb111a8e74 Add support for setting a logger
Adds support for setting `Stripe.logger` to a logger that's compatible
with `Logger` from Ruby's standard library. In set, the library will no
longer log to stdout, and instead emit straight to the logger and defer
decision on what log level to print to it.

Addresses a request in #566.
2017-08-11 11:22:14 -07:00
Brandur
eb3671b067 Log Stripe-Account values with STRIPE_LOG
Hopefully the last tweak in a while, but a discussion on [1] tipped me
off that this was missing. Here we add a `Stripe-Account` for a request
and response to logging. Follows #566 and #567.

[1] https://github.com/stripe/stripe-node/issues/364
2017-08-03 14:02:06 -07:00
Brandur
441331bd82 Log num_retries with STRIPE_LOG
This one is minor, but I realized after shipping #566 that it would be
nice if the number of retries was also logged for every request. This
patch follows up #566 by adding that in.

I also renamed `retry_count` to `num_retries` because I subjectively
think this name is a little better.
2017-08-03 13:50:00 -07:00
Brandur
ce69d749e1 Implement STRIPE_LOG for stripe-ruby
Adds logging support for stripe-ruby in a similar way that we did it for
stripe-python [1], with the idea that users you can optionally get some
additional low-cost-to-configure logging for operational visibility or
debugging.

I made a few tweaks from the Python implementation (which I'll try to
contribute back to there):

* Added an elapsed parameter to responses so you can tell how long they
  lasted.
* Mixed in idempotency_key to all lines that users have a way to
  aggregate logs related to a request from start to finish.
* Standardized naming between different log lines as much as possible.
* Detect a TTY and produce output that's colorized and formatted.

[1] https://github.com/stripe/stripe-python/pull/269
2017-08-03 13:39:15 -07:00
Jacqueline Xu
43b78055aa Add upcoming invoice subscription items test 2017-08-01 17:22:29 -07:00
Brandur
00180c5f35 Power test suite with stripe-mock
Moves away from Committee and towards stripe-mock, an external
self-contained executable API stub server based on OpenAPI [1]. The
motivation here is that instead of making stripe-ruby a special
snowflake, we can use a single well-tested and feature-rich mock
implementation to drive every API's test suite.

[1] https://github.com/stripe/stripe-mock
2017-07-31 13:25:48 -07:00
Brandur
1417cb5bd1 Allow empty strings in API invocation parameters
Currently, with a normal API resource, you can unset fields by
specifying a `nil` to that field's setter:

``` ruby
c = Charge.retrieve('ch_123')
c.customer = nil
c.save
```

This actually gets serialized as the form `customer=` (i.e. an empty
string), but we had to use the empty string to handle unsets because
form encoding has no concept of a `nil`/`null`.

To try and prevent usage errors, we actually prevent you from setting
fields with an empty string:

``` ruby
c = Charge.retrieve('ch_123')
c.customer = '' # error! use nil instead
```

When specifying parameters though, this doesn't work anywhere nearly as
well because usage patterns like this are very common in Ruby:

``` ruby
charge_opts = {
  params[:amount],
  params[:currency],
  params[:customer],
}
charge = Charge.create(charge_opts)
```

Each one of `params` above may or may not be `nil`, so we've
traditionally filtered those fields out during the invocation of
`Charge.create`.

Recently, I suggested to Slava that we may be able to change this
behavior, and we ended up putting in a patch as part of #557. Users
brought to my attention that this would be far too disruptive of a
change in #560 though, and having thought about it more carefully, I
agree. There's also an argument that filtered `nil` values are just a
better API, especially in Ruby where patterns like the one above are
frequently in effect.

So the best thing I can think of currently is to leave things as they
were before #557, and just require that users use an explicit empty
string when passes in parameter hashes:

``` ruby
Charge.update(customer: '') # will try to unset customer
```

Empty strings will continue to error for `StripeObject` fields like they
always have.

I don't think this is a perfect solution by any means (the different
between values on `StripeObject` versus using parameters is weird), but
it's the least disruptive thing that I can think of right now that gets
us the functionality that we need for endpoints like
`/v1/invoices/upcoming`.

Fixes #560.
2017-07-27 13:47:25 -07:00
Brandur
24a1704f05 Improve error handling safety in the event of unrecognized OAuth error
It was brought up in #562 that in case we receive an OAuth error that we
don't know about, `specific_oauth_error` will fall through with a `nil`,
then picked up by `specific_api_error` which will always try to handle
the error as if it were a `Hash` (even if we know it's not!) and thus
lead to typing problems at runtime.

This patch throws a generic `OAuthError` in cases where a code comes
back that we don't recognize. I'm still crazy about the fact that we
don't have a better way of recognizing an OAuth error in particular, but
it should do the trick.
2017-07-27 09:01:53 -07:00
Yossef Mendelssohn
83444b60cd test handling of invalid_client error code
An error in OAuth deauthorization could return the error code of
`invalid_client`, but that isn't handled by the code. That leads to a
`TypeError` instead of a clean, understandable error.
2017-07-26 13:14:12 -04:00
Brandur
5786abcb7a Merge pull request #557 from stripe/slava-1156
Allows removing coupon via passing nil
2017-07-12 13:59:23 -07:00
Slava Akhmechet
3094199fa8 styling 2017-07-12 12:58:20 -07:00
Slava Akhmechet
b4308aaa7e Moving tests closer to the core 2017-07-12 12:54:53 -07:00
Brandur
d90c2b8e74 Include IDs of resources set as properties
Tweaks the serialization behavior so that when a resource is explicitly
set to a resource's field and that resource is subsequently saved, then
if it looks like the set resource was persisted we extract its ID and
send it up to the API.

By slight extension we also throw an `ArgumentError` if it looks like
that set resource was _not_ persisted because if the user set it
explicitly then it was probably not their intention to have it silently
ignored by the library in the event of a problem.
2017-07-11 12:37:19 -07:00
Slava Akhmechet
d0c4450e7e Allows removing coupon via passing nil 2017-07-07 11:27:26 -07:00
Olivier Bellone
78cd1d4f3d Add parameters when calling pay on an invoice 2017-06-27 14:01:08 +02:00