Class: RubyBHL::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyBHL/request.rb

Overview

Constant Summary collapse

BASE_URL =
'http://www.biodiversitylibrary.org'
API_VERSION =
'api2'
INTERFACE =
'httpquery.ashx?'
FORMAT =
'json'
SEARCH_BASE =
[BASE_URL, API_VERSION, INTERFACE].join("/")
METHODS =
{ 
  AuthorSearch: %w{name},
  BookSearch: %w{title lname volume edition year subject language collectionid}, 
  GetAuthorParts: %w{creatorid}, 
  GetAuthorTitles: %w{creatorid},  
  GetCollections: %w{},        # no params
  GetItemByIdentifier: %w{type value}, 
  GetItemMetadata: %w{itemid pages oc parts}, 
  GetItemPages: %w{itemid ocr}, 
  GetItemParts: %w{itemid}, 
  GetLanguages: %w{},          # no params
  GetPageMetadata: %w{pageid ocr names}, 
  GetPageNames: %w{pageid}, 
  GetPageOcrText: %w{pageid}, 
  GetPartBibTeX: %w{partid}, 
  GetPartByIdentifier: %w{type value}, 
  GetPartEndNote: %w{partid}, 
  GetPartMetadata: %w{partid}, 
  GetPartNames: %w{partid}, 
  GetSubjectParts: %w{subject}, 
  GetSubjectTitles: %w{subject}, 
  GetTitleBibTex: %w{titleid}, 
  GetTitleByIdentifier: %w{type value}, 
  GetTitleEndNote: %w{titleid}, 
  GetTitleItems: %w{titleid}, 
  GetTitleMetadata: %w{titleid items}, 
  GetUnpublishedItems: %w{},      # No params 
  GetUnpublishedParts: %w{},      # No params 
  GetUnpublishedTitles: %w{},     # No params 
  NameCount: %w{startdate enddate}, 
  NameGetDetail: %w{namebankid name},            # !! Not in list below part of V1, see || criteria
  NameCountBetweenDates: %w{},                   # !! No documentation provided
 # NameGetDetailForName: %w{},                   # !! No documentation provided, can't confirm it works
 # NameGetDetailForNameBankID: %w{},             # !! No documentation provided, can't confirm it works
  NameList: %w{startrow batchsize stardate enddate},   # part of V1 !! may be problems
  NameListBetweenDates: %w{}, # !! No documentation provided 
  NameSearch: %w{name},                                # part of V1 !! may be problems
  PartSearch: %w{title containerTitle author date volume series issue}, 
  SubjectSearch: %w{subject}, 
  TitleSearchSimple: %w{title}, 
}
REQUIRED_PARAMS =
{
  'name' => [:AuthorSearch, :NameSearch],
  'creatorid' => [:GetAuthorParts, :GetAuthorTitles],
  'type' => [:GetItemByIdentifier, :GetPartByIdentifier, :GetTitleByIdentifier],
  'value' => [:GetItemByIdentifier, :GetPartByIdentifier, :GetTitleByIdentifier],
  'itemid' => [:GetItemMetadata, :GetItemPages, :GetItemParts],
  'pageid' => [:GetPageMetadata, :GetPageNames, :GetPageOcrText, :GetPartBibTeX],
  'partid'  => [:GetPartBibTeX, :GetPartEndNote, :GetPartMetadata, :GetPartNames],
  'subject' => [:GetSubjectParts, :GetSubjectTitles, :SubjectSearch],
  'titleid' => [:GetTitleBibTex, :GetTitleEndNote, :GetTitleItems, :GetTitleMetadata],
  'title' => [:TitleSearchSimple] 
}
METHODS_REQUIRED_PARAMS =
mrp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rubyBHL/request.rb', line 89

def initialize(options = {})
  opts = {
    api_key: RubyBHL::API_KEY,
    format:  RubyBHL::Request::FORMAT,
    method:  :NameSearch,
    params: { }
  }.merge!(options)

  assign_options(opts)
  build_url if valid?
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



86
87
88
# File 'lib/rubyBHL/request.rb', line 86

def api_key
  @api_key
end

#formatObject

Returns the value of attribute format.



86
87
88
# File 'lib/rubyBHL/request.rb', line 86

def format
  @format
end

#methodObject

Returns the value of attribute method.



86
87
88
# File 'lib/rubyBHL/request.rb', line 86

def method
  @method
end

#paramsObject

Returns the value of attribute params.



86
87
88
# File 'lib/rubyBHL/request.rb', line 86

def params
  @params
end

#search_urlObject (readonly)

Returns the value of attribute search_url.



87
88
89
# File 'lib/rubyBHL/request.rb', line 87

def search_url
  @search_url
end

Instance Method Details

#assign_options(opts) ⇒ Object



101
102
103
104
105
106
# File 'lib/rubyBHL/request.rb', line 101

def assign_options(opts)
  @api_key = opts[:api_key]
  @method = opts[:method]
  @format = opts[:format]
  @params = opts[:params]
end

#has_required_params?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
# File 'lib/rubyBHL/request.rb', line 128

def has_required_params?
  return false if @method.nil?
  return true if METHODS_REQUIRED_PARAMS[@method].nil?
  METHODS_REQUIRED_PARAMS[@method].select{|v| !@params.keys.include?(v)} == []
end

#params_are_supported?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
# File 'lib/rubyBHL/request.rb', line 122

def params_are_supported? 
  return false if @method.nil?
  return true if METHODS[@method] == []
  @params.keys - METHODS[@method] == []
end

#responseObject



113
114
115
116
117
118
119
120
# File 'lib/rubyBHL/request.rb', line 113

def response
  build_url
  if valid?
    Response.new(request: self)
  else
    false # raise?
  end
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:

  • (API_KEY_MESSAGE)


134
135
136
137
138
139
140
# File 'lib/rubyBHL/request.rb', line 134

def valid?
  raise API_KEY_MESSAGE if @api_key.nil?
  raise "Method #{@method} not recognized." if @method && !RubyBHL::Request::METHODS.keys.include?(@method)
  raise "Format #{@format} not recognized." if @format && !%w{json xml}.include?(@format)

  !@method.nil? && !@format.nil? && params_are_supported? && has_required_params?
end