baremetrics_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c1bfbcb23380d81c89c0107640b7577caaa80df2
4
+ data.tar.gz: cae7ad5b59dc336d88d63b27b4806ad58616ae70
5
+ SHA512:
6
+ metadata.gz: 252dc2c3e1894303baf12896ae5713abe8c5b11890ce612e705c9f5ecd9feaf2f3b34bf343491e24e09e7caafb0983ee898f3300b6c433065b4b0c4233a73769
7
+ data.tar.gz: 46cad942c6c0b470d806f9df297a5dea4406292166634af955afff788e97c2ba9c43482ea3fc005bff06668369ca9572127a40ce15da9a109027b72588d04dab
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in baremetrics.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Third Blink Software Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Baremetrics
2
+
3
+ This is a Ruby Gem for the Baremetrics API developed by Rewind.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'baremetrics_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install baremetrics_api
20
+
21
+ ## Usage
22
+
23
+ **Initializing the Client**
24
+
25
+ This gem uses a client model for communication with the API. You initialize a client first with the desired configuration and then use it for making requests:
26
+
27
+ ```ruby
28
+ client = BaremetricsAPI::Client.new(api_key: '1234')
29
+
30
+ client.list_sources
31
+ ```
32
+
33
+ The only required configuration parameter to initialize the client is the API key using this constructor. Unprovided parameters will have their default value set.
34
+
35
+ You can also initialize the client using a configuration block to have it globally configured:
36
+
37
+ ```ruby
38
+ BaremetricsAPI.client.configure do |config|
39
+ config.api_key = '123'
40
+ config.sandbox = false
41
+ config.response_limit = 30
42
+ config.log_traffic = false
43
+ end
44
+
45
+ BaremetricsAPI.client.list_source
46
+ ```
47
+
48
+ Note that you must specify all the configuration parameters when using this method as they will not have a default value.
49
+
50
+ **Configuration Parameters**
51
+
52
+ `api_key`: String - The Baremetrics API key. Can be sandbox or production.
53
+
54
+ `sandbox`: Boolean - Whether to use the sandbox or production API endpoint. (Default: false)
55
+
56
+ `response_limit`: Integer - The amount of items to return per page for GET requests (Default: 30)
57
+
58
+ `log_traffic`: Boolean - Output a log of requests and responses from the Baremetrics API (Default: false)
59
+
60
+ **Making Requests**
61
+
62
+ The gem tries to best match its method names to the name of the corresponding API call in the Baremetrics documentation.
63
+ All API calls are available as methods on the client object.
64
+ To list which calls are available run: `client.methods`
65
+
66
+ The arguments required for each endpoint are named keywords. We tried to best match the names of the parameters as described in the Baremetrics API documentation.
67
+
68
+ An example call would be:
69
+
70
+ ```ruby
71
+ client.list_customers(source_id: '123')
72
+ ```
73
+
74
+ The response to all calls is raw JSON that is returned from the Baremetrics API.
75
+
76
+ **Rate Limiting**
77
+
78
+ The Baremetrics API has a rate limit which refreshes per-hour. When the rate limit is exceeded, this gem will throw the following exception `Error::RateLimitExeeded`.
79
+
80
+ You should rescue this exception and retry after an hour when the rate limit is refreshed.
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rewindit/baremetrics.
85
+
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'baremetrics_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'baremetrics_api'
8
+ spec.version = BaremetricsAPI::VERSION
9
+ spec.authors = ['Third Blink Software Inc']
10
+ spec.email = ['help@rewindit.io']
11
+
12
+ spec.summary = 'Ruby client library for Baremetrics V1 API'
13
+ spec.description = 'Provides an easy way to interact with the Baremetrics API in any application.'
14
+ spec.homepage = 'https://github.com/rewindit/baremetrics_api'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+
28
+ spec.add_dependency 'faraday', '~> 0.9'
29
+ spec.add_dependency 'faraday_middleware', '~> 0.11'
30
+ spec.add_dependency 'httpclient', '~> 2.8'
31
+ spec.add_dependency 'activesupport', '~> 4.2'
32
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "baremetrics_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,94 @@
1
+ require 'baremetrics_api/configuration'
2
+ require 'baremetrics_api/endpoint/account'
3
+ require 'baremetrics_api/constants'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+ require 'faraday/rate_checker'
7
+ require 'httpclient'
8
+ require 'baremetrics_api/errors'
9
+ require 'logger'
10
+ require 'active_support/core_ext/hash'
11
+ require 'json'
12
+
13
+ module BaremetricsAPI
14
+ class Client
15
+ attr_reader :configuration
16
+
17
+ def initialize(options = nil)
18
+ @configuration = nil
19
+ append_request_methods_to_class
20
+
21
+ return if options.nil?
22
+
23
+ @configuration = Configuration.new(options)
24
+
25
+ ensure_valid_configuration
26
+ end
27
+
28
+ # Configure the API client
29
+ # @yield [Configuration] the configuration
30
+ # @example Basic configuration
31
+ # BaremetricsAPI.client.configure do |config|
32
+ # config.api_key = 'test123'
33
+ # config.response_limit = '30'
34
+ # end
35
+ def configure
36
+ raise Error::AlreadyConfigured unless @configuration.nil?
37
+
38
+ @configuration = Configuration.new
39
+ yield(@configuration)
40
+
41
+ ensure_valid_configuration
42
+ end
43
+
44
+ # Returns the raw Faraday connection configured using the current configuration
45
+ def connection
46
+ ensure_valid_configuration
47
+ host = configuration.sandbox ? Constants::SANDBOX_API_HOST : Constants::API_HOST
48
+ Faraday.new(host, headers: { authorization: "Bearer #{configuration.api_key}", content_type: 'application/json', accept: 'application/json' }) do |f|
49
+ f.request :json
50
+ f.response :logger if configuration.log_traffic
51
+ f.use Faraday::RateChecker
52
+ f.adapter :httpclient
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def ensure_valid_configuration
59
+ if configuration.nil? || !configuration.valid?
60
+ # nil out the configuration so we can reconfigure
61
+ @configuration = nil
62
+ raise Error::MissingConfigurationKeys
63
+ else
64
+ # Lock in the configuration so it can't be changed during app usage
65
+ @configuration.freeze
66
+ end
67
+ end
68
+
69
+ # The methods below are responsible for appending methods from the
70
+ # endpoint classes onto the client. This allows for simple interaction
71
+ # with the gem.
72
+ # For example: client.retrieve_account instead of
73
+ # BaremetricsAPI::Endpoint::Account.new(client).retrieve_account
74
+
75
+ def append_request_methods_to_class
76
+ Constants::ENDPOINT_CLASSES.each do |endpoint_class|
77
+ endpoint_instance = endpoint_class.new(self)
78
+ create_methods_from_instance(endpoint_instance)
79
+ end
80
+ end
81
+
82
+ def create_methods_from_instance(instance)
83
+ instance.public_methods(false).each do |method_name|
84
+ add_method(instance, method_name)
85
+ end
86
+ end
87
+
88
+ def add_method(instance, method_name)
89
+ define_singleton_method(method_name) do |*args|
90
+ instance.public_send(method_name, *args)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,27 @@
1
+ require 'baremetrics_api/constants'
2
+
3
+ module BaremetricsAPI
4
+ class Configuration
5
+ CONFIG_KEYS = Constants::CONFIG_KEYS
6
+
7
+ attr_accessor *CONFIG_KEYS
8
+
9
+ def initialize(configuration = nil)
10
+ return unless configuration.is_a?(Hash)
11
+
12
+ configuration.each do |name, value|
13
+ send("#{name}=", value)
14
+ end
15
+
16
+ # Default to false
17
+ self.sandbox = false if sandbox.nil?
18
+ self.log_traffic = false if log_traffic.nil?
19
+ self.response_limit = Constants::DEFAULT_RESPONSE_LIMIT
20
+ end
21
+
22
+ # A configuration is valid if none of the keys are nil
23
+ def valid?
24
+ CONFIG_KEYS.none? { |key| send(key).nil? }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ require 'baremetrics_api/endpoint/account'
2
+ require 'baremetrics_api/endpoint/annotations'
3
+ require 'baremetrics_api/endpoint/charges'
4
+ require 'baremetrics_api/endpoint/customers'
5
+ require 'baremetrics_api/endpoint/events'
6
+ require 'baremetrics_api/endpoint/goals'
7
+ require 'baremetrics_api/endpoint/metrics'
8
+ require 'baremetrics_api/endpoint/plans'
9
+ require 'baremetrics_api/endpoint/sources'
10
+ require 'baremetrics_api/endpoint/subscriptions'
11
+ require 'baremetrics_api/endpoint/users'
12
+
13
+ module BaremetricsAPI
14
+ class Constants
15
+ API_HOST = 'https://api.baremetrics.com/v1'.freeze
16
+ SANDBOX_API_HOST = 'https://api-sandbox.baremetrics.com/v1'.freeze
17
+ CONFIG_KEYS = [:api_key, :response_limit, :sandbox, :log_traffic].freeze
18
+ DEFAULT_RESPONSE_LIMIT = 30
19
+
20
+ # Endpoints
21
+ ENDPOINT_CLASSES = [BaremetricsAPI::Endpoint::Account, BaremetricsAPI::Endpoint::Annotations, BaremetricsAPI::Endpoint::Charges,
22
+ BaremetricsAPI::Endpoint::Customers, BaremetricsAPI::Endpoint::Events, BaremetricsAPI::Endpoint::Goals,
23
+ BaremetricsAPI::Endpoint::Metrics, BaremetricsAPI::Endpoint::Plans, BaremetricsAPI::Endpoint::Sources,
24
+ BaremetricsAPI::Endpoint::Subscriptions, BaremetricsAPI::Endpoint::Users].freeze
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Account
4
+ PATH = 'account'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def get_account
11
+ JSON.parse(account_request.body).with_indifferent_access
12
+ end
13
+
14
+ private
15
+
16
+ def account_request
17
+ @client.connection.get do |req|
18
+ req.url PATH
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,59 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Annotations
4
+ PATH = 'annotations'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_annotations(page: nil)
11
+ JSON.parse(list_annotations_request(page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_annotation(id:)
15
+ JSON.parse(show_annotation_request(id).body).with_indifferent_access
16
+ end
17
+
18
+ def create_annotation(annotation_params:)
19
+ JSON.parse(create_annotation_request(annotation_params).body).with_indifferent_access
20
+ end
21
+
22
+ def delete_annotation(id:)
23
+ JSON.parse(delete_annotation_request(id).body).with_indifferent_access
24
+ end
25
+
26
+ private
27
+
28
+ def list_annotations_request(page)
29
+ query_params = {
30
+ per_page: @client.configuration.response_limit
31
+ }
32
+
33
+ query_params[:page] = page unless page.nil?
34
+
35
+ @client.connection.get do |req|
36
+ req.url PATH
37
+ req.params = query_params
38
+ end
39
+ end
40
+
41
+ def show_annotation_request(id)
42
+ @client.connection.get do |req|
43
+ req.url "#{PATH}/#{id}"
44
+ end
45
+ end
46
+
47
+ def create_annotation_request(annotation_params)
48
+ @client.connection.post do |req|
49
+ req.url PATH
50
+ req.body = annotation_params
51
+ end
52
+ end
53
+
54
+ def delete_annotation_request(id)
55
+ @client.connection.delete "#{PATH}/#{id}"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,51 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Charges
4
+ PATH = 'charges'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_charges(source_id:, page: nil)
11
+ JSON.parse(list_charges_request(source_id, page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_charge(source_id:, oid:)
15
+ JSON.parse(show_charge_request(source_id, oid).body).with_indifferent_access
16
+ end
17
+
18
+ def create_charge(source_id:, charge_params:)
19
+ JSON.parse(create_charge_request(source_id, charge_params).body).with_indifferent_access
20
+ end
21
+
22
+ private
23
+
24
+ def list_charges_request(source_id)
25
+ query_params = {
26
+ per_page: @client.configuration.response_limit
27
+ }
28
+
29
+ query_params[:page] = page unless page.nil?
30
+
31
+ @client.connection.get do |req|
32
+ req.url "#{source_id}/#{PATH}"
33
+ req.params = query_params
34
+ end
35
+ end
36
+
37
+ def show_charge_request(source_id, oid)
38
+ @client.connection.get do |req|
39
+ req.url "#{source_id}/#{PATH}/#{oid}"
40
+ end
41
+ end
42
+
43
+ def create_charge_request(source_id, charge_params)
44
+ client.connection.post do |req|
45
+ req.url "#{source_id}/#{PATH}"
46
+ req.body = charge_params
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,88 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Customers
4
+ PATH = 'customers'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_customers(source_id: nil, search: nil, page: nil)
11
+ JSON.parse(list_customers_request(source_id, search, page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_customer(source_id:, oid:)
15
+ JSON.parse(show_customer_request(source_id, oid).body).with_indifferent_access
16
+ end
17
+
18
+ def list_customer_events(source_id:, oid:, page: nil)
19
+ JSON.parse(list_customer_events_request(source_id, oid, page).body).with_indifferent_access
20
+ end
21
+
22
+ def update_customer(customer_oid:, source_id:, customer_params:)
23
+ JSON.parse(update_customer_request(customer_oid, source_id, customer_params).body).with_indifferent_access
24
+ end
25
+
26
+ def create_customer(source_id:, customer_params:)
27
+ JSON.parse(create_customer_request(source_id, customer_params).body).with_indifferent_access
28
+ end
29
+
30
+ def delete_customer(oid:, source_id:)
31
+ JSON.parse(delete_customer_request(oid, source_id).body).with_indifferent_access
32
+ end
33
+
34
+ private
35
+
36
+ def list_customers_request(source_id, search, page)
37
+ query_params = {
38
+ per_page: @client.configuration.response_limit
39
+ }
40
+
41
+ query_params[:search] = search unless search.nil?
42
+ query_params[:page] = page unless page.nil?
43
+
44
+ @client.connection.get do |req|
45
+ req.url source_id.nil? ? PATH : "#{source_id}/#{PATH}"
46
+ req.params = query_params
47
+ end
48
+ end
49
+
50
+ def show_customer_request(source_id, oid)
51
+ @client.connection.get do |req|
52
+ req.url "#{source_id}/#{PATH}/#{oid}"
53
+ end
54
+ end
55
+
56
+ def list_customer_events_request(source_id, oid, page)
57
+ query_params = {
58
+ per_page: @client.configuration.response_limit
59
+ }
60
+
61
+ query_params[:page] = page unless page.nil?
62
+
63
+ @client.connection.get do |req|
64
+ req.url "#{source_id}/#{PATH}/#{oid}/events"
65
+ req.params = query_params
66
+ end
67
+ end
68
+
69
+ def update_customer_request(customer_oid, source_id, customer_params)
70
+ @client.connection.put do |req|
71
+ req.url "#{source_id}/#{PATH}/#{customer_oid}"
72
+ req.body = customer_params
73
+ end
74
+ end
75
+
76
+ def create_customer_request(source_id, customer_params)
77
+ @client.connection.post do |req|
78
+ req.url "#{source_id}/#{PATH}"
79
+ req.body = customer_params
80
+ end
81
+ end
82
+
83
+ def delete_customer_request(oid, source_id)
84
+ @client.connection.delete "#{source_id}/#{PATH}/#{oid}"
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,41 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Events
4
+ PATH = 'events'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_events(source_id:, live_stream: false, page: nil)
11
+ JSON.parse(list_events_request(source_id, live_stream, page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_event(source_id:, id:)
15
+ JSON.parse(show_event_request(source_id, id).body).with_indifferent_access
16
+ end
17
+
18
+ private
19
+
20
+ def list_events_request(source_id, live_stream, page)
21
+ query_params = {
22
+ per_page: @client.configuration.response_limit,
23
+ live_stream: live_stream
24
+ }
25
+
26
+ query_params[:page] = page unless page.nil?
27
+
28
+ @client.connection.get do |req|
29
+ req.url "#{source_id}/#{PATH}"
30
+ req.params = query_params
31
+ end
32
+ end
33
+
34
+ def show_event_request(source_id, id)
35
+ @client.connection.get do |req|
36
+ req.url "#{source_id}/#{PATH}/#{id}"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,59 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Goals
4
+ PATH = 'goals'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_goals(page: nil)
11
+ JSON.parse(list_goals_request(page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_goal(id:)
15
+ JSON.parse(show_goal_request(id).body).with_indifferent_access
16
+ end
17
+
18
+ def create_goal(goal_params:)
19
+ JSON.parse(create_goal_request(goal_params).body).with_indifferent_access
20
+ end
21
+
22
+ def delete_goal(id:)
23
+ JSON.parse(delete_goal_request(id).body).with_indifferent_access
24
+ end
25
+
26
+ private
27
+
28
+ def list_goals_request(page)
29
+ query_params = {
30
+ per_page: @client.configuration.response_limit
31
+ }
32
+
33
+ query_params[:page] = page unless page.nil?
34
+
35
+ @client.connection.get do |req|
36
+ req.url PATH
37
+ req.params = query_params
38
+ end
39
+ end
40
+
41
+ def show_goal_request(id)
42
+ @client.connection.get do |req|
43
+ req.url "#{PATH}/#{id}"
44
+ end
45
+ end
46
+
47
+ def create_goal_request(goal_params)
48
+ @client.connection.post do |req|
49
+ req.url PATH
50
+ req.body = goal_params
51
+ end
52
+ end
53
+
54
+ def delete_goal_request(id)
55
+ @client.connection.delete "#{PATH}/#{id}"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,90 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Metrics
4
+ PATH = 'metrics'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def show_summary(start_date:, end_date:, page: nil)
11
+ JSON.parse(show_summary_request(start_date, end_date, page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_metric(metric:, start_date:, end_date:, compare_to: 30, page: nil)
15
+ JSON.parse(show_metric_request(metric, start_date, end_date, compare_to, page).body).with_indifferent_access
16
+ end
17
+
18
+ def show_customers(metric:, start_date:, end_date:, page: nil)
19
+ JSON.parse(show_customers_request(metric, start_date, end_date, page).body).with_indifferent_access
20
+ end
21
+
22
+ def show_plan_breakout(metric:, start_date:, end_date:, page: nil)
23
+ JSON.parse(show_plan_breakout_request(metric, start_date, end_date, page).body).with_indifferent_access
24
+ end
25
+
26
+ private
27
+
28
+ def show_summary_request(start_date, end_date, page)
29
+ query_params = {
30
+ per_page: @client.configuration.response_limit,
31
+ start_date: start_date,
32
+ end_date: end_date
33
+ }
34
+
35
+ query_params[:page] = page unless page.nil?
36
+
37
+ @client.connection.get do |req|
38
+ req.url PATH
39
+ req.params = query_params
40
+ end
41
+ end
42
+
43
+ def show_metric_request(metric, start_date, end_date, compare_to, page)
44
+ query_params = {
45
+ per_page: @client.configuration.response_limit,
46
+ start_date: start_date,
47
+ end_date: end_date,
48
+ compare_to: compare_to
49
+ }
50
+
51
+ query_params[:page] = page unless page.nil?
52
+
53
+ @client.connection.get do |req|
54
+ req.url "#{PATH}/#{metric}"
55
+ req.params = query_params
56
+ end
57
+ end
58
+
59
+ def show_customers_request(metric, start_date, end_date)
60
+ query_params = {
61
+ per_page: @client.configuration.response_limit,
62
+ start_date: start_date,
63
+ end_date: end_date
64
+ }
65
+
66
+ query_params[:page] = page unless page.nil?
67
+
68
+ @client.connection.get do |req|
69
+ req.url "#{PATH}/#{metric}/customers"
70
+ req.params = query_params
71
+ end
72
+ end
73
+
74
+ def show_plan_breakout_request(metric, start_date, end_date)
75
+ query_params = {
76
+ per_page: @client.configuration.response_limit,
77
+ start_date: start_date,
78
+ end_date: end_date
79
+ }
80
+
81
+ query_params[:page] = page unless page.nil?
82
+
83
+ @client.connection.get do |req|
84
+ req.url "#{PATH}/#{metric}/plans"
85
+ req.params = query_params
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,71 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Plans
4
+ PATH = 'plans'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_plans(source_id: nil, search: nil, page: nil)
11
+ JSON.parse(list_plans_request(source_id, search, page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_plan(source_id:, oid:)
15
+ JSON.parse(show_plan_request(source_id, oid).body).with_indifferent_access
16
+ end
17
+
18
+ def update_plan(plan_oid:, source_id:, plan_params:)
19
+ JSON.parse(update_plan_request(plan_oid, source_id, plan_params).body).with_indifferent_access
20
+ end
21
+
22
+ def create_plan(source_id:, plan_params:)
23
+ JSON.parse(create_plan_request(source_id, plan_params).body).with_indifferent_access
24
+ end
25
+
26
+ def delete_plan(oid:, source_id:)
27
+ JSON.parse(delete_plan_request(oid, source_id).body).with_indifferent_access
28
+ end
29
+
30
+ private
31
+
32
+ def list_plans_request(source_id, search, page)
33
+ query_params = {
34
+ per_page: @client.configuration.response_limit
35
+ }
36
+
37
+ query_params[:search] = search unless search.nil?
38
+ query_params[:page] = page unless page.nil?
39
+
40
+ @client.connection.get do |req|
41
+ req.url source_id.nil? ? PATH : "#{source_id}/#{PATH}"
42
+ req.params = query_params
43
+ end
44
+ end
45
+
46
+ def show_plan_request(source_id, oid)
47
+ @client.connection.get do |req|
48
+ req.url "#{source_id}/#{PATH}/#{oid}"
49
+ end
50
+ end
51
+
52
+ def update_plan_request(plan_oid, source_id, plan_params)
53
+ @client.connection.put do |req|
54
+ req.url "#{source_id}/#{PATH}/#{plan_oid}"
55
+ req.body = plan_params
56
+ end
57
+ end
58
+
59
+ def create_plan_request(source_id, plan_params)
60
+ @client.connection.post do |req|
61
+ req.url "#{source_id}/#{PATH}"
62
+ req.body = plan_params
63
+ end
64
+ end
65
+
66
+ def delete_plan_request(oid, source_id)
67
+ @client.connection.delete "#{source_id}/#{PATH}/#{oid}"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,23 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Sources
4
+ PATH = 'sources'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_sources
11
+ JSON.parse(sources_request.body).with_indifferent_access
12
+ end
13
+
14
+ private
15
+
16
+ def sources_request
17
+ @client.connection.get do |req|
18
+ req.url PATH
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,82 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Subscriptions
4
+ PATH = 'subscriptions'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_subscriptions(source_id: nil, customer_oid: nil, page: nil)
11
+ JSON.parse(list_subscriptions_request(source_id, customer_oid, page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_subscription(source_id:, oid:)
15
+ JSON.parse(show_subscription_request(source_id, oid).body).with_indifferent_access
16
+ end
17
+
18
+ def update_subscription(subscription_oid:, source_id:, subscription_params:)
19
+ JSON.parse(update_subscription_request(subscription_oid, source_id, subscription_params).body).with_indifferent_access
20
+ end
21
+
22
+ def cancel_subscription(subscription_oid:, source_id:, canceled_at:)
23
+ JSON.parse(cancel_subscription_request(subscription_oid, source_id, canceled_at).body).with_indifferent_access
24
+ end
25
+
26
+ def create_subscription(source_id:, subscription_params:)
27
+ JSON.parse(create_subscription_request(source_id, subscription_params).body).with_indifferent_access
28
+ end
29
+
30
+ def delete_subscription(oid:, source_id:)
31
+ JSON.parse(delete_subscription_request(oid, source_id).body).with_indifferent_access
32
+ end
33
+
34
+ private
35
+
36
+ def list_subscriptions_request(source_id, customer_oid, page)
37
+ query_params = {
38
+ per_page: @client.configuration.response_limit,
39
+ customer_oid: customer_oid
40
+ }
41
+
42
+ query_params[:page] = page unless page.nil?
43
+
44
+ @client.connection.get do |req|
45
+ req.url source_id.nil? ? PATH : "#{source_id}/#{PATH}"
46
+ req.params = query_params
47
+ end
48
+ end
49
+
50
+ def show_subscription_request(source_id, oid)
51
+ @client.connection.get do |req|
52
+ req.url "#{source_id}/#{PATH}/#{oid}"
53
+ end
54
+ end
55
+
56
+ def update_subscription_request(subscription_oid, source_id, subscription_params)
57
+ @client.connection.put do |req|
58
+ req.url "#{source_id}/#{PATH}/#{subscription_oid}"
59
+ req.body = subscription_params
60
+ end
61
+ end
62
+
63
+ def cancel_subscription_request(subscription_oid, source_id, canceled_at)
64
+ @client.connection.put do |req|
65
+ req.url "#{source_id}/#{PATH}/#{subscription_oid}/cancel"
66
+ req.body = { canceled_at: canceled_at }
67
+ end
68
+ end
69
+
70
+ def create_subscription_request(source_id, subscription_params)
71
+ @client.connection.post do |req|
72
+ req.url "#{source_id}/#{PATH}"
73
+ req.body = subscription_params
74
+ end
75
+ end
76
+
77
+ def delete_subscription_request(oid, source_id)
78
+ @client.connection.delete "#{source_id}/#{PATH}/#{oid}"
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,40 @@
1
+ module BaremetricsAPI
2
+ module Endpoint
3
+ class Users
4
+ PATH = 'users'.freeze
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def list_users(page: nil)
11
+ JSON.parse(list_users_request(page).body).with_indifferent_access
12
+ end
13
+
14
+ def show_user(id:)
15
+ JSON.parse(show_user_request(id).body).with_indifferent_access
16
+ end
17
+
18
+ private
19
+
20
+ def list_users_request(page)
21
+ query_params = {
22
+ per_page: @client.configuration.response_limit
23
+ }
24
+
25
+ query_params[:page] = page unless page.nil?
26
+
27
+ @client.connection.get do |req|
28
+ req.url PATH
29
+ req.params = query_params
30
+ end
31
+ end
32
+
33
+ def show_user_request(id)
34
+ @client.connection.get do |req|
35
+ req.url "#{PATH}/#{id}"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ module BaremetricsAPI
2
+ module Error
3
+ class MissingConfigurationKeys < StandardError
4
+ def initialize(msg = 'One or more required configuration keys are missing or invalid')
5
+ super
6
+ end
7
+ end
8
+
9
+ class AlreadyConfigured < StandardError
10
+ def initialize(msg = 'Client has already been configured')
11
+ super
12
+ end
13
+ end
14
+
15
+ class RateLimitExeeded < StandardError
16
+ def initialize(msg = 'You have exeeded your rate limit')
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module BaremetricsAPI
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'baremetrics_api/version'
2
+ require 'baremetrics_api/client'
3
+
4
+ module BaremetricsAPI
5
+ def self.client
6
+ @client ||= BaremetricsAPI::Client.new
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ require 'baremetrics_api/errors'
2
+
3
+ module Faraday
4
+ class RateChecker < Faraday::Middleware
5
+ def initialize(app)
6
+ super(app)
7
+ end
8
+
9
+ def call(request_env)
10
+ @app.call(request_env).on_complete do |response_env|
11
+ if response_env[:body].include?('You have exceeded your rate limit')
12
+ raise BaremetricsAPI::Error::RateLimitExeeded
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baremetrics_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Third Blink Software Inc
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httpclient
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.8'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.2'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.2'
111
+ description: Provides an easy way to interact with the Baremetrics API in any application.
112
+ email:
113
+ - help@rewindit.io
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - baremetrics.gemspec
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/baremetrics_api.rb
129
+ - lib/baremetrics_api/client.rb
130
+ - lib/baremetrics_api/configuration.rb
131
+ - lib/baremetrics_api/constants.rb
132
+ - lib/baremetrics_api/endpoint/account.rb
133
+ - lib/baremetrics_api/endpoint/annotations.rb
134
+ - lib/baremetrics_api/endpoint/charges.rb
135
+ - lib/baremetrics_api/endpoint/customers.rb
136
+ - lib/baremetrics_api/endpoint/events.rb
137
+ - lib/baremetrics_api/endpoint/goals.rb
138
+ - lib/baremetrics_api/endpoint/metrics.rb
139
+ - lib/baremetrics_api/endpoint/plans.rb
140
+ - lib/baremetrics_api/endpoint/sources.rb
141
+ - lib/baremetrics_api/endpoint/subscriptions.rb
142
+ - lib/baremetrics_api/endpoint/users.rb
143
+ - lib/baremetrics_api/errors.rb
144
+ - lib/baremetrics_api/version.rb
145
+ - lib/faraday/rate_checker.rb
146
+ homepage: https://github.com/rewindit/baremetrics_api
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.5.1
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Ruby client library for Baremetrics V1 API
170
+ test_files: []