Skip to content

Commit d1481f7

Browse files
author
Tyler Nappy
authored
Merge pull request #18 from button/ty/DLC-4558
Added Links resource
2 parents f44b913 + f007d07 commit d1481f7

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Current Version
22
-
33

4+
2.4.0 December 6, 2017
5+
- Add `Links` resource
6+
47
2.3.0 June 22, 2017
58
- Add api_version to Client config
69

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ We currently expose the following resources to manage:
8383
* [`Customers`](#customers)
8484
* [`Merchants`](#merchants)
8585
* [`Orders`](#orders)
86+
* [`Links`](#links)
8687

8788
### Accounts
8889

@@ -244,6 +245,42 @@ puts response
244245
# => Button::Response()
245246
```
246247

248+
### Links
249+
250+
##### Create
251+
252+
```ruby
253+
require 'button'
254+
255+
client = Button::Client.new('sk-XXX')
256+
257+
response = client.links.create({
258+
url: "https://www.jet.com",
259+
experience: {
260+
btn_pub_ref: "my-pub-ref",
261+
btn_pub_user: "user-id"
262+
}
263+
})
264+
265+
puts response
266+
# => Button::Response()
267+
```
268+
269+
##### Get Info
270+
271+
```ruby
272+
require 'button'
273+
274+
client = Button::Client.new('sk-XXX')
275+
276+
response = client.links.get_info({
277+
url: "https://www.jet.com"
278+
})
279+
280+
puts response
281+
# => Button::Response()
282+
```
283+
247284
## Response
248285

249286
An instance of the `Button::Response` class will be returned by all API methods. It is used to read the response data as well as collect any meta data about the response, like potential next and previous cursors to more data in a paged endpoint.

lib/button/client.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'button/resources/accounts'
22
require 'button/resources/customers'
3+
require 'button/resources/links'
34
require 'button/resources/merchants'
45
require 'button/resources/orders'
56
require 'button/errors'
@@ -28,6 +29,7 @@ def initialize(api_key, config = {})
2829

2930
@accounts = Accounts.new(api_key, config_with_defaults)
3031
@customers = Customers.new(api_key, config_with_defaults)
32+
@links = Links.new(api_key, config_with_defaults)
3133
@merchants = Merchants.new(api_key, config_with_defaults)
3234
@orders = Orders.new(api_key, config_with_defaults)
3335
end
@@ -44,7 +46,7 @@ def merge_defaults(config)
4446
}
4547
end
4648

47-
attr_reader :accounts, :customers, :merchants, :orders
49+
attr_reader :accounts, :customers, :merchants, :orders, :links
4850
private :merge_defaults
4951
end
5052
end

lib/button/resources/links.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'button/resources/resource'
2+
3+
module Button
4+
# https://www.usebutton.com/developers/api-reference/
5+
#
6+
class Links < Resource
7+
def path
8+
'/v1/links'
9+
end
10+
11+
# Create a Link
12+
#
13+
# @param [Hash] link the link to create
14+
# @return [Button::Response] the API response
15+
#
16+
def create(link)
17+
api_post(path, link)
18+
end
19+
20+
# Get information for Link
21+
#
22+
# @param [Hash] link the link to get information on
23+
# @return [Button::Response] the API response
24+
#
25+
def get_info(link)
26+
api_post(path + '/info', link)
27+
end
28+
end
29+
end

lib/button/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Button
2-
VERSION = '2.3.0'.freeze
2+
VERSION = '2.4.0'.freeze
33
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require File.expand_path('../../../test_helper', __FILE__)
2+
3+
class LinksTest < Test::Unit::TestCase
4+
def setup
5+
@links = Button::Links.new(
6+
'sk-XXX',
7+
secure: true,
8+
timeout: nil,
9+
hostname: 'api.usebutton.com',
10+
port: 443
11+
)
12+
13+
@headers = {
14+
'Authorization' => 'Basic c2stWFhYOg==',
15+
'User-Agent' => Button::Resource::USER_AGENT
16+
}
17+
end
18+
19+
def teardown
20+
WebMock.reset!
21+
end
22+
23+
def test_create
24+
stub_request(:post, 'https://api.usebutton.com/v1/links')
25+
.with(body: '{"a":1}', headers: @headers)
26+
.to_return(status: 200, body: '{ "meta": { "status": "ok" }, "object": { "a": 1 } }')
27+
28+
response = @links.create(a: 1)
29+
assert_equal(response.data[:a], 1)
30+
end
31+
32+
def test_get_info
33+
stub_request(:post, 'https://api.usebutton.com/v1/links/info')
34+
.with(body: '{"a":1}', headers: @headers)
35+
.to_return(status: 200, body: '{ "meta": { "status": "ok" }, "object": { "a": 1 } }')
36+
37+
response = @links.get_info(a: 1)
38+
assert_equal(response.data[:a], 1)
39+
end
40+
end

0 commit comments

Comments
 (0)