koha 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ module Koha
6
6
 
7
7
  %W(Client Error Connection Uri Version).each{|n| autoload n.to_sym, "koha/#{n.downcase}"}
8
8
 
9
- def self.version; "0.0.1" end
9
+ def self.version; "0.0.2" end
10
10
 
11
11
  VERSION = self.version
12
12
 
@@ -27,7 +27,7 @@ class Koha::Client
27
27
  end
28
28
 
29
29
  # Create the get, post, and head methods
30
- %W(get post put).each do |meth|
30
+ %W(get post put delete).each do |meth|
31
31
  class_eval <<-RUBY
32
32
  def #{meth} path, opts = {}, &block
33
33
  send_and_receive path, opts.merge(:method => :#{meth}), &block
@@ -70,6 +70,7 @@ class Koha::Client
70
70
  # returns a hash with the following keys:
71
71
  # :method
72
72
  # :params
73
+ # :data
73
74
  # :uri
74
75
  # :path
75
76
  # :query
@@ -79,11 +80,25 @@ class Koha::Client
79
80
  path = path.to_s
80
81
  opts[:proxy] = proxy unless proxy.nil?
81
82
  opts[:method] ||= :get
83
+ raise "The :data option can only be used if :method => :post" if opts[:method] != :post and opts[:data]
82
84
  opts[:params] ||= {}
83
- opts[:params][:borrowernumber] = opts[:borrowernumber] if opts[:borrowernumber]
84
- opts[:params][:user_name] = opts[:borrowername] if opts[:borrowername]
85
+ opts[:data] ||= {}
86
+ if opts[:method] == :get #for get q's we want user info in the params, for all others it will be a data post/put param
87
+ opts[:params][:borrowernumber] = opts[:borrowernumber] if opts[:borrowernumber]
88
+ opts[:params][:user_name] = opts[:borrowername] if opts[:borrowername]
89
+ else
90
+ opts[:data][:borrowernumber] = opts[:borrowernumber] if opts[:borrowernumber]
91
+ opts[:data][:user_name] = opts[:borrowername] if opts[:borrowername]
92
+ end
85
93
  query = Koha::Uri.to_params(opts[:params]) unless opts[:params].empty?
86
94
  opts[:query] = query
95
+
96
+ if opts[:data].is_a? Hash
97
+ opts[:data] = Koha::Uri.to_params opts[:data]
98
+ opts[:headers] ||= {}
99
+ opts[:headers]['Content-Type'] ||= 'application/x-www-form-urlencoded; charset=UTF-8'
100
+ end
101
+
87
102
  opts[:path] = path
88
103
  if base_uri
89
104
  opts[:uri] = URI.join(base_uri, path.to_s)
@@ -108,7 +123,7 @@ class Koha::Client
108
123
  raise "The response does not have the correct keys => :body, :headers, :status" unless
109
124
  %W(body headers status) == response.keys.map{|k|k.to_s}.sort
110
125
  raise Koha::Error::Http.new request, response unless [200,302].include? response[:status]
111
- result = request[:evaluate] ? evaluate_json_response(request, response, request[:evaluate]) : response[:body]
126
+ result = request[:evaluate] ? evaluate_json_response( response, request[:evaluate]) : response[:body]
112
127
  result.extend Context
113
128
  result.request, result.response = request, response
114
129
  result
@@ -144,33 +159,65 @@ class Koha::Client
144
159
  JSON.parse(get path, opts)
145
160
  end
146
161
 
162
+ def delete_hold opts= {}
163
+ opts[:evaluate] ||= "canceled"
164
+ opts[:prefix] = opts[:biblionumber] ? "biblio" : "item" #delete end-point is like :/user/byid/544/holds/biblio/1
165
+ path, opts = build_user_path("holds", opts)
166
+ return delete(path, opts)
167
+ end
168
+
147
169
  def user_issues opts= {}
148
170
  path, opts = build_user_path("issues", opts)
149
171
  JSON.parse(get path, opts)
150
172
  end
151
173
 
152
- def build_user_path rest_method, opts= {}
174
+ def renew_issue opts={}
175
+ path, opts = build_user_path("issues", opts)
176
+ JSON.parse(put path, opts)
177
+ end
178
+
179
+
180
+ def build_user_path koha_method, opts= {}
153
181
  raise ArgumentError unless ( opts[:borrowernumber] or opts[:borrowername] ) #we have to be passed either a name or number
154
182
  borrowernumber, borrowername = opts.delete(:borrowernumber), opts.delete(:borrowername)
155
- path = borrowernumber ? "user/byid/#{borrowernumber}/#{rest_method}" : "user/#{borrowername}/#{rest_method}"
183
+ biblionumber, itemnumber = opts.delete(:biblionumber), opts.delete(:itemnumber)
184
+ prefix = opts.delete(:prefix)
185
+ path = borrowernumber ? "user/byid/#{borrowernumber}/#{koha_method}/" : "user/#{borrowername}/#{koha_method}/"
186
+ path << prefix if prefix
187
+ path << "/#{itemnumber}/" if itemnumber
188
+ path << "/#{biblionumber}/" if ( biblionumber && itemnumber.nil?)
156
189
  return path, opts
157
190
  end
158
191
 
159
192
 
160
193
  ### Biblio and Item Methods ###
161
194
 
162
- # This method will get the item record from koha given the biblionumber
195
+ # This method will get the biblio record from koha given the biblionumber
163
196
  def find_biblio biblionumber, opts = {}
164
197
  biblionumber = biblionumber.to_s
165
- JSON.parse(get "biblio/#{biblionumber}/items", opts)
198
+ JSON.parse(get "biblio/#{biblionumber}", opts)
166
199
  end
167
200
 
201
+ # This method will get the item records from koha given the biblionumber
202
+ def find_biblio_items biblionumber, opts = {}
203
+ biblionumber = biblionumber.to_s
204
+ JSON.parse(get "biblio/#{biblionumber}/items", opts)
205
+ end
206
+
207
+
168
208
  # wrapper to check if a biblio is holdable
169
209
  # take a koha biblio number and standard client opts
170
210
  def biblio_holdable?(biblionumber, opts = {})
171
211
  is_holdable?(:biblio, biblionumber, opts )
172
212
  end
173
213
 
214
+ # makes a hold on the title.
215
+ # takes a koha biblionumber and the client opts, which should include either a borrowernumber or user_name param.
216
+ # returns a hash of the pickup location and hold date.
217
+ def hold_biblio(biblionumber, opts = {})
218
+ JSON.parse(post "biblio/#{biblionumber}/hold", opts )
219
+ end
220
+
174
221
  # wrapper to check a biblio items holdabale statues.
175
222
  # takes a koha bilionumber and standard client opts
176
223
  # this returns just a hash of the items and their status, no need to evaulate.
@@ -186,6 +233,13 @@ class Koha::Client
186
233
  is_holdable?(:item, itemnumber, opts)
187
234
  end
188
235
 
236
+ # makes a hold on the title.
237
+ # takes a koha biblionumber and the client opts, which should include either a borrowernumber or user_name param.
238
+ # returns a hash of the pickup location and hold date.
239
+ def hold_item(itemnumber, opts = {})
240
+ JSON.parse( post "item/#{itemnumber}/hold", opts )
241
+ end
242
+
189
243
  def is_holdable?(koha_type, identifier, opts = {} )
190
244
  opts ||= {}
191
245
  opts[:evaluate] = :is_holdable unless opts[:evaluate] == false
@@ -195,13 +249,21 @@ class Koha::Client
195
249
  get "#{koha_type}/#{identifier}/#{holdable}", opts
196
250
  end
197
251
 
252
+
198
253
  protected
199
254
 
200
255
  # this is used to retrun a ruby primitive based on a json node. For example holdable returns { "is_holdable" : true, "reasons" : [] }
201
256
  # and we just want true.
202
- def evaluate_json_response request, response, node
257
+ def evaluate_json_response response, node
203
258
  json = JSON.parse(response[:body])
204
- json[node.to_s]
259
+ case json[node.to_s] # god-damn perl messing up my json
260
+ when "true"
261
+ return true
262
+ when "false"
263
+ return false
264
+ else
265
+ return json[node.to_s]
266
+ end
205
267
  end
206
268
 
207
269
 
@@ -50,8 +50,10 @@ class Koha::Connection
50
50
  Net::HTTP::Post
51
51
  when :put
52
52
  Net::HTTP::Put
53
+ when :delete
54
+ Net::HTTP::Delete
53
55
  else
54
- raise "Only :get, :post and :head http method types are allowed."
56
+ raise "Only :get, :post and :delete http method types are allowed."
55
57
  end
56
58
  headers = request_context[:headers] || {}
57
59
  raw_request = http_method.new request_context[:uri].request_uri
@@ -1,10 +1,30 @@
1
1
  module Koha::Error
2
2
 
3
+ module KohaContext
4
+
5
+ attr_accessor :request, :response
6
+
7
+ def to_s
8
+ m = "#{super.to_s}"
9
+ if response
10
+ m << " - #{response[:status]} #{Http::STATUS_CODES[response[:status].to_i]}"
11
+ m << "\nError: #{response[:body]}\n" if response[:body]
12
+ end
13
+ p = "\nURI: #{request[:uri].to_s}"
14
+ p << "\nRequest Headers: #{request[:headers].inspect}" if request[:headers]
15
+ p << "\nRequest Data: #{request[:data].inspect}" if request[:data]
16
+ p << "\n"
17
+ p << "\nBacktrace: " + self.backtrace[0..10].join("\n")
18
+ m << p
19
+ m
20
+ end
21
+
22
+ end
3
23
 
4
24
 
5
25
  class Http < RuntimeError
6
26
 
7
-
27
+ include KohaContext
8
28
  # ripped right from ActionPack
9
29
  # Defines the standard HTTP status codes, by integer, with their
10
30
  # corresponding default message texts.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-21 00:00:00.000000000 Z
12
+ date: 2013-03-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: simplecov
@@ -162,15 +162,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
162
  - - ! '>='
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
+ segments:
166
+ - 0
167
+ hash: -1777528153601572521
165
168
  required_rubygems_version: !ruby/object:Gem::Requirement
166
169
  none: false
167
170
  requirements:
168
171
  - - ! '>='
169
172
  - !ruby/object:Gem::Version
170
173
  version: '0'
174
+ segments:
175
+ - 0
176
+ hash: -1777528153601572521
171
177
  requirements: []
172
178
  rubyforge_project:
173
- rubygems_version: 1.8.24
179
+ rubygems_version: 1.8.25
174
180
  signing_key:
175
181
  specification_version: 3
176
182
  summary: A Ruby client for Koha ILSDI interface