diff --git a/CHANGELOG.md b/CHANGELOG.md index 733613c..f6e853b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Current Version - +2.4.0 December 6, 2017 + - Add `Links` resource + 2.3.0 June 22, 2017 - Add api_version to Client config diff --git a/README.md b/README.md index 6a97a5a..34e7726 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ We currently expose the following resources to manage: * [`Customers`](#customers) * [`Merchants`](#merchants) * [`Orders`](#orders) +* [`Links`](#links) ### Accounts @@ -244,6 +245,42 @@ puts response # => Button::Response() ``` +### Links + +##### Create + +```ruby +require 'button' + +client = Button::Client.new('sk-XXX') + +response = client.links.create({ + url: "https://www.jet.com", + experience: { + btn_pub_ref: "my-pub-ref", + btn_pub_user: "user-id" + } +}) + +puts response +# => Button::Response() +``` + +##### Get Info + +```ruby +require 'button' + +client = Button::Client.new('sk-XXX') + +response = client.links.get_info({ + url: "https://www.jet.com" +}) + +puts response +# => Button::Response() +``` + ## Response 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. diff --git a/lib/button/client.rb b/lib/button/client.rb index 36cbc67..6925739 100644 --- a/lib/button/client.rb +++ b/lib/button/client.rb @@ -1,5 +1,6 @@ require 'button/resources/accounts' require 'button/resources/customers' +require 'button/resources/links' require 'button/resources/merchants' require 'button/resources/orders' require 'button/errors' @@ -28,6 +29,7 @@ def initialize(api_key, config = {}) @accounts = Accounts.new(api_key, config_with_defaults) @customers = Customers.new(api_key, config_with_defaults) + @links = Links.new(api_key, config_with_defaults) @merchants = Merchants.new(api_key, config_with_defaults) @orders = Orders.new(api_key, config_with_defaults) end @@ -44,7 +46,7 @@ def merge_defaults(config) } end - attr_reader :accounts, :customers, :merchants, :orders + attr_reader :accounts, :customers, :merchants, :orders, :links private :merge_defaults end end diff --git a/lib/button/resources/links.rb b/lib/button/resources/links.rb new file mode 100644 index 0000000..7b70bb5 --- /dev/null +++ b/lib/button/resources/links.rb @@ -0,0 +1,29 @@ +require 'button/resources/resource' + +module Button + # https://www.usebutton.com/developers/api-reference/ + # + class Links < Resource + def path + '/v1/links' + end + + # Create a Link + # + # @param [Hash] link the link to create + # @return [Button::Response] the API response + # + def create(link) + api_post(path, link) + end + + # Get information for Link + # + # @param [Hash] link the link to get information on + # @return [Button::Response] the API response + # + def get_info(link) + api_post(path + '/info', link) + end + end +end diff --git a/lib/button/version.rb b/lib/button/version.rb index fd24165..c9add87 100644 --- a/lib/button/version.rb +++ b/lib/button/version.rb @@ -1,3 +1,3 @@ module Button - VERSION = '2.3.0'.freeze + VERSION = '2.4.0'.freeze end diff --git a/test/button/resources/links_test.rb b/test/button/resources/links_test.rb new file mode 100644 index 0000000..18c2ff4 --- /dev/null +++ b/test/button/resources/links_test.rb @@ -0,0 +1,40 @@ +require File.expand_path('../../../test_helper', __FILE__) + +class LinksTest < Test::Unit::TestCase + def setup + @links = Button::Links.new( + 'sk-XXX', + secure: true, + timeout: nil, + hostname: 'api.usebutton.com', + port: 443 + ) + + @headers = { + 'Authorization' => 'Basic c2stWFhYOg==', + 'User-Agent' => Button::Resource::USER_AGENT + } + end + + def teardown + WebMock.reset! + end + + def test_create + stub_request(:post, 'https://api.usebutton.com/v1/links') + .with(body: '{"a":1}', headers: @headers) + .to_return(status: 200, body: '{ "meta": { "status": "ok" }, "object": { "a": 1 } }') + + response = @links.create(a: 1) + assert_equal(response.data[:a], 1) + end + + def test_get_info + stub_request(:post, 'https://api.usebutton.com/v1/links/info') + .with(body: '{"a":1}', headers: @headers) + .to_return(status: 200, body: '{ "meta": { "status": "ok" }, "object": { "a": 1 } }') + + response = @links.get_info(a: 1) + assert_equal(response.data[:a], 1) + end +end