jortt 2.0.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +5 -5
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +1 -3
  4. data/README.md +86 -48
  5. data/Rakefile +0 -1
  6. data/jortt.gemspec +24 -13
  7. data/lib/jortt.rb +21 -0
  8. data/lib/jortt/client.rb +121 -0
  9. data/lib/jortt/client/customers.rb +91 -0
  10. data/lib/jortt/client/error.rb +33 -0
  11. data/lib/jortt/client/invoices.rb +65 -0
  12. data/lib/jortt/client/ledger_accounts.rb +27 -0
  13. data/lib/jortt/client/version.rb +6 -0
  14. data/spec/fixtures/vcr_cassettes/Jortt/_client/1_1_1.yml +64 -0
  15. data/spec/fixtures/vcr_cassettes/Jortt_Client/configured/_customers/1_1_1_1.yml +64 -0
  16. data/spec/fixtures/vcr_cassettes/Jortt_Client/configured/_invoices/1_1_2_1.yml +64 -0
  17. data/spec/fixtures/vcr_cassettes/Jortt_Client/configured/_ledger_accounts/1_1_3_1.yml +64 -0
  18. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_create/faulty_payload/shows_a_nice_error.yml +413 -0
  19. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_create/valid_payload/creates_the_customer.yml +505 -0
  20. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_delete/deletes_the_customer.yml +505 -0
  21. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_direct_debit_mandate/sends_direct_debit_mandate_to_the_customer_or_responds_with_an_error_when_not_possible.yml +470 -0
  22. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_index/query/returns_the_queried_customers.yml +464 -0
  23. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_index/without_params/returns_customers.yml +415 -0
  24. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_show/returns_the_customer.yml +464 -0
  25. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_update/updates_the_customer.yml +603 -0
  26. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_create/creates_the_invoice.yml +170 -0
  27. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_download/returns_the_invoice_download_link.yml +168 -0
  28. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_index/invoice_status/returns_those_invoices.yml +776 -0
  29. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_index/query/returns_the_queried_invoices.yml +315 -0
  30. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_show/returns_the_invoice.yml +421 -0
  31. data/spec/fixtures/vcr_cassettes/Jortt_Client_LedgerAccounts/_index/returns_invoices.yml +118 -0
  32. data/spec/jortt/client/customers_spec.rb +104 -0
  33. data/spec/jortt/client/invoices_spec.rb +119 -0
  34. data/spec/jortt/client/ledger_accounts_spec.rb +19 -0
  35. data/spec/jortt/client_spec.rb +22 -0
  36. data/spec/{freemle_spec.rb → jortt_spec.rb} +2 -3
  37. data/spec/spec_helper.rb +22 -10
  38. metadata +99 -42
  39. data/.rubocop.yml +0 -26
  40. data/lib/freemle.rb +0 -22
  41. data/lib/freemle/client.rb +0 -69
  42. data/lib/freemle/client/resource.rb +0 -140
  43. data/lib/freemle/client/version.rb +0 -7
  44. data/spec/freemle/client/resource_spec.rb +0 -31
  45. data/spec/freemle/client_spec.rb +0 -43
@@ -1,26 +0,0 @@
1
- require: rubocop-rspec
2
-
3
- Style/AccessModifierIndentation:
4
- EnforcedStyle: outdent
5
-
6
- Style/AlignParameters:
7
- EnforcedStyle: with_fixed_indentation
8
-
9
- Style/DotPosition:
10
- EnforcedStyle: trailing
11
-
12
- Style/TrailingComma:
13
- EnforcedStyleForMultiline: comma
14
-
15
- Style/EmptyLinesAroundBody:
16
- Description: "Keeps track of empty lines around expression bodies."
17
- Enabled: false
18
-
19
- Style/RegexpLiteral:
20
- MaxSlashes: 0
21
-
22
- Style/HashSyntax:
23
- EnforcedStyle: ruby19
24
-
25
- Style/SpaceInsideHashLiteralBraces:
26
- EnforcedStyle: no_space
@@ -1,22 +0,0 @@
1
- # encoding: UTF-8
2
- require 'freemle/client'
3
- require 'freemle/client/version'
4
-
5
- ##
6
- # This module contains everything needed to setup a connection to the Freemle
7
- # API. It's only method returns a configured Freemle::Client.
8
- module Freemle
9
-
10
- # Convenient way to initialize a freemle client.
11
- #
12
- # @see {Freemle::Client.initialize}
13
- #
14
- # @return [ Freemle::Client ] a new freemle client instance
15
- #
16
- # @since 1.0.1
17
- def client(*args)
18
- Freemle::Client.new(*args)
19
- end
20
- module_function :client
21
-
22
- end
@@ -1,69 +0,0 @@
1
- # encoding: UTF-8
2
- require 'freemle/client/resource'
3
-
4
- module Freemle
5
- ##
6
- # This class is the main interface used to communicate with the Freemle API.
7
- # It is by the {Freemle} module to create configured instances.
8
- class Client
9
- BASE_URL = 'https://www.freemle.com/api'
10
-
11
- attr_accessor :base_url, :app_name, :api_key
12
-
13
- # Initialize a Freemle client.
14
- #
15
- # @example
16
- # Freemle::Client.new(
17
- # app_name: <application-name, chosen in freemle.com>
18
- # api_key: <api-key, as provided by freemle.com>
19
- # )
20
- #
21
- # @params [ Hash ] opts Options for the client,
22
- # optionally including base_url.
23
- #
24
- # @return [ Freemle::Client ]
25
- #
26
- # @since 1.0.0
27
- def initialize(opts)
28
- self.base_url = opts.fetch(:base_url, BASE_URL)
29
- self.app_name = opts.fetch(:app_name)
30
- self.api_key = opts.fetch(:api_key)
31
- end
32
-
33
- # Access the customer resource.
34
- #
35
- # @example
36
- # client.customers
37
- #
38
- # @return [ Freemle::Client::Resource ] entry to the customer resource.
39
- #
40
- # @since 1.0.0
41
- def customers
42
- @customers ||= new_resource(self, :customer, :customers)
43
- end
44
-
45
- # Access the invoice resource.
46
- #
47
- # @example
48
- # client.invoices
49
- #
50
- # @return [ Freemle::Client::Resource ] entry to the invoice resource.
51
- #
52
- # @since 1.0.0
53
- def invoices
54
- @invoices ||= new_resource(self, :invoice, :invoices)
55
- end
56
-
57
- private
58
-
59
- # Creates a freemle client resource based on the passed configuration
60
- #
61
- # @return [ Freemle::Client::Resource ] entry to a resource.
62
- #
63
- # @since 1.0.1
64
- def new_resource(*args)
65
- Freemle::Client::Resource.new(*args)
66
- end
67
-
68
- end
69
- end
@@ -1,140 +0,0 @@
1
- # encoding: UTF-8
2
- require 'rest-client'
3
-
4
- module Freemle # :nodoc:
5
- class Client # :nodoc:
6
- ##
7
- # This class is used by {Freemle::Client} internally.
8
- # It wraps rest API calls of a single resource,
9
- # so they can easily be used using the client DSL.
10
- #
11
- # @see { Freemle::Client.customer }
12
- class Resource
13
-
14
- # Details needed to connect to this resource, see
15
- # +Freemle::Client#initialize+
16
- #
17
- # @since 1.0.0
18
- attr_accessor :config
19
-
20
- # The singular form, posted to and returned when describing
21
- # a single member of this resource.
22
- #
23
- # @since 1.0.0
24
- attr_accessor :singular
25
-
26
- # Used to describe multiple members of this resource.
27
- #
28
- # @since 1.0.0
29
- attr_accessor :plural
30
-
31
- # Creates a new resource instance.
32
- #
33
- # @see { Freemle::Client#new_resource }
34
- #
35
- # @returns [ Freemle::Client::Resource ] bound to the resource
36
- # defined by +singular+ & +plural+
37
- #
38
- # @since 1.0.0
39
- def initialize(config, singular, plural)
40
- self.config = config
41
- self.singular = singular
42
- self.plural = plural
43
- end
44
-
45
- # Performs a search on this resource, given a query.
46
- #
47
- # @example
48
- # customers.search('Zilverline')
49
- #
50
- # @example
51
- # customers.search('Zilverline') do |response|
52
- # # Roll your own response handler
53
- # end
54
- #
55
- # @param [ Hash ] query A hash containing the fields to search for.
56
- # @param [ Proc ] block A custom response handler.
57
- #
58
- # @return [ Array<Hash> ] By default, a JSON parsed response body.
59
- #
60
- # @since 1.0.0
61
- def search(query, &block)
62
- block = default_handler unless block_given?
63
- request.get(params: {query: query}, &block)
64
- end
65
-
66
- # Persists a resource on freemle.com, given a payload.
67
- #
68
- # @example
69
- # customers.create(
70
- # company_name: "Zilverline B.V.",
71
- # address: {
72
- # street: "Cruquiusweg 109 F",
73
- # postal_code: "1019 AG",
74
- # city: "Amsterdam",
75
- # country_code: "NL"
76
- # }
77
- # )
78
- #
79
- # @example
80
- # customers.create(
81
- # company_name: "Zilverline B.V.",
82
- # address: {
83
- # street: "Cruquiusweg 109 F",
84
- # postal_code: "1019 AG",
85
- # city: "Amsterdam",
86
- # country_code: "NL"
87
- # }
88
- # ) do |response|
89
- # # Roll your own response handler
90
- # end
91
- #
92
- # @param [ Hash ] payload A hash containing the fields to set.
93
- # @param [ Proc ] block A custom response handler.
94
- #
95
- # @return [ Hash ] By default, a JSON parsed response body.
96
- #
97
- # @since 1.0.0
98
- def create(payload, &block)
99
- block = default_handler unless block_given?
100
- request.post(json.generate(singular => payload), &block)
101
- end
102
-
103
- private
104
-
105
- # Returns a response handler, which parses the response body as JSON.
106
- #
107
- # @return [ Proc ] Default response handler.
108
- #
109
- # @since 1.0.0
110
- def default_handler
111
- proc { |response| json.parse(response.body) }
112
- end
113
-
114
- # Returns a new request handler for this resource.
115
- #
116
- # @return [ RestClient::Resource ] A request handler.
117
- #
118
- # @since 1.0.0
119
- def request
120
- RestClient::Resource.new(
121
- "#{config.base_url}/#{plural}",
122
- user: config.app_name,
123
- password: config.api_key,
124
- )
125
- end
126
-
127
- # Returns the JSON library used in request/default-response handling.
128
- #
129
- # @return [ Class ] A JSON library.
130
- #
131
- # @since 1.0.0
132
- def json
133
- return @json if @json
134
- require 'json'
135
- @json = JSON
136
- end
137
-
138
- end
139
- end
140
- end
@@ -1,7 +0,0 @@
1
- # encoding: UTF-8
2
- module Freemle
3
- # Define the version of +Freemle::Client+
4
- class Client
5
- VERSION = '2.0.0'
6
- end
7
- end
@@ -1,31 +0,0 @@
1
- # encoding: UTF-8
2
- require 'spec_helper'
3
-
4
- describe Freemle::Client::Resource do
5
- let(:resource) do
6
- described_class.new(
7
- double('client', base_url: 'foo', app_name: 'app', api_key: 'secret'),
8
- :person,
9
- :people,
10
- )
11
- end
12
-
13
- describe '#search' do
14
- subject { resource.search('terms') }
15
- before do
16
- stub_request(:get, 'http://app:secret@foo/people?query=terms').
17
- to_return(status: 200, body: '{"people": []}')
18
- end
19
- it { should eq('people' => []) }
20
- end
21
-
22
- describe '#create' do
23
- subject { resource.create(foo: :bar) }
24
- before do
25
- stub_request(:post, 'http://app:secret@foo/people').
26
- with(body: '{"person":{"foo":"bar"}}').
27
- to_return(status: 201, body: '{"person_id": "123"}')
28
- end
29
- it { should eq('person_id' => '123') }
30
- end
31
- end
@@ -1,43 +0,0 @@
1
- # encoding: UTF-8
2
- require 'spec_helper'
3
-
4
- describe Freemle::Client do
5
- describe '#initialize' do
6
- subject { described_class.new(opts) }
7
- let(:opts) { {} }
8
-
9
- specify { expect { subject }.to raise_error(KeyError) }
10
-
11
- context 'given app_name' do
12
- before { opts[:app_name] = 'name' }
13
-
14
- specify { expect { subject }.to raise_error(KeyError) }
15
-
16
- context 'and api_key' do
17
- before { opts[:api_key] = 'secret' }
18
- it { should be_instance_of(described_class) }
19
- its(:base_url) { should eq(described_class::BASE_URL) }
20
- its(:app_name) { should eq('name') }
21
- its(:api_key) { should eq('secret') }
22
- end
23
- end
24
- end
25
-
26
- context 'configured' do
27
- let(:client) { described_class.new(app_name: 'app', api_key: 'secret') }
28
-
29
- describe '#customers' do
30
- subject { client.customers }
31
- it { should be_instance_of(described_class::Resource) }
32
- its(:singular) { should eq(:customer) }
33
- its(:plural) { should eq(:customers) }
34
- end
35
-
36
- describe '#invoices' do
37
- subject { client.invoices }
38
- it { should be_instance_of(described_class::Resource) }
39
- its(:singular) { should eq(:invoice) }
40
- its(:plural) { should eq(:invoices) }
41
- end
42
- end
43
- end