freemle-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1139b760f58e0601e519d18c248b68b3df74b542
4
+ data.tar.gz: a220215243170d854787238ff73542ce058cbaf2
5
+ SHA512:
6
+ metadata.gz: abc7703aa8921e96a5b9c82d47aa0dee09942b29973c1ebcb0b7fabd275930ead1cbca3b78f2d2d197cb88b9d2a5cf5d7d6707cccfa6bef57814ab1532679277
7
+ data.tar.gz: 4142ea8746a7031b13a786d6801ff9a94acf4e86c4d887071bf3c263c9a298a59575a1448897f34314012645e53078850ea4ac3bb5bee49f9c8217f5edbf4d6f
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/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,11 @@
1
+ # Freemle REST API client
2
+
3
+ Usage:
4
+
5
+ ```
6
+ Freemle::Client.base_url = "https://www.freemle.com/api"
7
+ Freemle::Client.app_name = "your_application_name"
8
+ Freemle::Client.api_key = "your_api_key"
9
+ ```
10
+
11
+ Check https://www.freemle.com/api-documentatie for more info.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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-client"
8
+ spec.version = Freemle::Client::VERSION
9
+ spec.authors = ["Bob Forma"]
10
+ spec.email = ["bforma@zilverline.com"]
11
+ spec.summary = %q{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_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "webmock"
24
+
25
+ spec.add_dependency "activesupport"
26
+ spec.add_dependency "httpclient"
27
+ end
@@ -0,0 +1,115 @@
1
+ require "freemle/client/version"
2
+ require "active_support/all"
3
+ require "httpclient"
4
+
5
+ module Freemle
6
+ class Client
7
+ class << self
8
+ attr_accessor :base_url, :app_name, :api_key
9
+ end
10
+
11
+ # Finds customers in Freemle and returns them as a list of hashes. Each hash represents a customer.
12
+ #
13
+ # Raises Freemle::BadRequest when validation errors occur.
14
+ # Raises Freemle::UnauthorizedError when app_name and api_key are not properly set.
15
+ # Raises Freemle::ServerError if something goes wrong on the server for any other reason.
16
+ def self.find_customers(company_name)
17
+ client = get_client
18
+ response = client.get "#{base_url}/customers", {company_name: company_name}, {Authorization: "Basic #{get_auth_header}"}
19
+ handle_response(response, {200 => lambda { |response| from_json(response.body)["customers"] }})
20
+ end
21
+
22
+ # Creates an invoice in Freemle with the given values. Returns the unique ID of the created invoice.
23
+ #
24
+ # Raises Freemle::BadRequest when validation errors occur.
25
+ # Raises Freemle::UnauthorizedError when app_name and api_key are not properly set.
26
+ # Raises Freemle::ServerError if something goes wrong on the server for any other reason.
27
+ def self.create_invoice(invoice)
28
+ post "/invoices", invoice: invoice do |body_as_hash|
29
+ body_as_hash["invoice_id"]
30
+ end
31
+ end
32
+
33
+ # Creates a customer in Freemle with the given values. Returns the unique ID of the created invoice.
34
+ #
35
+ # Raises Freemle::BadRequest when validation errors occur.
36
+ # Raises Freemle::UnauthorizedError when app_name and api_key are not properly set.
37
+ # Raises Freemle::ServerError if something goes wrong on the server for any other reason.
38
+ def self.create_customer(customer)
39
+ post "/customers", customer: customer do |body_as_hash|
40
+ body_as_hash["customer_id"]
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def self.post(url, body, &block)
47
+ client = get_client
48
+ response = client.post "#{base_url}#{url}", body.to_json, {Authorization: "Basic #{get_auth_header}", 'Content-Type' => 'application/json'}
49
+ body_as_hash = handle_response(response, {201 => lambda { |response| from_json(response.body) }})
50
+ yield body_as_hash
51
+ end
52
+
53
+ def self.get_client
54
+ HTTPClient.new
55
+ end
56
+
57
+ def self.get_auth_header
58
+ Base64.encode64("#{app_name}:#{api_key}").delete("\r\n")
59
+ end
60
+
61
+ def self.handle_response(response, status_handlers = {})
62
+ case response.status
63
+ when 400 then
64
+ raise BadRequest.new(from_json(response.body)["errors"])
65
+ when 401 then
66
+ raise UnauthorizedError
67
+ when 500 then
68
+ raise ServerError
69
+ else
70
+ if status_handlers[response.status].present?
71
+ status_handlers[response.status].call(response)
72
+ else
73
+ raise UnsupportedStatusCode.new(response.status)
74
+ end
75
+ end
76
+ end
77
+
78
+ def self.from_json(body)
79
+ ActiveSupport::JSON.decode(body)
80
+ end
81
+
82
+ class Error < RuntimeError
83
+ end
84
+
85
+ class UnsupportedStatusCode < Error
86
+ attr_reader :status_code
87
+
88
+ def initialize(status_code)
89
+ @status_code = status_code
90
+ end
91
+
92
+ def to_s
93
+ @status_code.to_s
94
+ end
95
+ end
96
+
97
+ class UnauthorizedError < Error
98
+ end
99
+
100
+ class ServerError < Error
101
+ end
102
+
103
+ class BadRequest < Error
104
+ attr_reader :errors
105
+
106
+ def initialize(errors)
107
+ @errors = errors
108
+ end
109
+
110
+ def to_s
111
+ @errors.to_s
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,5 @@
1
+ module Freemle
2
+ class Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,93 @@
1
+ require "spec_helper"
2
+
3
+ describe Freemle::Client do
4
+
5
+ before do
6
+ Freemle::Client.base_url = "https://www.freemle.com/api"
7
+ Freemle::Client.app_name = "application"
8
+ Freemle::Client.api_key = "key"
9
+ end
10
+
11
+ describe "searching for customers" do
12
+ it "return empty list when no customers are found" do
13
+ stub_request(:get, /.*\/api\/customers\?company_name=foo/).
14
+ with(:headers => {'Authorization' => 'Basic YXBwbGljYXRpb246a2V5'}).
15
+ to_return(status: 200, body: {customers: []}.to_json)
16
+
17
+ actual = Freemle::Client.find_customers "foo"
18
+ actual.should == []
19
+ end
20
+
21
+ it "should return the customers as hashes" do
22
+ stub_request(:get, /.*\/api\/customers\?company_name=foo/).
23
+ with(:headers => {'Authorization' => 'Basic YXBwbGljYXRpb246a2V5'}).
24
+ to_return(status: 200, body: {customers: [{name: "foo"}, {name: "foobar"}]}.to_json)
25
+ actual = Freemle::Client.find_customers "foo"
26
+ actual.should == [{"name" => "foo"}, {"name" => "foobar"}]
27
+
28
+ end
29
+
30
+ it "should raise unauthorized when basic authentication fails" do
31
+ stub_request(:get, /.*\/api\/customers\?company_name=foo/).to_return(status: 401)
32
+ expect { Freemle::Client.find_customers("foo") }.to raise_error Freemle::Client::UnauthorizedError
33
+ end
34
+
35
+ it "should raise server error when 500 is returned" do
36
+ stub_request(:get, /.*\/api\/customers\?company_name=foo/).to_return(status: 500)
37
+ expect { Freemle::Client.find_customers("foo") }.to raise_error Freemle::Client::ServerError
38
+ end
39
+
40
+ end
41
+
42
+ describe "creating customers" do
43
+ before :each do
44
+ @customer = {name: "foo"}
45
+ @customer_id = "abc"
46
+ end
47
+
48
+ it "should return the id of the customer when successfully created" do
49
+ stub_request(:post, /.*\/api\/customers/).
50
+ with(headers: {'Authorization' => 'Basic YXBwbGljYXRpb246a2V5', 'Content-Type' => 'application/json'}, body: {customer: @customer}.to_json).
51
+ to_return(status: 201, body: {customer_id: @customer_id}.to_json)
52
+ actual = Freemle::Client.create_customer(@customer)
53
+ actual.should == @customer_id
54
+ end
55
+
56
+ it "should raise unauthorized when basic authentication fails" do
57
+ stub_request(:post, /.*\/api\/customers/).with(body: {customer: @customer}.to_json).to_return(status: 401)
58
+ expect { Freemle::Client.create_customer(@customer) }.to raise_error Freemle::Client::UnauthorizedError
59
+ end
60
+
61
+ it "should raise server error when 500 is returned" do
62
+ stub_request(:post, /.*\/api\/customers/).with(headers: {'Authorization' => 'Basic YXBwbGljYXRpb246a2V5', 'Content-Type' => 'application/json'}, body: {customer: @customer}.to_json).to_return(status: 500)
63
+ expect { Freemle::Client.create_customer(@customer) }.to raise_error Freemle::Client::ServerError
64
+ end
65
+ end
66
+
67
+ describe "creating invoices" do
68
+ before :each do
69
+ @invoice = {amount: 500}
70
+ @invoice_id = "cba"
71
+ end
72
+
73
+ it "should return the id of the invoice when successfully created" do
74
+ stub_request(:post, /.*\/api\/invoices/).
75
+ with(headers: {'Authorization' => 'Basic YXBwbGljYXRpb246a2V5', 'Content-Type' => 'application/json'}, body: {invoice: @invoice}.to_json).
76
+ to_return(status: 201, body: {invoice_id: @invoice_id}.to_json)
77
+ actual = Freemle::Client.create_invoice(@invoice)
78
+ actual.should == @invoice_id
79
+ end
80
+
81
+ it "should return list of errors when making a bad request" do
82
+ errors = {"invoice_date" => {"code" => "invalid"}}
83
+ stub_request(:post, /.*\/api\/invoices/).
84
+ with(headers: {'Authorization' => 'Basic YXBwbGljYXRpb246a2V5', 'Content-Type' => 'application/json'}, body: {invoice: @invoice}.to_json).
85
+ to_return(status: 400, body: {errors: errors}.to_json)
86
+ expect { Freemle::Client.create_invoice(@invoice) }.to raise_error { |error|
87
+ error.should be_a(Freemle::Client::BadRequest)
88
+ error.errors.should == errors
89
+ }
90
+ end
91
+ end
92
+
93
+ end
@@ -0,0 +1,3 @@
1
+ require "rspec"
2
+ require "webmock/rspec"
3
+ require "freemle/client"
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freemle-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bob Forma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-28 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httpclient
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - bforma@zilverline.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - freemle-client.gemspec
110
+ - lib/freemle/client.rb
111
+ - lib/freemle/client/version.rb
112
+ - spec/freemle/client_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: https://www.freemle.com/api-documentatie
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.2.0
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Freemle.com REST API client
138
+ test_files:
139
+ - spec/freemle/client_spec.rb
140
+ - spec/spec_helper.rb