lce 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +1 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE +21 -0
  8. data/README.md +98 -0
  9. data/Rakefile +5 -0
  10. data/lce.gemspec +30 -0
  11. data/lib/lce.rb +72 -0
  12. data/lib/lce/client.rb +34 -0
  13. data/lib/lce/client/connection.rb +23 -0
  14. data/lib/lce/client/errors.rb +25 -0
  15. data/lib/lce/client/request.rb +80 -0
  16. data/lib/lce/offer.rb +25 -0
  17. data/lib/lce/order.rb +51 -0
  18. data/lib/lce/quote.rb +34 -0
  19. data/lib/lce/version.rb +4 -0
  20. data/lib/paginated_array.rb +25 -0
  21. data/spec/fixtures/access_denied +20 -0
  22. data/spec/fixtures/account_disabled +20 -0
  23. data/spec/fixtures/check +464 -0
  24. data/spec/fixtures/offers/available_delivery_locations/found +785 -0
  25. data/spec/fixtures/offers/find/found +66 -0
  26. data/spec/fixtures/offers/find/not_found +15 -0
  27. data/spec/fixtures/order_params +23 -0
  28. data/spec/fixtures/orders/all/page_1 +111 -0
  29. data/spec/fixtures/orders/find/found +106 -0
  30. data/spec/fixtures/orders/find/not_found +15 -0
  31. data/spec/fixtures/orders/labels/labels.pdf +0 -0
  32. data/spec/fixtures/orders/labels/response +0 -0
  33. data/spec/fixtures/orders/place/created +106 -0
  34. data/spec/fixtures/orders/place/empty_params +15 -0
  35. data/spec/fixtures/orders/place/missing_params +15 -0
  36. data/spec/fixtures/orders/tracking +30 -0
  37. data/spec/fixtures/quote_params +25 -0
  38. data/spec/fixtures/quotes/all/page_1 +11594 -0
  39. data/spec/fixtures/quotes/all/page_2 +1665 -0
  40. data/spec/fixtures/quotes/find/found +492 -0
  41. data/spec/fixtures/quotes/find/not_found +15 -0
  42. data/spec/fixtures/quotes/request/created +492 -0
  43. data/spec/fixtures/quotes/request/empty_quote +15 -0
  44. data/spec/fixtures/quotes/request/missing_params +15 -0
  45. data/spec/lce/client_spec.rb +41 -0
  46. data/spec/lce/offer_spec.rb +85 -0
  47. data/spec/lce/order_spec.rb +136 -0
  48. data/spec/lce/quote_spec.rb +111 -0
  49. data/spec/lce_spec.rb +109 -0
  50. data/spec/paginated_array_spec.rb +55 -0
  51. data/spec/spec_helper.rb +17 -0
  52. data/tasks/rspec.rake +3 -0
  53. metadata +239 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42308cd92f775b295eda5cee11a402c71fa922bd
4
+ data.tar.gz: 43f4d00d85f70453cb424a175b30e112f8179483
5
+ SHA512:
6
+ metadata.gz: c590bcd48a506cbc23f7b59a74b44c72ff746fd5876a0baceebb6e75e402938699503ba47e1c68ea5bf4ece3f4385e67391fbcd58b0ac0a5cfae82dd8bff3d1c
7
+ data.tar.gz: fa8bdae09510c95a474c9b4d9552cbbd1c9d0dc68d1f3bba0dd00e8ced48d73d1d2c18b42a0e7887743995f5d076dc2439fd95cca27716421d7b7b954338c61b
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1 @@
1
+ ruby-lce
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-lce.gemspec
4
+ gemspec
5
+
6
+ gem 'awesome_print'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Low Cost Express SAS
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,98 @@
1
+ # Lce
2
+
3
+ A Ruby library that provides an interface to the LCE web services.
4
+
5
+ ## Installation
6
+
7
+ This library is packaged as a gem. So you can add this line to your application's Gemfile:
8
+
9
+ gem 'lce'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lce
18
+
19
+ ## Configuration
20
+
21
+ You can configure this gem by using a configuration block :
22
+
23
+ Lce.configure do |config|
24
+ config.environment = :staging
25
+ config.login = 'login'
26
+ config.password = 'password'
27
+ end
28
+
29
+ If you are using the gem in a Rails application, you can store this configuation with oher initializers in :
30
+
31
+ RAILS_ROOT/config/initializers/lce.rb
32
+
33
+ Here are all available configuration options with their default values :
34
+
35
+ Lce.configure do |config|
36
+ # Switch to :production when you are ready to go live
37
+ config.environment = :staging
38
+
39
+ # No default for authentication attributes. You should have received that by email if not ask us at support@lce.io
40
+ config.login =
41
+ config.password =
42
+
43
+ # You can set it to Rails.logger if you are using a rails app.
44
+ config.logger = Logger.new(STDOUT)
45
+
46
+ # You can set your own application's name and version.
47
+ config.application = 'ruby-lce'
48
+ config.version = '0.0.1'
49
+ end
50
+
51
+ ## Usage
52
+
53
+ Once configured, you can start making request to the lce webservices. The full documentation for all requests and attributes is available at https://www.lce.io/docs
54
+
55
+ ### Requesting a quote
56
+
57
+ quote_params = {
58
+ shipper: {city: "Toulouse", postal_code: "31000", country:"FR"},
59
+ recipient: {city: "Nice", postal_code: "06000", country: "FR", is_a_company: false},
60
+ parcels: [
61
+ {length: 25,height: 25, width: 25, weight: 3}
62
+ ]
63
+ }
64
+ quote = Lce::Quote.request(quote_params)
65
+
66
+ ### Placing an order
67
+
68
+ order_params = {
69
+ shipper: {name: "Firstname Lastname", street: "999, street name", phone: "+33699999999", email: "support@lce.io"},
70
+ recipient: {name: "Firstname Lastname", street: "111, other street", phone: "+33600000", email: "support@lce.io"},
71
+ parcels: [{description: 'Gift'}]
72
+ }
73
+
74
+ # First, we select an offer we want to order (for this example, we are using the first one)
75
+ offer = quote.offers.first
76
+ order = offer.place_order(order_params)
77
+
78
+ ### Retrieving the labels
79
+
80
+ # We can retrieve the labels
81
+ labels = order.labels
82
+ # and write them to our drive
83
+ File.open("order-#{order.id}.pdf", "wb") do |f|
84
+ f.write(labels)
85
+ end
86
+
87
+ ### Getting the tracking informations
88
+
89
+ # And then, we can get the tracking events by parcels
90
+ order.tracking
91
+
92
+ ## Contributing
93
+
94
+ 1. Fork it ( http://github.com/lce/ruby-lce/fork )
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
97
+ 4. Push to the branch (`git push origin my-new-feature`)
98
+ 5. Create new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
4
+
5
+ task :default => [:spec]
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lce/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lce"
8
+ spec.version = Lce::VERSION
9
+ spec.authors = ["Florent Piteau", "Thomas Belliard"]
10
+ spec.email = ["info@lce.io"]
11
+ spec.summary = %q{A Ruby library that provides an interface to the LCE web services.}
12
+ spec.homepage = "http://lce.io/"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
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', '~> 3.0'
23
+ spec.add_development_dependency 'webmock', '~> 1.18.0'
24
+
25
+ spec.add_dependency "faraday", "~> 0.9"
26
+ spec.add_dependency "faraday_middleware", "~> 0.9.1"
27
+ spec.add_dependency "faraday_middleware-parse_oj", "~> 0.3.0"
28
+ spec.add_dependency "hashie", "~> 3.2.0"
29
+
30
+ end
@@ -0,0 +1,72 @@
1
+ require "lce/version"
2
+ require "lce/client"
3
+ require "lce/offer"
4
+ require "lce/quote"
5
+ require "lce/order"
6
+ require "logger"
7
+ require "awesome_print"
8
+
9
+ module Lce
10
+ class << self
11
+ attr_accessor :configuration, :client
12
+ end
13
+
14
+ def self.client
15
+ @client ||= Client.new
16
+ end
17
+
18
+ def self.configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def self.configure
23
+ yield(configuration)
24
+ end
25
+
26
+ def self.reset
27
+ @configuration = Configuration.new
28
+ end
29
+
30
+ def self.check
31
+ begin
32
+ client.get
33
+ rescue Lce::Client::Errors::LceError => e
34
+ if configuration.raise_lce_errors
35
+ raise e
36
+ else
37
+ configuration.logger.warn(e.to_s)
38
+ end
39
+ return false
40
+ end
41
+ end
42
+
43
+
44
+ class Configuration
45
+ attr_accessor :login, :password, :environment, :api_version, :http_adapter, :raise_lce_errors, :logger, :application, :version
46
+
47
+ def initialize
48
+ @environment = :staging
49
+ @api_version = 1
50
+ @http_adapter = Faraday.default_adapter
51
+ @raise_lce_errors = true
52
+ @logger = Logger.new(STDOUT)
53
+ @logger.level = Logger::DEBUG
54
+ @application = 'ruby-lce'
55
+ @version = Lce::VERSION
56
+ end
57
+
58
+ def environment=(value)
59
+ raise 'Environment must be :staging or :production' unless [:staging, :production].include?(value)
60
+ @environment = value
61
+ end
62
+
63
+ def application=(app)
64
+ @application = "#{app} (ruby-lce)"
65
+ end
66
+
67
+ def version=(version)
68
+ @version = "#{version} (#{Lce::VERSION})"
69
+ end
70
+ end
71
+ end
72
+
@@ -0,0 +1,34 @@
1
+ require 'lce/client/connection'
2
+ require 'lce/client/errors'
3
+ require 'lce/client/request'
4
+
5
+ module Lce
6
+ class Client
7
+
8
+ HOSTS = {
9
+ development: "http://localhost:9000",
10
+ staging: "https://test.lce.io",
11
+ production: "https://api.lce.io"
12
+ }
13
+
14
+ include Connection
15
+ include Request
16
+ include Errors
17
+
18
+ attr_accessor :http_adapter
19
+
20
+ def initialize
21
+ @http_adapter = Lce.configuration.http_adapter
22
+ end
23
+
24
+ def host
25
+ HOSTS[Lce.configuration.environment]
26
+ end
27
+
28
+ def api_version
29
+ raise VersionError.new("Wrong API version",' wrong_api_version', "Version must be 1.") if Lce.configuration.api_version != 1
30
+ 'v'+Lce.configuration.api_version.to_s
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'faraday_middleware/parse_oj'
4
+
5
+ module Lce
6
+ class Client
7
+ module Connection
8
+ private
9
+ def connection
10
+ Faraday.new(:url => host) do |faraday|
11
+ faraday.basic_auth(Lce.configuration.login, Lce.configuration.password)
12
+ faraday.headers['Lce-Origin-Application'] = Lce.configuration.application
13
+ faraday.headers['Lce-Origin-Version'] = Lce.configuration.version
14
+ faraday.request :json
15
+ faraday.response :mashify
16
+ faraday.response :oj, :content_type => /\bjson$/
17
+ faraday.response :logger, Lce.configuration.logger
18
+ faraday.adapter http_adapter
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ module Lce
2
+ class Client
3
+ module Errors
4
+ class LceError < StandardError
5
+ attr_reader :type, :details
6
+ def initialize(msg, type, details)
7
+ super(msg)
8
+ @type = type
9
+ @details = details
10
+ end
11
+ def to_s
12
+ s = super
13
+ d = (@details.is_a? Array)? @details.join(' ') : @details
14
+ s+= "\n#{d}" if d && !d.empty?
15
+ s
16
+ end
17
+ end
18
+ class VersionError < LceError; end
19
+ class ConnectionError < LceError; end
20
+ class AccessDenied < LceError; end
21
+ class AccountDisabled < LceError; end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,80 @@
1
+ module Lce
2
+ class Client
3
+ module Request
4
+ def get(resource = nil, id = nil, action = nil, format = nil, params = nil)
5
+ p = path(resource, id, action, format)
6
+ request(:get, p, params, format)
7
+ end
8
+
9
+ def post(resource = nil, params = nil)
10
+ p = path(resource)
11
+ request(:post, p, params)
12
+ end
13
+
14
+
15
+ private
16
+
17
+ def path(resource = nil, id = nil, action = nil, format = nil)
18
+ path = []
19
+ path << api_version << resource.to_s if resource
20
+ path << id.to_s if id
21
+ path << action.to_s if action
22
+ path = '/'+path.join('/')
23
+ path += ".#{format}" if format
24
+ return path
25
+ end
26
+
27
+ def request(action, path, params, format = nil)
28
+ response = connection.send(action, path, params)
29
+ if success?(response)
30
+ return process_data(response.headers['content-type'], response.body)
31
+ else
32
+ error!(response)
33
+ return nil
34
+ end
35
+ end
36
+
37
+ def process_data(type, body)
38
+ case type
39
+ when 'application/json'
40
+ process_json(body)
41
+ when 'application/pdf'
42
+ process_pdf(body)
43
+ end
44
+ end
45
+
46
+ def process_pdf(body)
47
+ body
48
+ end
49
+
50
+ def process_json(body)
51
+ if body.data.is_a? Array
52
+ a = PaginatedArray.new(body[:count], body[:page], body[:per_page])
53
+ body.data.each do |d|
54
+ a << d
55
+ end
56
+ return a
57
+ else
58
+ return body.data
59
+ end
60
+ end
61
+
62
+ def success?(response)
63
+ response.status.between?(200, 299) && (response.headers['content-type'] != 'application/json' || response.body.status == "success")
64
+ end
65
+
66
+ def error!(response)
67
+ if response.body.error
68
+ case response.body.error.type
69
+ when 'access_denied'
70
+ raise Lce::Client::AccessDenied.new(response.body.error.message, response.body.error.type, response.body.error.details)
71
+ when 'account_disabled'
72
+ raise Lce::Client::AccountDisabled.new(response.body.error.message, response.body.error.type, response.body.error.details)
73
+ when 'internal', 'resource_not_found'
74
+ raise Lce::Client::LceError.new(response.body.error.message, response.body.error.type, response.body.error.details)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,25 @@
1
+ require 'hashie'
2
+ module Lce
3
+ class Offer < Hashie::Mash
4
+ class << self
5
+ def find(id)
6
+ response = Lce.client.get('offers', id)
7
+ new(response)
8
+ end
9
+
10
+ end
11
+
12
+ def place_order(params)
13
+ params.merge!(offer_id: id)
14
+ Order.place(params)
15
+ end
16
+
17
+ def available_delivery_locations(params)
18
+ return [] unless product.preset_delivery_location == true
19
+ Lce.client.get('offers', id, 'available_delivery_locations', nil , {location: params }).map! do |l|
20
+ Hashie::Mash.new(l)
21
+ end
22
+ end
23
+
24
+ end
25
+ end