A simple Ruby client library for KOSapi RESTful service.
Add this line to your application's Gemfile:
gem 'kosapi_client', github: 'flexik/kosapi_client'
And then execute:
$ bundle
# Creates a new instance of client with OAuth2 credentials
client = KOSapiClient.new({client_id: OAUTH_CLIENT_ID, client_secret: OAUTH_SECRET})
# Retrieves first page of all course events
course_events_page = client.course_events
course_events_page.each { |event| do_stuff_with_event(event) }
# Fetches page of parallels according to API parameters
parallels_page = client.parallels.offset(0).limit(50).query('course.department' => '18*')
# Finds all parallels related to parallel with id = 42
client.parallels.find(42).related- Add concrete resource builder to
lib/kosapi_client/resource/. Use current resources as an inspiration. - Register resource in
lib/kosapi_client/api_client.rblike:
module KOSapiClient
class ApiClient
include ResourceMapper
# accessible resources definition
resource :courses
resource :course_events
resource :parallels
resource :exams
resource :semesters
resource :new_resource
attr_reader :http_client
...KOSapiClient can be created and configured in two ways.
The simple way is to call KOSapiClient.new, which returns ApiClient instance.
client = KOSapiClient.new({client_id: OAUTH_CLIENT_ID, client_secret: OAUTH_SECRET})
client.parallels.find(42)The other way is to use configure client using KOSapiClient.configure and setting options inside block.
In addition to returning the client instance from configure, the client is also stored in KOSapiClient singleton property and its methods can be accessed by calling them on KOSapiClient directly.
This approach is more suitable for configuring client inside an initializer.
KOSapiClient.configure do |c|
c.client_id = ENV['KOSAPI_OAUTH_CLIENT_ID']
c.client_secret = ENV['KOSAPI_OAUTH_CLIENT_SECRET']
end
KOSapiClient.parallels.find(42)- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request