ddr-aux-client 1.1.0 → 1.2.0

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: 8f9c3cc6c1e5f15ab0e067460e227071a3493f11
4
- data.tar.gz: ecf92af2bfd5010f4bd33295c661996f3732f7a7
3
+ metadata.gz: 859ccede341d46abeeb166e9e69de02cb36d93ef
4
+ data.tar.gz: a45bc63f297283af6d749eb3c39c882fd6095e60
5
5
  SHA512:
6
- metadata.gz: f8fb113dd55fbb8fa53471e6fa006dc57424e9700e08319a5a6aaa8ab3377bcc126545d33014df45c349c946f6ea1209b07c1651339105ae1c0ec8c974eff036
7
- data.tar.gz: b1d6a84bffb0d269b82ba086aac61e1d33deecd0b0e3c38d366d6077cf067175703ed2d6dbf4518959a2735c7e7997702426f4eb602bad2e5f1db84f421bf3fb
6
+ metadata.gz: 4ce07a5dea2a6b5a05c1d346bd0793b76af10283217cf8d918f840b568207f47459ca07df32c8332d4cebb5ac57fe88472b2184db770f3e95caef2088c53ff17
7
+ data.tar.gz: 9eb774411533890f521e79b5db0694ed2146a76f1eaff528e0f681ddf18cef540a7300846d418232cc9cd7e216649c16b47c7b22c978487b2371d76e59d7b862
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.1
3
+ - 2.2
4
4
  - 2.1.5
5
5
  before_install: gem install bundler -v 1.10.2
6
6
 
data/README.md CHANGED
@@ -20,7 +20,13 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ See [API](https://github.com/duke-libraries/ddr-aux-client/wiki/API).
24
+
25
+ Configure the ddr-aux API URL via the `DDR_AUX_API_URL` environment variable, or by using the attribute setter:
26
+
27
+ ```ruby
28
+ DdrAux::Client.api_url = "http://localhost:3000/api"
29
+ ```
24
30
 
25
31
  ## Development
26
32
 
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.add_dependency "hashie", "~> 3.4"
21
+
20
22
  spec.add_development_dependency "bundler", "~> 1.10"
21
23
  spec.add_development_dependency "rake", "~> 10.0"
22
24
  spec.add_development_dependency "rspec"
@@ -1,16 +1,25 @@
1
1
  require "ddr_aux/client/version"
2
- require "ddr_aux/client/connection"
3
- require "ddr_aux/client/api"
4
2
 
5
3
  module DdrAux
6
4
  module Client
7
- extend Api
5
+ autoload :AdminSet, "ddr_aux/client/admin_set"
6
+ autoload :Api, "ddr_aux/client/api"
7
+ autoload :Connection, "ddr_aux/client/connection"
8
+ autoload :License, "ddr_aux/client/license"
9
+ autoload :Model, "ddr_aux/client/model"
10
+ autoload :Request, "ddr_aux/client/request"
11
+ autoload :Response, "ddr_aux/client/response"
8
12
 
9
13
  class << self
10
14
  attr_accessor :api_url
15
+
16
+ def uri
17
+ URI(api_url)
18
+ end
11
19
  end
12
20
 
13
21
  self.api_url = ENV["DDR_AUX_API_URL"]
14
22
 
23
+ extend Api
15
24
  end
16
25
  end
@@ -0,0 +1,9 @@
1
+ module DdrAux::Client
2
+ class AdminSet < Model
3
+
4
+ self.path = "/admin_sets"
5
+
6
+ coerce_timestamps!
7
+
8
+ end
9
+ end
@@ -1,31 +1,29 @@
1
- require "uri"
2
- require_relative "api/model"
3
- require_relative "api/license"
4
- require_relative "api/admin_set"
5
-
6
1
  module DdrAux::Client
7
2
  module Api
8
3
 
9
4
  MODELS = {
10
- license: License,
11
- admin_set: AdminSet,
5
+ license: "License",
6
+ admin_set: "AdminSet",
12
7
  }.freeze
13
8
 
14
- MODELS.each do |key, klass|
9
+ MODELS.each do |key, class_name|
15
10
  # get_license(id) => License.get(id)
16
11
  define_method "get_#{key}" do |id|
12
+ klass = const_get(class_name, false)
17
13
  klass.get(id)
18
14
  end
19
15
 
20
16
  # get_licenses => License.list
21
17
  # licenses => License.list
22
18
  define_method "get_#{key}s" do
19
+ klass = const_get(class_name, false)
23
20
  klass.list
24
21
  end
25
22
  alias_method "#{key}s", "get_#{key}s"
26
23
 
27
24
  # find_license(**args) => License.find(**args)
28
25
  define_method "find_#{key}" do |args|
26
+ klass = const_get(class_name, false)
29
27
  klass.find **args
30
28
  end
31
29
 
@@ -1,6 +1,5 @@
1
- require 'net/http'
2
- require 'json'
3
- require 'delegate'
1
+ require "net/http"
2
+ require "delegate"
4
3
 
5
4
  module DdrAux::Client
6
5
  class Connection < SimpleDelegator
@@ -10,29 +9,18 @@ module DdrAux::Client
10
9
  end
11
10
 
12
11
  def initialize
13
- conn = Net::HTTP.new(uri.host, uri.port)
12
+ uri = DdrAux::Client.uri
13
+ super Net::HTTP.new(uri.host, uri.port)
14
14
  if uri.scheme == "https"
15
- conn.use_ssl = true
16
- conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
15
+ self.use_ssl = true
16
+ self.verify_mode = OpenSSL::SSL::VERIFY_NONE
17
17
  end
18
- super conn
19
18
  end
20
19
 
21
20
  def get_response(relative_path)
22
- path = uri.path + relative_path
23
- res = get(path, request_headers)
24
- res.value # raises exception if not 2XX response code
25
- JSON.parse(res.body)
26
- end
27
-
28
- private
29
-
30
- def uri
31
- URI(DdrAux::Client.api_url)
32
- end
33
-
34
- def request_headers
35
- {"Accept"=>"application/json"}
21
+ warn "[DEPRECATION] `get_response` is deprecated and will be removed from ddr-aux-client 2.0." \
22
+ " Use `DdrAux::Client::Request.get_response` instead."
23
+ Request.get_response(relative_path)
36
24
  end
37
25
 
38
26
  end
@@ -0,0 +1,9 @@
1
+ module DdrAux::Client
2
+ class License < Model
3
+
4
+ self.path = "/licenses"
5
+
6
+ coerce_timestamps!
7
+
8
+ end
9
+ end
@@ -0,0 +1,49 @@
1
+ require "uri"
2
+ require "hashie"
3
+ require "delegate"
4
+ require "time"
5
+
6
+ module DdrAux::Client
7
+ # @abstract
8
+ class Model < Hashie::Hash
9
+ include Hashie::Extensions::MergeInitializer
10
+ include Hashie::Extensions::IndifferentAccess
11
+ include Hashie::Extensions::MethodReader
12
+ include Hashie::Extensions::Coercion
13
+
14
+ DEFAULT_TIMESTAMPS = [:created_at, :updated_at].freeze
15
+
16
+ class << self
17
+
18
+ attr_accessor :path
19
+ private :path=
20
+
21
+ def coerce_timestamps!(*keys)
22
+ if keys.empty?
23
+ keys = DEFAULT_TIMESTAMPS
24
+ end
25
+ coerce_keys *keys, ->(v) { ::Time.parse(v) }
26
+ end
27
+
28
+ def get(id)
29
+ request_path = [path, id].join("/")
30
+ response = Request.get_response(request_path)
31
+ new response.to_h
32
+ end
33
+
34
+ def list
35
+ response = Request.get_response(path)
36
+ response.map { |record| new(record) }
37
+ end
38
+ alias_method :all, :list
39
+
40
+ def find(**args)
41
+ request_path = "#{path}/find?%s" % URI.encode_www_form(args)
42
+ response = Request.get_response(request_path)
43
+ new response.to_h
44
+ end
45
+ alias_method :where, :find
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,28 @@
1
+ module DdrAux::Client
2
+ class Request
3
+
4
+ attr_reader :connection, :path
5
+
6
+ def self.get_response(path)
7
+ new(path).get_response
8
+ end
9
+
10
+ def initialize(path)
11
+ @path = path
12
+ @connection = Connection.new
13
+ end
14
+
15
+ def get_response
16
+ request_path = DdrAux::Client.uri.path + path
17
+ http_res = connection.get(request_path, request_headers)
18
+ Response.new http_res
19
+ end
20
+
21
+ private
22
+
23
+ def request_headers
24
+ {"Accept"=>"application/json"}
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ require "json"
2
+ require "delegate"
3
+
4
+ module DdrAux::Client
5
+ class Response < SimpleDelegator
6
+
7
+ def initialize(response)
8
+ response.value # raises exception if not 2XX response code
9
+ super JSON.parse(response.body)
10
+ end
11
+
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  module DdrAux
2
2
  module Client
3
- VERSION = "1.1.0"
3
+ VERSION = "1.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddr-aux-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Chandek-Stark
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-16 00:00:00.000000000 Z
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hashie
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.4'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -84,11 +98,13 @@ files:
84
98
  - bin/setup
85
99
  - ddr-aux-client.gemspec
86
100
  - lib/ddr_aux/client.rb
101
+ - lib/ddr_aux/client/admin_set.rb
87
102
  - lib/ddr_aux/client/api.rb
88
- - lib/ddr_aux/client/api/admin_set.rb
89
- - lib/ddr_aux/client/api/license.rb
90
- - lib/ddr_aux/client/api/model.rb
91
103
  - lib/ddr_aux/client/connection.rb
104
+ - lib/ddr_aux/client/license.rb
105
+ - lib/ddr_aux/client/model.rb
106
+ - lib/ddr_aux/client/request.rb
107
+ - lib/ddr_aux/client/response.rb
92
108
  - lib/ddr_aux/client/version.rb
93
109
  homepage: https://github.com/duke-libraries/ddr-aux-client
94
110
  licenses: []
@@ -1,9 +0,0 @@
1
- module DdrAux::Client
2
- module Api
3
- class AdminSet < Model
4
-
5
- self.path = "/admin_sets"
6
-
7
- end
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- module DdrAux::Client
2
- module Api
3
- class License < Model
4
-
5
- self.path = "/licenses"
6
-
7
- end
8
- end
9
- end
@@ -1,26 +0,0 @@
1
- require "uri"
2
-
3
- module DdrAux::Client
4
- module Api
5
- class Model
6
-
7
- class << self
8
- attr_accessor :path
9
-
10
- def get(id)
11
- Connection.call [path, id].join("/")
12
- end
13
-
14
- def list
15
- Connection.call path
16
- end
17
- alias_method :all, :list
18
-
19
- def find(**args)
20
- Connection.call "#{path}/find?%s" % URI.encode_www_form(args)
21
- end
22
- end
23
-
24
- end
25
- end
26
- end