Class: RubyBHL::Request
- Inherits:
-
Object
- Object
- RubyBHL::Request
- 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
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#format ⇒ Object
Returns the value of attribute format.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#search_url ⇒ Object
readonly
Returns the value of attribute search_url.
Instance Method Summary collapse
- #assign_options(opts) ⇒ Object
- #has_required_params? ⇒ Boolean
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
- #params_are_supported? ⇒ Boolean
- #response ⇒ Object
- #valid? ⇒ Boolean
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( = {}) opts = { api_key: RubyBHL::API_KEY, format: RubyBHL::Request::FORMAT, method: :NameSearch, params: { } }.merge!() (opts) build_url if valid? end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
86 87 88 |
# File 'lib/rubyBHL/request.rb', line 86 def api_key @api_key end |
#format ⇒ Object
Returns the value of attribute format.
86 87 88 |
# File 'lib/rubyBHL/request.rb', line 86 def format @format end |
#method ⇒ Object
Returns the value of attribute method.
86 87 88 |
# File 'lib/rubyBHL/request.rb', line 86 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
86 87 88 |
# File 'lib/rubyBHL/request.rb', line 86 def params @params end |
#search_url ⇒ Object (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 (opts) @api_key = opts[:api_key] @method = opts[:method] @format = opts[:format] @params = opts[:params] end |
#has_required_params? ⇒ 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
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 |
#response ⇒ Object
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
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 |