Class: YANAPI::API
- Inherits:
-
Object
- Object
- YANAPI::API
- Defined in:
- lib/yanapi/api.rb
Constant Summary collapse
- REQUIRED_PARAMS =
[:method, :query_params]
- ACCEPTED_METHODS =
['questionSearch', 'getByUser', 'getByCategory', 'getQuestion' ]
Instance Method Summary collapse
- #check_params(params) ⇒ Object private
-
#get ⇒ Object
This is a connector to the specific method in the query.
-
#initialize(params) ⇒ API
constructor
A new instance of API.
- #version ⇒ Object
Constructor Details
#initialize(params) ⇒ API
Returns a new instance of API.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/yanapi/api.rb', line 21 def initialize(params) @params = check_params(params) @query = case @params[:method] when 'questionSearch' TermQuery.new(@params) when 'getByCategory' CategoryQuery.new(@params) when 'getByUser' UserQuery.new(@params) when 'getQuestion' QuestionQuery.new(@params) end end |
Instance Method Details
#check_params(params) ⇒ Object (private)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/yanapi/api.rb', line 47 def check_params(params) # It should be an instance on non empty hash. unless params.instance_of?(Hash) fail(UserError, "Params should be a Hash, not #{params.class}!") end if params.empty? fail(UserError, 'The params hash is empty!') end # It should contain required keys and be two dimensional. missed_keys = [] REQUIRED_PARAMS.each do |key| unless params.has_key?(key) missed_keys << key end end unless missed_keys.empty? fail(UserError, "You should provide: <:#{missed_keys.join('>, <:')}>!") end unless params[:query_params].instance_of?(Hash) key = params[:query_params] fail(UserError, "<:query_params> should be a Hash, not #{key.class}!") end if params[:query_params].empty? fail(UserError, '<:query_params> is empty!') end # It should accept only allowed methods. unless ACCEPTED_METHODS.include?(params[:method]) fail(UserError, "<#{params[:method]}> is not a valid search method!") end # It should warn about superfluous keys. superfluous_keys = [] params.each_key do |key| unless REQUIRED_PARAMS.include?(key) superfluous_keys << key end end unless superfluous_keys.empty? warn "Keys ignored: <:#{superfluous_keys.join('>, <:')}>!" end params end |
#get ⇒ Object
This is a connector to the specific method in the query.
38 39 40 |
# File 'lib/yanapi/api.rb', line 38 def get @query.get end |
#version ⇒ Object
42 43 44 |
# File 'lib/yanapi/api.rb', line 42 def version VERSION end |