Class: ContextIO::API::AbstractAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/contextio/api/abstract_api.rb

Direct Known Subclasses

Lite::API

Constant Summary collapse

BASE_URL =
'https://api.context.io'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, opts = {}) ⇒ AbstractAPI

Returns a new instance of AbstractAPI.

Parameters:

  • key (String)

    The user’s OAuth key for their Context.IO account.

  • secret (String)

    The user’s OAuth secret for their Context.IO account.

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

    Optional options for OAuth connections. ie. :timeout and :open_timeout are supported



46
47
48
49
50
51
52
# File 'lib/contextio/api/abstract_api.rb', line 46

def initialize(key, secret, opts={})
	@key = key
	@secret = secret
	@opts = opts || {}
	@base_url = self.class.base_url
	@version = self.class.version
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



33
34
35
# File 'lib/contextio/api/abstract_api.rb', line 33

def base_url
  @base_url
end

#keyString (readonly)

Returns The OAuth key for the user’s Context.IO account.

Returns:

  • (String)

    The OAuth key for the user’s Context.IO account.



41
42
43
# File 'lib/contextio/api/abstract_api.rb', line 41

def key
  @key
end

#optsObject (readonly)

Returns the value of attribute opts.



41
# File 'lib/contextio/api/abstract_api.rb', line 41

attr_reader :key, :secret, :opts

#secretString (readonly)

Returns The OAuth secret for the user’s Context.IO account.

Returns:

  • (String)

    The OAuth secret for the user’s Context.IO account.



41
# File 'lib/contextio/api/abstract_api.rb', line 41

attr_reader :key, :secret, :opts

#versionObject

Returns the value of attribute version.



33
34
35
# File 'lib/contextio/api/abstract_api.rb', line 33

def version
  @version
end

Class Method Details

.base_urlString

Returns The base URL the API is served from.

Returns:

  • (String)

    The base URL the API is served from.



21
22
23
# File 'lib/contextio/api/abstract_api.rb', line 21

def self.base_url
	BASE_URL
end

.user_agent_stringObject

Raises:



25
26
27
# File 'lib/contextio/api/abstract_api.rb', line 25

def self.user_agent_string
	raise NotDefinedError, 'user_agent_string undefined in your API subclassed model.'
end

.versionString

Returns The version of the Context.IO API this version of the gem is intended for use with.

Returns:

  • (String)

    The version of the Context.IO API this version of the gem is intended for use with.

Raises:



15
16
17
18
# File 'lib/contextio/api/abstract_api.rb', line 15

def self.version
	raise NotDefinedError, 'VERSION is not defined in your API subclassed model.' if self::VERSION.nil?
	self::VERSION
end

Instance Method Details

#path(resource_path, params = {}) ⇒ Object

Generates the path for a resource_path and params hash for use with the API.

Parameters:

  • resource_path (String)

    The resource_path or full resource URL for the resource being acted on.

  • params ({String, Symbol => String, Symbol, Array<String, Symbol>}) (defaults to: {})

    A Hash of the query parameters for the action represented by this path.



60
61
62
# File 'lib/contextio/api/abstract_api.rb', line 60

def path(resource_path, params = {})
	"/#{version}/#{strip_resource_path(resource_path)}#{self.class.hash_to_url_params(params)}"
end

#raw_request(method, resource_path, params = {}) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/contextio/api/abstract_api.rb', line 81

def raw_request(method, resource_path, params={})
	response = oauth_request(method, resource_path, params)

	with_error_handling(response) do |response|
		response.body
	end
end

#request(method, resource_path, params = {}) ⇒ Object

Makes a request against the Context.IO API.

Parameters:

  • method (String, Symbol)

    The HTTP verb for the request (lower case).

  • resource_path (String)

    The path to the resource in question.

  • params ({String, Symbol => String, Symbol, Array<String, Symbol>}) (defaults to: {})

    A Hash of the query parameters for the action represented by this request.

Raises:

  • (API::Error)

    if the response code isn’t in the 200 or 300 range.



73
74
75
76
77
78
79
# File 'lib/contextio/api/abstract_api.rb', line 73

def request(method, resource_path, params = {})
	response = oauth_request(method, resource_path, params, { 'Accept' => 'application/json' })

	with_error_handling(response) do |response|
		parse_json(response.body)
	end
end

#user_agent_stringObject



29
30
31
# File 'lib/contextio/api/abstract_api.rb', line 29

def user_agent_string
	self.class.user_agent_string
end