lockme 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ef8f125b89e9768fc655846a789cebeb6b528c1
4
- data.tar.gz: 6c9886a66d6cf22dcc53de92767893d5a09f349b
3
+ metadata.gz: 524261899efd42507740502d05f185f9acf04c0a
4
+ data.tar.gz: f92acf0789df28f6970379739e44dfff01b1a654
5
5
  SHA512:
6
- metadata.gz: 7742c5b0218e8e2c0fbaf93e4f7f2ff7bee1f397a9cff591b8f4fe1eac15ca396148033bfea200cdeddd175aa5d87ab388537377c28e2501c7f1338b4b353844
7
- data.tar.gz: 9bbd62b782b4c7ea7af643079e5d3e7e0a9a147144880f6a4de072b76d41383fdbc7dbd4f3a36849bd15ed1b04124c8cde6b5f6ed92cd9a7059abf8b9f9f9649
6
+ metadata.gz: db3f67c37fc6332d6b1131983f4b0824bbc1b12ca40f032b6a6d28f355d806d8496fb58857ecbc2ec01db673a7f1e0e1d404092c404614c7bf76443c8d142d96
7
+ data.tar.gz: 581add854ee6105efa0810dd5ca7771d1faca895efbc905fb46350d81464ac05efeba22adb4badf720d82d7331c47fd8a1e19a4d082885348ec40daa87495367
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ AllCops:
2
+ Exclude:
3
+ - examples/*
4
+ - '*'
5
+ - bin/*
6
+
7
+ Style/StringLiterals:
8
+ Enabled: false
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: false
11
+ Style/PreferredHashMethods:
12
+ EnforcedStyle: verbose
13
+ Style/NumericLiterals:
14
+ Exclude:
15
+ - test/*_test.rb
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - test/*_test.rb
19
+ Style/FrozenStringLiteralComment:
20
+ Exclude:
21
+ - test/*_test.rb
22
+ Metrics/LineLength:
23
+ Exclude:
24
+ - test/*_test.rb
25
+
@@ -0,0 +1,5 @@
1
+ # Lockme examples
2
+
3
+ ## Basic usage
4
+
5
+ CRUD operations on LockMe API.
data/examples/basic.rb ADDED
@@ -0,0 +1,27 @@
1
+ # Find a reservation using an ID from LockMe. Returns an instance of Lockme::Reservation
2
+ Lockme::Reservation.find(lockme_id)
3
+
4
+ # Provide a hash of attributes expected by LockMe API
5
+ def to_lockme_hash
6
+ {
7
+ reservationid: lockme_id,
8
+ roomid: room_id,
9
+ date: start_at.strftime("%Y-%m-%d"),
10
+ hour: start_at.strftime("%H:%M:%S"),
11
+ people: 4,
12
+ pricer: '1',
13
+ price: '%.2f' % BigDecimal.new(price),
14
+ name: first_name,
15
+ surname: last_name,
16
+ email: email,
17
+ phone: phone,
18
+ comment: nil,
19
+ extid: id.to_s
20
+ }.compact
21
+ end
22
+
23
+ # Save or update a reservation. Returns an instance of Lockme::Reservation
24
+ Lockme::Reservation.new(to_lockme_hash).save
25
+
26
+ # Destroy a reservation. Returns `true` on success
27
+ Lockme::Reservation.new(reservationid: lockme_id).destroy
data/lib/lockme/base.rb CHANGED
@@ -1,5 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Lockme
4
+ # Base class for all LockMe API objects
2
5
  class Base
3
6
  include JsonUtils
7
+
8
+ class << self; attr_reader :base_path end
9
+
10
+ # Check if object is persisted with Lockme
11
+ def persisted?
12
+ !id.nil?
13
+ end
14
+
15
+ def singular_path
16
+ self.class.singular_path(id)
17
+ end
18
+
19
+ def collection_path
20
+ self.class.collection_path
21
+ end
22
+
23
+ def self.singular_path(id)
24
+ [base_path, id].join('/')
25
+ end
26
+
27
+ def self.collection_path
28
+ base_path
29
+ end
4
30
  end
5
31
  end
@@ -1,4 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ #:nodoc:
1
4
  module Lockme
5
+ # Mixin providing gem configuration options methods
2
6
  module Configuration
3
7
  def api_key
4
8
  defined?(@api_key) ? @api_key : nil
data/lib/lockme/error.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Lockme
4
+ # Error class
2
5
  class Error < StandardError
3
6
  end
4
7
  end
@@ -1,7 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ #:nodoc:
1
4
  module Lockme
5
+ # Mixin providing JSON encoding method compatible with LockMe API
2
6
  module JsonUtils
3
7
  def to_json
4
- @data.to_h.to_json.force_encoding('UTF-8').each_codepoint.reduce('') {|str, c| str << (c > 127 ? '\u'+c.to_s(16).rjust(4, '0') : c.chr) }
8
+ utf_str = @data.to_h.to_json.force_encoding('UTF-8')
9
+ utf_str.each_codepoint.reduce(+'') do |str, codepoint|
10
+ str << if codepoint > 127
11
+ '\u' + codepoint.to_s(16).rjust(4, '0')
12
+ else
13
+ codepoint.chr
14
+ end
15
+ end
5
16
  end
6
17
  end
7
18
  private_constant :JsonUtils
@@ -1,35 +1,43 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Lockme
4
+ # API request module providing argument processing and signing
2
5
  module SignedRequest
3
6
  def self.perform(method, path, data = nil)
4
- headers = signature(method.upcase, path.gsub(/^\//, ''), data)
5
- params = {
6
- body: data,
7
- headers: headers,
8
- debug_output: Lockme.logger
9
- }.delete_if {|k,v| k.nil? || v.nil? }
7
+ params = prepare_params(method, path, data)
10
8
 
11
9
  resp = Request.send(method.downcase, path, params).parsed_response
12
- if resp.is_a?(Hash) && resp['error']
13
- raise Lockme::Error.new(resp['error'])
10
+ if resp.is_a?(Hash) && resp.has_key?('error')
11
+ raise Lockme::Error, resp['error']
14
12
  end
15
13
  resp
16
14
  rescue JSON::ParserError
17
- raise Lockme::Error.new('Invalid response from the server')
15
+ raise Lockme::Error, 'Invalid response from the server'
18
16
  end
19
17
 
20
18
  def self.signature(*args)
21
- sha1 = Digest::SHA1.hexdigest([
19
+ digest = Digest::SHA1.hexdigest([
22
20
  *args,
23
21
  Lockme.api_secret
24
22
  ].compact.join(''))
25
23
 
26
24
  {
27
25
  'Partner-Key' => Lockme.api_key,
28
- 'Signature' => sha1
26
+ 'Signature' => digest
29
27
  }
30
28
  end
29
+
30
+ def self.prepare_params(method, path, data)
31
+ headers = signature(method.upcase, path.gsub(%r{^\/}, ''), data)
32
+ {
33
+ body: data,
34
+ headers: headers,
35
+ debug_output: Lockme.logger
36
+ }.delete_if { |key, val| key.nil? || val.nil? }
37
+ end
31
38
  end
32
39
 
40
+ # HTTParty wrapper including some basic configuration
33
41
  module Request
34
42
  include ::HTTParty
35
43
 
@@ -37,7 +45,7 @@ module Lockme
37
45
  'User-Agent' => 'Ruby.Lockme.Api',
38
46
  'Accept' => 'application/json',
39
47
  'Content-Type' => 'application/json'
40
- }
48
+ }.freeze
41
49
  API_VERSION = '1.1'
42
50
 
43
51
  base_uri "https://lockme.pl/api/v#{API_VERSION}/"
@@ -1,11 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Lockme
4
+ # LockMe API reservation object
2
5
  class Reservation < Lockme::Base
6
+ @base_path = '/reservation'
7
+
3
8
  def initialize(args = {})
4
9
  parse_lockme_json(args)
5
10
  end
6
11
 
12
+ def id
13
+ reservationid
14
+ end
15
+
7
16
  def self.find(lockme_id)
8
- self.new(Lockme::SignedRequest.perform("get", "/reservation/#{lockme_id}"))
17
+ new(SignedRequest.perform("get", singular_path(lockme_id)))
9
18
  end
10
19
 
11
20
  def save
@@ -17,29 +26,25 @@ module Lockme
17
26
  end
18
27
 
19
28
  def destroy
20
- Lockme::SignedRequest.perform("delete", "/reservation/#{@data.reservationid}")
21
- end
22
-
23
- # Check if object is persisted with Lockme
24
- def persisted?
25
- !@data.reservationid.nil?
29
+ SignedRequest.perform("delete", singular_path)
26
30
  end
27
31
 
28
32
  # Provide attribute accessors
29
- def method_missing(method, *args)
33
+ def method_missing(method, *args)
30
34
  @data.send method, *args
31
35
  end
32
36
 
33
37
  def create
34
- @data.reservationid = Lockme::SignedRequest.perform("put", "/reservation", self.to_json)
35
- return self
38
+ resp = SignedRequest.perform("put", collection_path, to_json)
39
+ @data.reservationid = resp
40
+ self
36
41
  end
37
42
  private :create
38
43
 
39
44
  def update
40
- resp = Lockme::SignedRequest.perform("post", "/reservation/#{@data.reservationid}", self.to_json)
45
+ resp = SignedRequest.perform("post", singular_path, to_json)
41
46
  parse_lockme_json(resp)
42
- return self
47
+ self
43
48
  end
44
49
  private :update
45
50
 
data/lib/lockme/room.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Lockme
4
+ # LockMe API room collection
2
5
  class Room < Lockme::Base
3
6
  def self.all
4
7
  Lockme::SignedRequest.perform("get", "/rooms")
data/lib/lockme/test.rb CHANGED
@@ -1,4 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ #:nodoc:
1
4
  module Lockme
5
+ # Mixin providing a method to test API connection
2
6
  module Test
3
7
  def test
4
8
  Lockme::SignedRequest.perform('get', '/test')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Lockme
2
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
3
5
  end
data/lib/lockme.rb CHANGED
@@ -1,5 +1,7 @@
1
- require 'httparty'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'forwardable'
4
+ require 'httparty'
3
5
 
4
6
  require 'lockme/configuration'
5
7
  require 'lockme/json_utils'
@@ -11,6 +13,7 @@ require 'lockme/reservation'
11
13
  require 'lockme/room'
12
14
  require 'lockme/version'
13
15
 
16
+ #:nodoc:
14
17
  module Lockme
15
18
  extend Configuration
16
19
  extend Test
data/lockme.gemspec CHANGED
@@ -21,8 +21,8 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.14"
24
- spec.add_development_dependency "rake", "~> 10.0"
25
24
  spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "simplecov"
27
27
  spec.add_dependency "httparty", "~> 0.15.6"
28
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lockme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Szyndel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-05 00:00:00.000000000 Z
11
+ date: 2018-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -25,33 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.14'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '5.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '5.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '5.0'
47
+ version: '10.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '5.0'
54
+ version: '10.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: simplecov
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -88,6 +88,7 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
+ - ".rubocop.yml"
91
92
  - ".travis.yml"
92
93
  - Gemfile
93
94
  - LICENSE.txt
@@ -95,6 +96,8 @@ files:
95
96
  - Rakefile
96
97
  - bin/console
97
98
  - bin/setup
99
+ - examples/README.md
100
+ - examples/basic.rb
98
101
  - lib/lockme.rb
99
102
  - lib/lockme/base.rb
100
103
  - lib/lockme/configuration.rb