leadcli 0.0.3
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 +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +45 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +1 -0
- data/leadcli.gemspec +25 -0
- data/lib/leadcli/api_call.rb +8 -0
- data/lib/leadcli/api_caller.rb +82 -0
- data/lib/leadcli/configuration.rb +26 -0
- data/lib/leadcli/lead.rb +36 -0
- data/lib/leadcli/rails.rb +16 -0
- data/lib/leadcli/routes.rb +52 -0
- data/lib/leadcli/version.rb +4 -0
- data/lib/leadcli.rb +55 -0
- data/spec/leadcli/api_call_spec.rb +17 -0
- data/spec/leadcli/api_caller_spec.rb +90 -0
- data/spec/leadcli/configuration_spec.rb +23 -0
- data/spec/leadcli/lead_spec.rb +37 -0
- data/spec/leadcli/routes_spec.rb +35 -0
- data/spec/spec_helper.rb +15 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 44b485ed30fd5d722aadf779a7ddce956a7592ad
|
4
|
+
data.tar.gz: 14fa115187f897aad9c3f7f3f45fc49b81df8558
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 94c93ae8b73f6144858541d663f277984e70a88921f44b73eb4635d3b92d0aaeadb374aa7de78a3b0ec10e761b10284b3439db6edc27327959dce4d9ba26f187
|
7
|
+
data.tar.gz: 8c0b866aa045f949105ac16a242f2d91b6c5215e49b4cc9cae2e47b3b1ea15c10e27da0705b4b932383021f3bea3b206a451dc2b090c273bfc3fb7d525465e7b
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
leadcli (0.0.2)
|
5
|
+
rest-client
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
domain_name (0.5.24)
|
12
|
+
unf (>= 0.0.5, < 1.0.0)
|
13
|
+
fakeweb (1.3.0)
|
14
|
+
http-cookie (1.0.2)
|
15
|
+
domain_name (~> 0.5)
|
16
|
+
mime-types (2.6.1)
|
17
|
+
netrc (0.10.3)
|
18
|
+
rest-client (1.8.0)
|
19
|
+
http-cookie (>= 1.0.2, < 2.0)
|
20
|
+
mime-types (>= 1.16, < 3.0)
|
21
|
+
netrc (~> 0.7)
|
22
|
+
rspec (3.3.0)
|
23
|
+
rspec-core (~> 3.3.0)
|
24
|
+
rspec-expectations (~> 3.3.0)
|
25
|
+
rspec-mocks (~> 3.3.0)
|
26
|
+
rspec-core (3.3.2)
|
27
|
+
rspec-support (~> 3.3.0)
|
28
|
+
rspec-expectations (3.3.1)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.3.0)
|
31
|
+
rspec-mocks (3.3.2)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.3.0)
|
34
|
+
rspec-support (3.3.0)
|
35
|
+
unf (0.1.4)
|
36
|
+
unf_ext
|
37
|
+
unf_ext (0.0.7.1)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
fakeweb
|
44
|
+
leadcli!
|
45
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Franck D'agostini
|
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,46 @@
|
|
1
|
+
# Leadcli
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'leadcli'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install leadcli
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class ContactController < ApplicationController
|
21
|
+
|
22
|
+
def create
|
23
|
+
@contact = Contact.new(params_contact)
|
24
|
+
if @contact.save
|
25
|
+
send_lead(params_contact)
|
26
|
+
redirect_to thanks_contact_path
|
27
|
+
else
|
28
|
+
render :new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def send_lead(params)
|
35
|
+
info = {
|
36
|
+
subject: "Contact",
|
37
|
+
content: params[:content],
|
38
|
+
name: params[:name],
|
39
|
+
email: params[:email]
|
40
|
+
}
|
41
|
+
Leadcli.send(info)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/leadcli.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'leadcli/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "leadcli"
|
8
|
+
gem.version = Leadcli::VERSION
|
9
|
+
gem.authors = ["Franck D'agostini"]
|
10
|
+
gem.email = ["franck.dagostini@gmail.com"]
|
11
|
+
gem.description = %q{Client gem for Leadheque server api}
|
12
|
+
gem.summary = %q{Client gem for Leadheque server api}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency('rest-client')
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
gem.add_development_dependency('fakeweb')
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module Leadcli
|
4
|
+
class ApiCaller
|
5
|
+
include ::Leadcli::Routes
|
6
|
+
|
7
|
+
attr_accessor :max_connection_attempts
|
8
|
+
attr_reader :endpoint, :options, :connection_attempts
|
9
|
+
|
10
|
+
DEFAULT_RETRY_LIMIT = 1
|
11
|
+
|
12
|
+
def initialize(endpoint, options={})
|
13
|
+
@endpoint, @options = endpoint, options
|
14
|
+
@connection_attempts = 0
|
15
|
+
@max_connection_attempts = options[:max_connection_attempts] || DEFAULT_RETRY_LIMIT
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(obj)
|
19
|
+
method, url = send("#{endpoint}_endpoint", options[:url_options] || {})
|
20
|
+
Leadcli.debug("API CALL: #{method} #{url}")
|
21
|
+
|
22
|
+
while connection_attempts < max_connection_attempts
|
23
|
+
sleep_if_retrying
|
24
|
+
|
25
|
+
response = make_call(method, url)
|
26
|
+
valid_response_codes = (200..207).to_a
|
27
|
+
if valid_response_codes.include?(response.code.to_i)
|
28
|
+
if options[:success]
|
29
|
+
Leadcli.debug("CALLING SUCCESS HANDLER: #{options[:success]}")
|
30
|
+
obj.send(options[:success], response)
|
31
|
+
end
|
32
|
+
success = true
|
33
|
+
break
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
if !success && options[:failure]
|
38
|
+
obj.send(options[:failure], response)
|
39
|
+
end
|
40
|
+
rescue Errno::ECONNREFUSED => error
|
41
|
+
Leadcli.debug("Remote server down : #{error}")
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def make_call(method, url)
|
47
|
+
begin
|
48
|
+
@connection_attempts += 1
|
49
|
+
Leadcli.debug("ATTEMPT #{@connection_attempts}")
|
50
|
+
headers = {
|
51
|
+
"AUTHORIZATION" => "Token token=\"#{Leadcli.configuration.token}\"",
|
52
|
+
:content_type => :json,
|
53
|
+
:accept => :json
|
54
|
+
}
|
55
|
+
parameters = {
|
56
|
+
url: url,
|
57
|
+
method: method,
|
58
|
+
headers: headers
|
59
|
+
}
|
60
|
+
parameters[:payload] = options[:payload] if method == :post or method == :put
|
61
|
+
RestClient::Request.execute(parameters)
|
62
|
+
rescue RestClient::ResourceNotFound,
|
63
|
+
RestClient::NotModified,
|
64
|
+
RestClient::InternalServerError,
|
65
|
+
RestClient::BadGateway,
|
66
|
+
RestClient::ServiceUnavailable,
|
67
|
+
RestClient::UnprocessableEntity,
|
68
|
+
RestClient::GatewayTimeout => error
|
69
|
+
return error.response
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def sleep_if_retrying
|
74
|
+
if @connection_attempts > 0
|
75
|
+
time = @connection_attempts * 5
|
76
|
+
Leadcli.debug("Sleeping for #{time} before retrying")
|
77
|
+
sleep time
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Leadcli
|
2
|
+
class Configuration
|
3
|
+
|
4
|
+
attr_accessor :host
|
5
|
+
attr_accessor :port
|
6
|
+
attr_accessor :secure
|
7
|
+
attr_accessor :token
|
8
|
+
attr_accessor :logger
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
defaults.each do |setting, value|
|
12
|
+
send("#{setting}=", value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def defaults
|
17
|
+
defaults = {
|
18
|
+
host: 'new.eureo-tools.com'
|
19
|
+
}
|
20
|
+
#defaults[:logger] = Logger.new(STDOUT)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
data/lib/leadcli/lead.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "leadcli/api_call"
|
2
|
+
|
3
|
+
module Leadcli
|
4
|
+
class Lead
|
5
|
+
include Leadcli::ApiCall
|
6
|
+
|
7
|
+
attr_accessor :responses
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@responses = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def create(params)
|
14
|
+
api_call :create, url_options: {}, payload: { lead: params }, failure: :handle_failure, success: :handle_success
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete(lead_id)
|
18
|
+
api_call :delete, url_options: { id: lead_id }, failure: :handle_failure, success: :handle_success
|
19
|
+
end
|
20
|
+
|
21
|
+
def handle_failure(response)
|
22
|
+
Leadcli.debug("LEAD API CALL FAILED : #{response}")
|
23
|
+
@responses << response
|
24
|
+
end
|
25
|
+
|
26
|
+
def handle_success(response)
|
27
|
+
@responses << response
|
28
|
+
end
|
29
|
+
|
30
|
+
def last_response
|
31
|
+
@responses.last
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Leadcli
|
2
|
+
module Rails
|
3
|
+
def self.initialize
|
4
|
+
if defined?(::Rails.logger)
|
5
|
+
rails_logger = ::Rails.logger
|
6
|
+
elsif defined?(RAILS_DEFAULT_LOGGER)
|
7
|
+
rails_logger = RAILS_DEFAULT_LOGGER
|
8
|
+
end
|
9
|
+
|
10
|
+
Leadcli.configure do |config|
|
11
|
+
config.logger = rails_logger
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Leadcli
|
2
|
+
module Routes
|
3
|
+
|
4
|
+
def create_endpoint(options)
|
5
|
+
[:post, create_url(options)]
|
6
|
+
end
|
7
|
+
|
8
|
+
def create_url(options={})
|
9
|
+
url = http_scheme.build(base_options.merge(path: create_path))
|
10
|
+
url.query = options[:query].map { |k,v| "#{k}=#{v}" }.join('&') if options[:query]
|
11
|
+
url.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def delete_endpoint(options)
|
15
|
+
[:delete, delete_url(options)]
|
16
|
+
end
|
17
|
+
|
18
|
+
def delete_url(options)
|
19
|
+
url = http_scheme.build(base_options.merge(path: delete_path(options[:id])))
|
20
|
+
url.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def http_scheme
|
26
|
+
if Leadcli.configuration.secure
|
27
|
+
URI::HTTPS
|
28
|
+
else
|
29
|
+
URI::HTTP
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def base_options
|
34
|
+
options = {}
|
35
|
+
options[:host] = Leadcli.configuration.host
|
36
|
+
options[:port] = Leadcli.configuration.port.to_i if Leadcli.configuration.port
|
37
|
+
options
|
38
|
+
end
|
39
|
+
|
40
|
+
def create_path
|
41
|
+
path = "/api/v1/leads"
|
42
|
+
path
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete_path(lead_id)
|
46
|
+
path = "/api/v1/leads/#{lead_id}"
|
47
|
+
path
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/lib/leadcli.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require "leadcli/version"
|
2
|
+
require "leadcli/configuration"
|
3
|
+
require "leadcli/routes"
|
4
|
+
require "leadcli/api_caller"
|
5
|
+
require "leadcli/api_call"
|
6
|
+
require "leadcli/lead"
|
7
|
+
|
8
|
+
module Leadcli
|
9
|
+
LOG_PREFIX = "** [Leadcli] "
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :configuration
|
13
|
+
|
14
|
+
def log(message)
|
15
|
+
if logger
|
16
|
+
logger.info(LOG_PREFIX + message)
|
17
|
+
else
|
18
|
+
puts(LOG_PREFIX + message)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def debug(message)
|
23
|
+
if logger
|
24
|
+
logger.debug(LOG_PREFIX + message)
|
25
|
+
else
|
26
|
+
puts(LOG_PREFIX + message)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def logger
|
31
|
+
self.configuration && self.configuration.logger
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# @example
|
36
|
+
#
|
37
|
+
# Leadcli.configure do |config|
|
38
|
+
# config.token = '1234567890abcdef'
|
39
|
+
# config.host = 'localhost'
|
40
|
+
# config.port = '3000'
|
41
|
+
# config.logger = Rails.logger
|
42
|
+
# end
|
43
|
+
|
44
|
+
def configure
|
45
|
+
self.configuration ||= Configuration.new
|
46
|
+
yield(configuration) if block_given?
|
47
|
+
end
|
48
|
+
|
49
|
+
def sync
|
50
|
+
return true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class ApiCallerTest
|
4
|
+
include Leadcli::ApiCall
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Leadcli::ApiCall do
|
8
|
+
|
9
|
+
it "creates an ApiCaller object and tells it to make the call" do
|
10
|
+
api_call_test = ApiCallerTest.new
|
11
|
+
api_call = double('api_call')
|
12
|
+
expect(api_call).to receive(:call).with(api_call_test)
|
13
|
+
expect(Leadcli::ApiCaller).to receive(:new).with(:endpoint, { foo: :bar }).and_return(api_call)
|
14
|
+
api_call_test.api_call(:endpoint, { foo: :bar })
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Leadcli::ApiCaller do
|
5
|
+
|
6
|
+
describe "#new" do
|
7
|
+
it "stores the endpoint and options" do
|
8
|
+
api_caller = Leadcli::ApiCaller.new(:myendpoint, foo: :bar)
|
9
|
+
expect(api_caller.endpoint).to eq(:myendpoint)
|
10
|
+
expect(api_caller.options).to eq({ foo: :bar })
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#call" do
|
15
|
+
let(:api_caller) { with_configuration { Leadcli::ApiCaller.new(:test) } }
|
16
|
+
let(:url) { 'http://example.com/test' }
|
17
|
+
it "gets the method and url from the endpoint" do
|
18
|
+
expect(api_caller).to receive(:test_endpoint).with({}).and_return([:get, url])
|
19
|
+
api_caller.call(self)
|
20
|
+
end
|
21
|
+
it "passes through any url options" do
|
22
|
+
expect(api_caller).to receive(:test_endpoint).with({ query: { updated_at: '2013-07-19', foo: :bar }}).and_return([:get, url])
|
23
|
+
api_caller.options[:url_options] = { query: { updated_at: '2013-07-19', foo: :bar } }
|
24
|
+
expect(RestClient::Request).to receive(:execute).and_return(double('response', :code => 200))
|
25
|
+
api_caller.call(self)
|
26
|
+
end
|
27
|
+
describe "a GET request" do
|
28
|
+
it "make the call to the api" do
|
29
|
+
expect(api_caller).to receive(:test_endpoint).and_return([:get, url])
|
30
|
+
expect(RestClient::Request).to receive(:execute).with(hash_including(url: url, method: :get)).and_return(double('response', :code => 200))
|
31
|
+
api_caller.call(self)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
describe "a POST request" do
|
35
|
+
it "make the call using :payload option as payload" do
|
36
|
+
expect(api_caller).to receive(:test_endpoint).and_return([:post, url])
|
37
|
+
api_caller.options[:payload] = 'test data'
|
38
|
+
expect(RestClient::Request).to receive(:execute).with(hash_including(:url => url, :payload => "test data", :method => :post)).and_return(double('response', :code => 200))
|
39
|
+
api_caller.call(self)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "call failed" do
|
45
|
+
let(:api_caller) { with_configuration { Leadcli::ApiCaller.new(:test) } }
|
46
|
+
let(:url) { 'url' }
|
47
|
+
let(:object) { double('calling object') }
|
48
|
+
before do
|
49
|
+
FakeWeb.register_uri(:get, url, :body => '', :status => [500, 'Internal Server Error'])
|
50
|
+
expect(api_caller).to receive(:test_endpoint).and_return([:get, url])
|
51
|
+
end
|
52
|
+
|
53
|
+
it "retries call, up to value of :max_connection_attempts option" do
|
54
|
+
api_caller.max_connection_attempts = 2
|
55
|
+
api_caller.call(object)
|
56
|
+
expect(api_caller.connection_attempts).to eq 2
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
{
|
61
|
+
304 => 'Not Modified',
|
62
|
+
404 => 'Resource Not Found',
|
63
|
+
500 => 'Internal Server Error',
|
64
|
+
# Work out when this could happen
|
65
|
+
# 501 => 'Not Implemented',
|
66
|
+
502 => 'Bad Gateway',
|
67
|
+
503 => 'Service Unavailable',
|
68
|
+
504 => 'Gateway Timeout',
|
69
|
+
# Work out when this could happen
|
70
|
+
# 505 => 'HTTP Version Not Supported',
|
71
|
+
# Work out when this could happen
|
72
|
+
# 506 => 'Variant Also Negotiates',
|
73
|
+
# Work out when this could happen
|
74
|
+
# 507 => 'Insufficient Storage', #WebDAV
|
75
|
+
# Work out when this could happen
|
76
|
+
# 509 => 'Bandwidth Limit Exceeded', #Apache
|
77
|
+
# Work out when this could happen
|
78
|
+
# 510 => 'Not Extended'
|
79
|
+
}.each do |code, reason|
|
80
|
+
it "fails when response is #{code} #{reason}" do
|
81
|
+
FakeWeb.register_uri(:get, url, :body => '', :status => [code.to_s, reason])
|
82
|
+
api_caller.options[:failure] = :fail
|
83
|
+
expect(object).to receive(:fail)
|
84
|
+
api_caller.call(object)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Leadcli::Configuration do
|
4
|
+
let(:configuration) { Leadcli::Configuration.new }
|
5
|
+
|
6
|
+
it "sets the host by default" do
|
7
|
+
expect(configuration.host).to eq "new.eureo-tools.com"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "allows the host to be overwritten" do
|
11
|
+
expect { configuration.host = 'test.host' }.to change(configuration, :host).to('test.host')
|
12
|
+
end
|
13
|
+
|
14
|
+
it "sets the port" do
|
15
|
+
expect { configuration.port = 1234 }.to change(configuration, :port).to(1234)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "sets the token" do
|
19
|
+
expect { configuration.token = '123456789' }.to change(configuration, :token).to("123456789")
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
describe Leadcli::Lead do
|
5
|
+
let(:params) { { subject: "Contact" }}
|
6
|
+
let(:lead) { Leadcli::Lead.new }
|
7
|
+
|
8
|
+
describe "#create" do
|
9
|
+
it "creates it" do
|
10
|
+
with_configuration(host: HOST, port: 3000, token: TOKEN) do
|
11
|
+
lead.create(params)
|
12
|
+
expect(lead.last_response).to match(/Contact/)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
after(:each) do
|
16
|
+
lead_id = JSON.parse(lead.last_response)['id']
|
17
|
+
lead.delete(lead_id)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#delete" do
|
22
|
+
context "when remote lead exists" do
|
23
|
+
it "delete it" do
|
24
|
+
with_configuration(host: HOST, port: 3000, token: TOKEN) do
|
25
|
+
lead.create(params)
|
26
|
+
created_lead_id = JSON.parse(lead.last_response)['id']
|
27
|
+
lead.delete(created_lead_id)
|
28
|
+
|
29
|
+
deleted_lead_id = JSON.parse(lead.last_response)['id']
|
30
|
+
expect(deleted_lead_id).to eq created_lead_id
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestRoute
|
4
|
+
include Leadcli::Routes
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Leadcli::Routes do
|
8
|
+
let!(:test_route) { TestRoute.new }
|
9
|
+
let(:config) { { host: 'test.host', port: 1234 } }
|
10
|
+
|
11
|
+
describe "create_endpoint" do
|
12
|
+
it "returns :post and the create url" do
|
13
|
+
options = { foo: :bar }
|
14
|
+
expect(test_route).to receive(:create_url).with(options).and_return('my_create_url')
|
15
|
+
expect(test_route.create_endpoint(options)).to eq([:post, 'my_create_url'])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "create_url" do
|
20
|
+
it "constructs the url from the configuration" do
|
21
|
+
with_configuration(config) do
|
22
|
+
expect(test_route.create_url).to eq "http://test.host:1234/api/v1/leads"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
it "adds query parameters on to the url" do
|
26
|
+
with_configuration(config) do
|
27
|
+
url = test_route.create_url(query: { updated_at: '2013-07-19', foo: :bar })
|
28
|
+
expect(url).to match(/\?.*updated_at=2013-07-19/)
|
29
|
+
expect(url).to match(/\?.*foo=bar/)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'leadcli'
|
2
|
+
require 'fakeweb'
|
3
|
+
|
4
|
+
HOST = 'localhost'
|
5
|
+
TOKEN = "dea23643d95b70b6ac27a8003f9575659dc975737c266c56bcb20deed98aab3e3e8cd659752f180cf41a933473da89f92d8a5053c140ec07fe3b0deabee1c15b"
|
6
|
+
|
7
|
+
def with_configuration(options = {})
|
8
|
+
Leadcli.configuration = nil
|
9
|
+
Leadcli.configure do |configuration|
|
10
|
+
options.each do |option, value|
|
11
|
+
configuration.send("#{option}=", value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
yield
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: leadcli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Franck D'agostini
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
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: fakeweb
|
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
|
+
description: Client gem for Leadheque server api
|
56
|
+
email:
|
57
|
+
- franck.dagostini@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- Gemfile
|
63
|
+
- Gemfile.lock
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- leadcli.gemspec
|
68
|
+
- lib/leadcli.rb
|
69
|
+
- lib/leadcli/api_call.rb
|
70
|
+
- lib/leadcli/api_caller.rb
|
71
|
+
- lib/leadcli/configuration.rb
|
72
|
+
- lib/leadcli/lead.rb
|
73
|
+
- lib/leadcli/rails.rb
|
74
|
+
- lib/leadcli/routes.rb
|
75
|
+
- lib/leadcli/version.rb
|
76
|
+
- spec/leadcli/api_call_spec.rb
|
77
|
+
- spec/leadcli/api_caller_spec.rb
|
78
|
+
- spec/leadcli/configuration_spec.rb
|
79
|
+
- spec/leadcli/lead_spec.rb
|
80
|
+
- spec/leadcli/routes_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
homepage: ''
|
83
|
+
licenses: []
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.4.5
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Client gem for Leadheque server api
|
105
|
+
test_files:
|
106
|
+
- spec/leadcli/api_call_spec.rb
|
107
|
+
- spec/leadcli/api_caller_spec.rb
|
108
|
+
- spec/leadcli/configuration_spec.rb
|
109
|
+
- spec/leadcli/lead_spec.rb
|
110
|
+
- spec/leadcli/routes_spec.rb
|
111
|
+
- spec/spec_helper.rb
|