alma_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2251cc05e84f6e62b33f7138a6a29016a1d78821
4
+ data.tar.gz: 8df5fa4c4ae44cf958b45b0f86d42aa306a2f10f
5
+ SHA512:
6
+ metadata.gz: ffe7b8a6ec6d0256686540beda16362ffd32fcb7bffb40eb3028c1b4f04788ac711e269ca25fee41f4e64ce6cc8ecfdb47850bc2b4252447a0bea5c0a834e0ec
7
+ data.tar.gz: de96d0f55d7de94861ecc0c0305b9e2e23ba2b7f3515ce0e7311558a2a06c50bd2184fd1471f5d302aa441d867cde7ddc5e4a9d513e553158bd14e2fb8af5e71
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0"
4
+ - "2.1"
5
+ - "2.2"
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in your gemspec
4
+ gemspec
5
+
6
+ if !ENV["CI"]
7
+ group :development do
8
+ gem "hashdiff"
9
+ gem "pry", "~> 0.9.12.6"
10
+ gem "pry-byebug", "<= 1.3.2"
11
+ gem "pry-rescue", "~> 1.4.1", github: "ConradIrwin/pry-rescue", branch: :master
12
+ gem "pry-stack_explorer", "~> 0.4.9.1"
13
+ gem "pry-syntax-hacks", "~> 0.0.6"
14
+ end
15
+ end
16
+
17
+ group :test do
18
+ gem "codeclimate-test-reporter", require: nil
19
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Michael Sievers
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.
@@ -0,0 +1,62 @@
1
+ # alma_api
2
+
3
+ This is a ruby wrapper for the Alma REST api. It aims to make working with this api as pleasant as possible. Options and parameters are the same as for the original api. It also sanitizes some api quirks such as unnecessary case sensitive option values.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "alma_api"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install alma_api
20
+
21
+ ## Usage
22
+
23
+ In order to use the api client you have to provide an apikey and the host you want to connect to. Both can be provided as paramters or as environment variables named ```ALMA_APIKEY``` or ```ALMA_HOST```.
24
+
25
+ ```ruby
26
+ require "alma_api"
27
+
28
+ client = AlmaApi::Client.new(apikey: "your_api_key", host: "https://alma_host")
29
+
30
+ # calling semantic resembles the api as close as possible
31
+ client.users.get # => [...] users collection
32
+ client.users("user_id").get # => user object
33
+ client.users("user_id").get(view: :brief) # => user object (brief view)
34
+ client.users("user_id").fees.get # => fees of a user
35
+ client.users("user_id").fees("fee_id").get # => fees of a user
36
+ ```
37
+
38
+ This gem uses ```mighty_struct``` for its results. This means you can navigate results with object notation like with ```OpenStruct```. If you want to get the underlaying hash/array just call `to_a` or `to_h`.
39
+
40
+ ```ruby
41
+ require "alma_api"
42
+
43
+ client = AlmaApi::Client.new(apikey: "your_api_key", host: "https://alma_host")
44
+ some_user = client.users(client.users.get.first.primary_id).get
45
+
46
+ # now you can investigate the user with method notation
47
+ some_user.contact_info.email.first.email_address # => "some@email_adress.org"
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://github.com/ubpb/alma_api/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :benchmark do
9
+ require_relative "./benchmark/joffrey/mab_document"
10
+ require_relative "./benchmark/joffrey/mab_xml_parser"
11
+
12
+ #Benchmark::Joffrey::MabDocument.new.call
13
+ Benchmark::Joffrey::MabXmlParser.new.call
14
+ end
@@ -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 "alma_api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "alma_api"
8
+ spec.version = AlmaApi::VERSION
9
+ spec.authors = ["Michael Sievers"]
10
+ spec.summary = %q{A ruby wrapper for the Alma REST api}
11
+ spec.homepage = "https://github.com/ubpb/alma-api"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+ spec.bindir = "exe"
15
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "activesupport"
19
+ spec.add_dependency "faraday"
20
+ spec.add_dependency "mighty_struct", ">= 0.1.2"
21
+
22
+ spec.add_development_dependency "benchmark-ips"
23
+ spec.add_development_dependency "bundler", ">= 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", ">= 3.0.0", "< 4.0.0"
26
+ spec.add_development_dependency "simplecov", ">= 0.8.0"
27
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "alma_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ #require "irb"
14
+ #IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+ require "alma_api/version"
4
+
5
+ module AlmaApi
6
+ require_relative "./alma_api/client"
7
+
8
+ def self.debug(arg)
9
+ binding.pry
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ require "faraday"
2
+ require_relative "../alma_api"
3
+
4
+ class AlmaApi::Client
5
+ require_relative "./client/users"
6
+
7
+ API_ROOT_PATH="/almaws"
8
+
9
+ attr_accessor :apikey
10
+ attr_accessor :host
11
+
12
+ alias_method :api_key, :apikey
13
+ alias_method :api_key=, :apikey=
14
+
15
+ def initialize(options = {})
16
+ options.symbolize_keys.try do |_sanitized_options|
17
+ @apikey = _sanitized_options[:apikey] || _sanitized_options[:api_key] || ENV["ALMA_APIKEY"] || ENV["ALMA_API_KEY"]
18
+ @host = (_sanitized_options[:host] || ENV["ALMA_HOST"]).try(:strip)
19
+ end
20
+ end
21
+
22
+ def users(user_id = nil)
23
+ Users.new(self, user_id)
24
+ end
25
+
26
+ # inter-client api
27
+ def http(method, path, options = {})
28
+ connection = Faraday.new.tap do |_connection|
29
+ _connection.headers["Accept"] = "application/json"
30
+ _connection.params["apikey"] = apikey
31
+ end
32
+
33
+ connection.send(method, "#{host}#{API_ROOT_PATH}#{path}", options)
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ require "alma_api/collection"
2
+ require "alma_api/object"
3
+ require "json"
4
+ require_relative "../client"
5
+
6
+ class AlmaApi::Client::Users
7
+ require_relative "./users/fees"
8
+ require_relative "./users/loans"
9
+ require_relative "./users/requests"
10
+
11
+ def initialize(client, user_id = nil)
12
+ @client = client
13
+ @user_id = user_id
14
+ end
15
+
16
+ def get(options = {})
17
+ if @user_id
18
+ (options = options.deep_dup).symbolize_keys!.reverse_merge!({
19
+ user_id_type: :all_unique,
20
+ view: :full
21
+ })
22
+
23
+ @client.http(:get, "/v1/users/#{@user_id}", options).try do |_response|
24
+ if _response.status == 200
25
+ AlmaApi::Object.new(JSON.parse(_response.body))
26
+ end
27
+ end
28
+ else
29
+ (options = options.deep_dup).symbolize_keys!.reverse_merge!({
30
+ limit: 10,
31
+ offset: 0,
32
+ order_by: [
33
+ :last_name,
34
+ :first_name,
35
+ :primary_id
36
+ ]
37
+ })
38
+
39
+ @client.http(:get, "/v1/users", options).try do |_response|
40
+ if _response.status == 200
41
+ AlmaApi::Collection.new(JSON.parse(_response.body), "user")
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ def fees(fee_id = nil)
48
+ Fees.new(@client, @user_id, fee_id)
49
+ end
50
+
51
+ def loans(loan_id = nil)
52
+ Loans.new(@client, @user_id, loan_id)
53
+ end
54
+
55
+ def requests(request_id = nil)
56
+ Requests.new(@client, @user_id, request_id)
57
+ end
58
+ end
@@ -0,0 +1,40 @@
1
+ require "alma_api/collection"
2
+ require "json"
3
+ require_relative "../users"
4
+
5
+ class AlmaApi::Client::Users::Fees
6
+ def initialize(client, user_id, fee_id = nil)
7
+ @client = client
8
+ @fee_id = fee_id
9
+ @user_id = user_id
10
+ end
11
+
12
+ def get(options = {})
13
+ if @fee_id
14
+ (options = options.deep_dup).symbolize_keys!.reverse_merge!({
15
+ user_id_type: :all_unique
16
+ })
17
+
18
+ @client.http(:get, "/v1/users/#{@user_id}/fees/#{@fee_id}", options).try do |_response|
19
+ if _response.status == 200
20
+ AlmaApi::Object.new(JSON.parse(_response.body))
21
+ end
22
+ end
23
+
24
+ else
25
+ (options = options.deep_dup).symbolize_keys!.reverse_merge!({
26
+ user_id_type: :all_unique,
27
+ status: :ACTIVE
28
+ })
29
+
30
+ # sanitize api quirks
31
+ options[:status] = options[:status].to_s.upcase.to_sym
32
+
33
+ @client.http(:get, "/v1/users/#{@user_id}/fees", options).try do |_response|
34
+ if _response.status == 200
35
+ AlmaApi::Collection.new(JSON.parse(_response.body), "fee")
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ require "alma_api/collection"
2
+ require "json"
3
+ require_relative "../users"
4
+
5
+ class AlmaApi::Client::Users::Loans
6
+ def initialize(client, user_id, loan_id = nil)
7
+ @client = client
8
+ @loan_id = loan_id
9
+ @user_id = user_id
10
+ end
11
+
12
+ def get(options = {})
13
+ (options = options.deep_dup).symbolize_keys!.reverse_merge!({
14
+ user_id_type: :all_unique,
15
+ limit: 10,
16
+ offset: 0,
17
+ order_by: :id,
18
+ direction: :ASC
19
+ })
20
+
21
+ # sanitize api quirks
22
+ options[:direction] = options[:direction].to_s.upcase.to_sym
23
+
24
+ @client.http(:get, "/v1/users/#{@user_id}/loans", options).try do |_response|
25
+ if _response.status == 200
26
+ AlmaApi::Collection.new(JSON.parse(_response.body), "item_loan")
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require "alma_api/collection"
2
+ require "json"
3
+ require_relative "../users"
4
+
5
+ class AlmaApi::Client::Users::Requests
6
+ def initialize(client, user_id, request_id = nil)
7
+ @client = client
8
+ @request_id = request_id
9
+ @user_id = user_id
10
+ end
11
+
12
+ def get(options = {})
13
+ (options = options.deep_dup).symbolize_keys!.reverse_merge!({
14
+ request_type: nil, # :HOLD, :DIGITIZATION, :BOOKING
15
+ limit: 10,
16
+ offset: 0,
17
+ status: :active # :active, :history
18
+ })
19
+
20
+ # sanitize api quirks
21
+ options[:request_type] = options[:request_type].try(:to_s).try(:upcase).try(:to_sym)
22
+ options[:status] = options[:status].to_s.downcase.to_sym
23
+
24
+ @client.http(:get, "/v1/users/#{@user_id}/requests", options).try do |_response|
25
+ if _response.status == 200
26
+ AlmaApi::Collection.new(JSON.parse(_response.body), "user_request")
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ require_relative "./object"
2
+ require_relative "../alma_api"
3
+
4
+ class AlmaApi::Collection < Delegator
5
+ def initialize(hash, items_key, item_class = AlmaApi::Object)
6
+ hash.each do |_key, _value|
7
+ if _key != items_key
8
+ singleton_class.class_eval do
9
+ attr_accessor "#{_key}".to_sym
10
+ end
11
+
12
+ instance_variable_set("@#{_key}".to_sym, _value)
13
+ end
14
+ end
15
+
16
+ @items = hash.try(:[], items_key).try(:map) do |_item|
17
+ item_class.new(_item)
18
+ end || []
19
+ end
20
+
21
+ def __getobj__
22
+ @items
23
+ end
24
+
25
+ def __setobj__(obj)
26
+ @items = obj
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ require "mighty_struct"
2
+ require_relative "../alma_api"
3
+
4
+ class AlmaApi::Object < MightyStruct
5
+ def initialize(object)
6
+ if object.is_a?(Hash)
7
+ object.each do |_key, _value|
8
+ if _value.is_a?(String) && _value[/\A\d\d\d\d-\d\d-\d\d\w.*/]
9
+ object[_key] = Date.parse(_value)
10
+ end
11
+ end
12
+ end
13
+
14
+ super(object)
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module AlmaApi
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alma_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Sievers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
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: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: mighty_struct
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: benchmark-ips
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: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.0
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: 4.0.0
107
+ type: :development
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 3.0.0
114
+ - - "<"
115
+ - !ruby/object:Gem::Version
116
+ version: 4.0.0
117
+ - !ruby/object:Gem::Dependency
118
+ name: simplecov
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 0.8.0
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 0.8.0
131
+ description:
132
+ email:
133
+ executables: []
134
+ extensions: []
135
+ extra_rdoc_files: []
136
+ files:
137
+ - ".gitignore"
138
+ - ".rspec"
139
+ - ".travis.yml"
140
+ - Gemfile
141
+ - LICENSE.txt
142
+ - README.md
143
+ - Rakefile
144
+ - alma_api.gemspec
145
+ - bin/console
146
+ - bin/setup
147
+ - lib/alma_api.rb
148
+ - lib/alma_api/client.rb
149
+ - lib/alma_api/client/users.rb
150
+ - lib/alma_api/client/users/fees.rb
151
+ - lib/alma_api/client/users/loans.rb
152
+ - lib/alma_api/client/users/requests.rb
153
+ - lib/alma_api/collection.rb
154
+ - lib/alma_api/object.rb
155
+ - lib/alma_api/version.rb
156
+ homepage: https://github.com/ubpb/alma-api
157
+ licenses: []
158
+ metadata: {}
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubyforge_project:
175
+ rubygems_version: 2.4.6
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: A ruby wrapper for the Alma REST api
179
+ test_files: []