class Twenty20::Client

Constants

BASE_URI

Public Instance Methods

get_challenges(&block) click to toggle source
# File lib/twenty20/client.rb, line 12
def get_challenges(&block)
  resource = build_route('/challenges/open-for-submissions')
  response_for(resource, :challenge, &block)
end

Private Instance Methods

build_route(uri, params = {}) click to toggle source
# File lib/twenty20/client.rb, line 43
def build_route(uri, params = {})
  BASE_URI + uri + '?' + params.to_query
end
response_for(resource, type, &block) click to toggle source
# File lib/twenty20/client.rb, line 19
def response_for(resource, type, &block)
  uri = URI(resource)
  response = Net::HTTP.get_response(uri)  #returns response as a string

  return 'Error' unless response.code == "200"

  collection = to_collection(response, type)

  return collection unless block_given?

  collection.each do |item|
    block.call(item)
  end
end
to_collection(response, collection_class) click to toggle source
# File lib/twenty20/client.rb, line 34
def to_collection(response, collection_class)
  collection_class = collection_class.to_s
  parsed_response = JSON.parse(response.body) #parses response as JSON - originally string

  parsed_response[collection_class.pluralize].map do |e|
    "Twenty20::#{collection_class.singularize.classify}".constantize.new(e)
  end
end