class YTLabsApi::Client
Constants
- END_POINTS
- URL
- VALID_CONTENT_TYPES
Attributes
base_url[R]
content_type[R]
errors[R]
token[R]
Public Class Methods
new(token, content_type=nil)
click to toggle source
# File lib/ytlabs_api/client.rb, line 35 def initialize(token, content_type=nil) @token = token @content_type = "application/#{content_type.to_s}" || "application/json" @base_url = URL @errors = [] raise InvalidAccessToken unless valid_token?(token) raise InvalidContentType unless valid_content_type?(content_type) end
Public Instance Methods
method_missing(method, *args, &block)
click to toggle source
Since the only public methods this client supports are calls to the specified endpoints, which are implemented - we will raise a more specific error here.
# File lib/ytlabs_api/client.rb, line 66 def method_missing(method, *args, &block) raise EndPointNotSupported end
Private Instance Methods
build_url(action, identifier=nil)
click to toggle source
Builds endpoint URL
dynamically.
# File lib/ytlabs_api/client.rb, line 95 def build_url(action, identifier=nil) end_point = END_POINTS[action] if identifier url = "#{base_url}/#{end_point}/#{identifier}" else url = "#{base_url}/#{end_point}" end url end
camelize_params_keys!(params_hash)
click to toggle source
Accepts underscored variables/params & converts them to camelCase as required by the YTLabs API. The intention is to 'Rubify' the wrapper allowing underscore_style_variable_naming. Example: camelize_params_keys!({:room_code=>“123”, :search_start_date=>“456”})
> {“roomCode”=>“123”, “searchStartDate”=>“456”}¶ ↑
# File lib/ytlabs_api/client.rb, line 83 def camelize_params_keys!(params_hash) return unless params_hash params_hash.keys.each do |key| value = params_hash.delete(key) new_key = key.to_s.camelize(:lower) params_hash[new_key] = value end params_hash end
parse_successful_response(response)
click to toggle source
# File lib/ytlabs_api/client.rb, line 117 def parse_successful_response(response) JSON.parse(response.body) end
prepare_response(response)
click to toggle source
The responses from the API are somewhat misleading. Calls that one might expect to 404 seem to return 403 Forbidden with “message”=>“Authorization header requires 'Credential' parameter. Consider how to provide more helpful messages.
# File lib/ytlabs_api/client.rb, line 108 def prepare_response(response) if response.parsed_response["status"] == (200..206) parse_successful_response(response) else # Temporary JSON.parse(response.body) end end
transform_params!(params)
click to toggle source
Transforms {:a => 2, :b => 2} to “a=2&b=2”
# File lib/ytlabs_api/client.rb, line 73 def transform_params!(params) URI.encode_www_form(params) if params end
valid_content_type?(content_type)
click to toggle source
# File lib/ytlabs_api/client.rb, line 125 def valid_content_type?(content_type) VALID_CONTENT_TYPES.include?(content_type) end
valid_token?(token)
click to toggle source
# File lib/ytlabs_api/client.rb, line 121 def valid_token?(token) token.size == 36 end