freemle 1.0.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: 4e1013fba2d5336932c0827e8d014bce165a24f3
4
+ data.tar.gz: 79bfd250d6e893121d107ca72ca496e6ed1dfd1c
5
+ SHA512:
6
+ metadata.gz: 04ff928ebaff86b4ebad56f803f761389c484fb0a6b4ca977c2c48f0bfb26a042fdbd28519be66281becea281b94dc22340ca1f5d8379a48d712070b34b58966
7
+ data.tar.gz: 45427f788741f31dd3bd32dcade47882a853a2bb0b5740d27161ad92e4e644415dceafbc10229e76dc243414ea7c839bfe71dbab28987d8620c62440585c1691
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in freemle-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Bob Forma
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Freemle REST API client
2
+
3
+ [![Build Status](https://travis-ci.org/freemle/freemle-ruby.svg)](https://travis-ci.org/freemle/freemle-ruby)
4
+ [![Coverage Status](https://coveralls.io/repos/freemle/freemle-ruby/badge.png)](https://coveralls.io/r/freemle/freemle-ruby)
5
+ [![Code Climate](https://codeclimate.com/github/freemle/freemle-ruby.png)](https://codeclimate.com/github/freemle/freemle-ruby)
6
+
7
+ A Ruby interface to the [Freemle](https://www.freemle.com/) REST API.
8
+
9
+ ## Usage
10
+
11
+ To create a freemle client:
12
+ ```ruby
13
+ freemle = Freemle::Client.new(
14
+ app_name: "application-name-as-chosen-on-freemle.com",
15
+ api_key: "api-key-as-provided-by-freemle.com"
16
+ )
17
+ ```
18
+
19
+ ### Customers
20
+
21
+ Accessing customers:
22
+ ```ruby
23
+ freemle.customers.search('Freemle')
24
+ [{
25
+ company_name: 'Freemle',
26
+ address: {
27
+ street: "Cruquiusweg 109 F",
28
+ postal_code: "1019 AG",
29
+ city: "Amsterdam",
30
+ country_code: "NL"
31
+ }
32
+ },
33
+ company_name: 'Elmeerf',
34
+ address: {
35
+ street: "Freemleweg",
36
+ ...
37
+ }
38
+ }]
39
+ ```
40
+
41
+ Adding customers:
42
+ ```ruby
43
+ customers.create(
44
+ company_name: "Zilverline B.V.",
45
+ address: {
46
+ street: "Cruquiusweg 109 F",
47
+ postal_code: "1019 AG",
48
+ city: "Amsterdam",
49
+ country_code: "NL"
50
+ }
51
+ )
52
+ ```
53
+
54
+ ### Invoices
55
+
56
+ Adding invoices:
57
+ ```ruby
58
+ invoices.create(
59
+ customer_id: "123456789",
60
+ )
61
+ ```
62
+ ## Documentation
63
+
64
+ Check https://www.freemle.com/api-documentatie for more info.
65
+
66
+ ## Development
67
+
68
+ ### Running tests
69
+
70
+ `bundle install` and then `rake spec` or `rspec spec`.
71
+
72
+ ### Building the gem
73
+
74
+ `rake build` and then `rake install` to test it locally (`irb` followed
75
+ by `require 'freemle/client'` and do your stuff).
76
+
77
+ ### Releasing the gem
78
+
79
+ Make a fix, commit and push. Make sure the build is green. Then bump the
80
+ version (edit `lib/freemle/client/version.rb`). Now `rake release` and follow
81
+ the instructions (you need a rubygems.org account and permissions ;-)).
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'freemle/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'freemle'
8
+ spec.version = Freemle::Client::VERSION
9
+ spec.authors = ['Bob Forma', 'Steven Weller']
10
+ spec.email = %w(bforma@zilverline.com suweller@zilverline.com)
11
+ spec.summary = %w(Freemle.com REST API client)
12
+ spec.homepage = 'https://www.freemle.com/api-documentatie'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r(^bin/)) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r(^(test|spec|features)/))
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'rest-client', '~> 1.6'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'coveralls', '~> 0.7'
24
+ spec.add_development_dependency 'rake', '~> 10.3'
25
+ spec.add_development_dependency 'rspec', '~> 2.14'
26
+ spec.add_development_dependency 'webmock', '~> 1.17'
27
+ end
@@ -0,0 +1,55 @@
1
+ require "freemle/client/version"
2
+ require "freemle/resource"
3
+
4
+ module Freemle
5
+ class Client
6
+ BASE_URL = "https://www.freemle.com/api"
7
+
8
+ attr_accessor :base_url, :app_name, :api_key
9
+
10
+ # Initialize a Freemle client.
11
+ #
12
+ # @example
13
+ # Freemle::Client.new(
14
+ # app_name: <application-name, chosen in freemle.com>
15
+ # api_key: <api-key, as provided by freemle.com>
16
+ # )
17
+ #
18
+ # @params [ Hash ] opts Options for the client,
19
+ # optionally including base_url.
20
+ #
21
+ # @return [ Freemle::Client ]
22
+ #
23
+ # @since 1.0.0
24
+ def initialize(opts)
25
+ self.base_url = opts.fetch(:base_url, BASE_URL)
26
+ self.app_name = opts.fetch(:app_name)
27
+ self.api_key = opts.fetch(:api_key)
28
+ end
29
+
30
+ # Access the customer resource.
31
+ #
32
+ # @example
33
+ # client.customers
34
+ #
35
+ # @return [ Freemle::Resource ] entry to the customer resource.
36
+ #
37
+ # @since 1.0.0
38
+ def customers
39
+ @customers ||= Freemle::Resource.new(self, :customer, :customers)
40
+ end
41
+
42
+ # Access the invoice resource.
43
+ #
44
+ # @example
45
+ # client.invoices
46
+ #
47
+ # @return [ Freemle::Resource ] entry to the invoice resource.
48
+ #
49
+ # @since 1.0.0
50
+ def invoices
51
+ @invoices ||= Freemle::Resource.new(self, :invoice, :invoices)
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module Freemle
2
+ class Client
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,100 @@
1
+ require 'rest-client'
2
+
3
+ module Freemle
4
+ class Resource < Struct.new(:config, :singular, :plural)
5
+
6
+ # Performs a search on this resource, given a query.
7
+ #
8
+ # @example
9
+ # customers.search('Zilverline')
10
+ #
11
+ # @example
12
+ # customers.search('Zilverline') do |response|
13
+ # # Roll your own response handler
14
+ # end
15
+ #
16
+ # @param [ Hash ] query A hash containing the fields to search for.
17
+ # @param [ Proc ] block A custom response handler.
18
+ #
19
+ # @return [ Array<Hash> ] By default, a JSON parsed response body.
20
+ #
21
+ # @since 1.0.0
22
+ def search(query, &block)
23
+ block = default_handler unless block_given?
24
+ request.get(params: {query: query}, &block)
25
+ end
26
+
27
+ # Persists a resource on freemle.com, given a payload.
28
+ #
29
+ # @example
30
+ # customers.create(
31
+ # company_name: "Zilverline B.V.",
32
+ # address: {
33
+ # street: "Cruquiusweg 109 F",
34
+ # postal_code: "1019 AG",
35
+ # city: "Amsterdam",
36
+ # country_code: "NL"
37
+ # }
38
+ # )
39
+ #
40
+ # @example
41
+ # customers.create(
42
+ # company_name: "Zilverline B.V.",
43
+ # address: {
44
+ # street: "Cruquiusweg 109 F",
45
+ # postal_code: "1019 AG",
46
+ # city: "Amsterdam",
47
+ # country_code: "NL"
48
+ # }
49
+ # ) do |response|
50
+ # # Roll your own response handler
51
+ # end
52
+ #
53
+ # @param [ Hash ] payload A hash containing the fields to set.
54
+ # @param [ Proc ] block A custom response handler.
55
+ #
56
+ # @return [ Hash ] By default, a JSON parsed response body.
57
+ #
58
+ # @since 1.0.0
59
+ def create(payload, &block)
60
+ block = default_handler unless block_given?
61
+ request.post(json.generate({singular => payload}), &block)
62
+ end
63
+
64
+ private
65
+
66
+ # Returns a response handler, which parses the response body as JSON.
67
+ #
68
+ # @return [ Proc ] Default response handler.
69
+ #
70
+ # @since 1.0.0
71
+ def default_handler
72
+ Proc.new { |response| json.parse(response.body) }
73
+ end
74
+
75
+ # Returns a new request handler for this resource.
76
+ #
77
+ # @return [ RestClient::Resource ] A request handler.
78
+ #
79
+ # @since 1.0.0
80
+ def request
81
+ RestClient::Resource.new(
82
+ "#{config.base_url}/#{plural}",
83
+ user: config.app_name,
84
+ password: config.api_key
85
+ )
86
+ end
87
+
88
+ # Returns the JSON library used in request and default response handling.
89
+ #
90
+ # @return [ Class ] A JSON library.
91
+ #
92
+ # @since 1.0.0
93
+ def json
94
+ return @json if @json
95
+ require 'json'
96
+ @json = JSON
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ describe Freemle::Client do
4
+ describe "#initialize" do
5
+ subject { Freemle::Client.new(opts) }
6
+ let(:opts) { Hash.new }
7
+
8
+ specify { expect { subject }.to raise_error(KeyError) }
9
+
10
+ context "given app_name" do
11
+ before { opts[:app_name] = "name" }
12
+
13
+ specify { expect { subject }.to raise_error(KeyError) }
14
+
15
+ context "and api_key" do
16
+ before { opts[:api_key] = "secret" }
17
+
18
+ it { should be_instance_of(Freemle::Client) }
19
+ its(:base_url) { should eq(Freemle::Client::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) { Freemle::Client.new(app_name: "app", api_key: "secret") }
28
+
29
+ describe "#customers" do
30
+ subject { client.customers }
31
+
32
+ it { should be_instance_of(Freemle::Resource) }
33
+ its(:singular) { should eq(:customer) }
34
+ its(:plural) { should eq(:customers) }
35
+ end
36
+
37
+ describe "#invoices" do
38
+ subject { client.invoices }
39
+
40
+ it { should be_instance_of(Freemle::Resource) }
41
+ its(:singular) { should eq(:invoice) }
42
+ its(:plural) { should eq(:invoices) }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe Freemle::Resource do
4
+ let(:client) do
5
+ double("client", base_url: "foo", app_name: "app", api_key: "secret")
6
+ end
7
+ let(:resource) { Freemle::Resource.new(client, :customer, :customers) }
8
+
9
+ describe "#search" do
10
+ subject { resource.search("terms") }
11
+
12
+ before do
13
+ stub_request(:get, "http://app:secret@foo/customers?query=terms").
14
+ to_return(status: 200, body: '{"customers": []}')
15
+ end
16
+
17
+ it { should eq("customers" => []) }
18
+ end
19
+
20
+ describe "#create" do
21
+ subject { resource.create(foo: :bar) }
22
+
23
+ before do
24
+ stub_request(:post, "http://app:secret@foo/customers").
25
+ with(body: '{"customer":{"foo":"bar"}}').
26
+ to_return(status: 201, body: '{"customer_id": "123"}')
27
+ end
28
+
29
+ it { should eq("customer_id" => "123") }
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
9
+ # Coveralls.wear!
10
+
11
+ require "rspec"
12
+ require "webmock/rspec"
13
+
14
+ require "freemle/client"
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freemle
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bob Forma
8
+ - Steven Weller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.6'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.6'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.6'
42
+ - !ruby/object:Gem::Dependency
43
+ name: coveralls
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '0.7'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '0.7'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.3'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.3'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '2.14'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '2.14'
84
+ - !ruby/object:Gem::Dependency
85
+ name: webmock
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '1.17'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.17'
98
+ description:
99
+ email:
100
+ - bforma@zilverline.com
101
+ - suweller@zilverline.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - freemle-client.gemspec
114
+ - lib/freemle/client.rb
115
+ - lib/freemle/client/version.rb
116
+ - lib/freemle/resource.rb
117
+ - spec/freemle/client_spec.rb
118
+ - spec/freemle/resource_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: https://www.freemle.com/api-documentatie
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.0
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: "[\"Freemle.com\", \"REST\", \"API\", \"client\"]"
144
+ test_files:
145
+ - spec/freemle/client_spec.rb
146
+ - spec/freemle/resource_spec.rb
147
+ - spec/spec_helper.rb