Class: BusinessInsightApiClient::Client

Inherits:
Object
  • Object
show all
Includes:
Api::Animals, Api::Applications, Api::Calendar, Api::Groups, Api::Installations
Defined in:
lib/business_insight_api_client/client.rb

Overview

The client that has to make requests to the Business Insight Api. This class includes all api endpoint methods. See section Instance Method Summary, for available methods.

Examples:

Call an API method

client = BusinessInsightApiClient::Client.new
access_token = client.authorization.access_token_from_hash(token: 'abc', refresh_token: 'efc', expires_in: 30020, expires_at: 23939192)
client.access_token= access_token
animals = client.animals

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api::Groups

#create_group, #group, #groups, #update_group

Methods included from Api::Calendar

#calendar_event, #calendar_events, #calendar_events_by_cycle, #calendar_events_current_cycle, #create_birth_event, #create_calving_event, #create_dryoff_event, #create_heat_event, #create_insemination_event, #create_keep_open_event, #create_pregnancy_check_event, #delete_calendar_event, #update_birth_event, #update_calving_event, #update_dryoff_event, #update_heat_event, #update_insemination_event, #update_keep_open_event, #update_pregnancy_check_event

Methods included from Api::Animals

#animal, #animal_with_life_number, #animal_with_number, #animal_with_responder, #animals, #create_animal, #delete_animal, #update_animal

Methods included from Api::Installations

#installations

Methods included from Api::Applications

#get_access_token, #get_access_tokens

Constructor Details

#initialize(options = {}) ⇒ Client

Initializes a new Client with options. If no options are given the default configuration options are used for the client, like configured in BusinessInsightApiClient.configure.

Examples:

BusinessInsightApiClient::Client.new api_url: 'some url', client_id: 'some id' , client_secret: 'some secret'

Parameters:

  • options (Hash) (defaults to: {})

    the options to configure the client.

Options Hash (options):

  • :api_url (String) — default: BusinessInsightApiClient.api_url

    the api url.

  • :authorization_url (String) — default: BusinessInsightApiClient.authorization_url

    the authorization url of the api.

  • :content_type (String) — default: BusinessInsightApiClient.default_content_type

    the content type used for sending and requesting.

  • :client_id (String) — default: BusinessInsightApiClient.client_id

    the client id uses in OAuth authorization.

  • :client_secret (String) — default: BusinessInsightApiClient.client_secret

    the client secret uses in OAuth authorization.



40
41
42
43
44
45
46
47
# File 'lib/business_insight_api_client/client.rb', line 40

def initialize(options = {})
  @options = options
  @options[:api_url]              ||= BusinessInsightApiClient.api_url
  @options[:authorization_url]    ||= BusinessInsightApiClient.authorization_url
  @options[:content_type]         ||= BusinessInsightApiClient.default_content_type
  @options[:client_id]            ||= BusinessInsightApiClient.client_id
  @options[:client_secret]        ||= BusinessInsightApiClient.client_secret
end

Instance Attribute Details

#optionsObject (readonly)

Lists all options that are set for the client.



27
28
29
# File 'lib/business_insight_api_client/client.rb', line 27

def options
  @options
end

Instance Method Details

#access_token=(token) ⇒ Object

Sets the access token to make requests with to the protected endpoints. Make sure the access token is not expired, or has enough time to make requests. Otherwise refresh the token with OAuth2::AccessToken#refresh!

Parameters:

  • token (OAuth2::AccessToken)

    sets a auth token to make requests with.

See Also:



65
66
67
68
# File 'lib/business_insight_api_client/client.rb', line 65

def access_token=(token)
  # TODO: fail when not access token class.
  authorization.current_token = token
end

#authorizationBusinessInsightApiClient::Helpers::Authorization

Creates new or returns an existing Authorization helper. The helper can be used to retrieve OAuth Tokens from the Authorization server. The helper passes default options as configured with the client.

Returns:



54
55
56
# File 'lib/business_insight_api_client/client.rb', line 54

def authorization
  @authorization ||= BusinessInsightApiClient::Helpers::Authorization.new(options)
end