Class: Koha::Client

Inherits:
Object
  • Object
show all
Includes:
Biblio, Info, User
Defined in:
lib/koha/client.rb

Defined Under Namespace

Modules: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Biblio

#biblio_holdable?, #biblio_items_holdable?, #find_biblio, #find_biblio_items, #find_items, #hold_biblio, #hold_item, #is_holdable?, #item_holdable?

Methods included from User

#all_users, #delete_hold, #renew_issue, #today_users, #user_holds, #user_issues

Methods included from Info

#branches

Constructor Details

#initialize(connection, options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/koha/client.rb', line 11

def initialize connection, options = {}
  @proxy = @uri = nil
  @connection = connection
  unless false === options[:url]
    url = options[:url] ? options[:url].dup : 'http://localhost/cgi-bin/koha/rest.pl/'
    @uri = Koha::Uri.create url
    if options[:proxy]
      proxy_url = options[:proxy].dup
      @proxy = Koha::Uri.create proxy_url if proxy_url
    end
  end
  @options = options
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/koha/client.rb', line 9

def connection
  @connection
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/koha/client.rb', line 9

def options
  @options
end

#proxyObject (readonly)

Returns the value of attribute proxy.



9
10
11
# File 'lib/koha/client.rb', line 9

def proxy
  @proxy
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/koha/client.rb', line 9

def uri
  @uri
end

Instance Method Details

#base_uriObject

returns the uri proxy if present, otherwise just the uri object.



27
28
29
# File 'lib/koha/client.rb', line 27

def base_uri
  @proxy ? @proxy : @uri
end

#build_request(path, opts) ⇒ Object

Sets up the uri/query string

@param path [String, Symbol]  the request path
@param opts [Hash] The options for the REST call. Can include:  
   :method, :params, :data, :uri, :path, :query


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/koha/client.rb', line 60

def build_request path, opts
  raise "path must be a string or symbol, not #{path.inspect}" unless [String,Symbol].include?(path.class)
  path = path.to_s
  opts[:proxy] = proxy unless proxy.nil?
  opts[:method] ||= :get
  raise "The :data option can only be used if :method => :post" if opts[:method] != :post and opts[:data]
  opts[:params] ||= {}
  opts[:data] ||= {}
  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
    opts[:params][:borrowernumber] = opts[:borrowernumber] if opts[:borrowernumber]
    opts[:params][:user_name] = opts[:borrowername] if opts[:borrowername]
  else
    opts[:data][:borrowernumber] = opts[:borrowernumber] if opts[:borrowernumber]
    opts[:data][:user_name] = opts[:borrowername] if opts[:borrowername]
  end
  query = Koha::Uri.to_params(opts[:params]) unless opts[:params].empty?
  opts[:query] = query    
  opts[:path] = path
  if base_uri
    opts[:uri] = URI.join(base_uri, path.to_s)
    opts[:uri].merge!("?#{query}" ) if query
  end
  opts
end

#process_response(request, response) ⇒ Object

Recieves the request and response from the connection and format it. Returns an object with the response body and @request and @response.



91
92
93
94
95
96
97
98
99
# File 'lib/koha/client.rb', line 91

def process_response request, response
  raise "The response does not have the correct keys => :body, :headers, :status" unless
    %W(body headers status) == response.keys.map{|k|k.to_s}.sort
  raise Koha::Error::Http.new request, response unless [200,302].include? response[:status]
  result = request[:evaluate] ? evaluate_json_response( response, request[:evaluate]) : response[:body]
  result.extend Response
  result.request, result.response = request, response
  result
end

#send_request(path, opts) ⇒ Object

the main request method responsible for sending requests.

@param path [String]  the client's API REST method
@param opts [Hash] A hash which can contain the following keys: 
     [:method] : optional - the http method (:get, :post or :put)
     [:params] : optional - the query string params in hash form
     All other options are passed to the execute method


47
48
49
50
51
52
53
# File 'lib/koha/client.rb', line 47

def send_request path, opts
  request_context = build_request path, opts
  [:open_timeout, :read_timeout].each do |k|
    request_context[k] = @options[k]
  end
  request request_context
end