Class: ContextIO::API

Inherits:
Object
  • Object
show all
Defined in:
lib/contextio/api.rb,
lib/contextio/api/resource.rb,
lib/contextio/api/url_builder.rb,
lib/contextio/api/association_helpers.rb,
lib/contextio/api/resource_collection.rb

Overview

For internal use only. Users of this gem should not be using this directly. Represents the handle on the Context.IO API. It handles the user's OAuth credentials with Context.IO and signing requests, etc.

Defined Under Namespace

Modules: AssociationHelpers, Resource, ResourceCollection Classes: Error, URLBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of API.

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



69
70
71
72
73
74
75
# File 'lib/contextio/api.rb', line 69

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.



56
57
58
# File 'lib/contextio/api.rb', line 56

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.



64
65
66
# File 'lib/contextio/api.rb', line 64

def key
  @key
end

#optsObject (readonly)

Returns the value of attribute opts.



64
# File 'lib/contextio/api.rb', line 64

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.



64
# File 'lib/contextio/api.rb', line 64

attr_reader :key, :secret, :opts

#versionObject

Returns the value of attribute version.



56
57
58
# File 'lib/contextio/api.rb', line 56

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.



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

def self.base_url
  BASE_URL
end

.url_for(resource) ⇒ String

Returns The URL for the resource in the API.

Parameters:

  • resource (Object)

    The resource you want the URL for.

Returns:

  • (String)

    The URL for the resource in the API.



37
38
39
# File 'lib/contextio/api.rb', line 37

def self.url_for(resource)
  ContextIO::API::URLBuilder.url_for(resource)
end

.user_agent_stringObject



48
49
50
# File 'lib/contextio/api.rb', line 48

def self.user_agent_string
  "contextio-ruby-#{ContextIO.version}"
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.



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

def self.version
  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.



83
84
85
# File 'lib/contextio/api.rb', line 83

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

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



104
105
106
107
108
109
110
# File 'lib/contextio/api.rb', line 104

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.



96
97
98
99
100
101
102
# File 'lib/contextio/api.rb', line 96

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

#url_for(resource) ⇒ String

Returns The URL for the resource in the API.

Parameters:

  • resource (Object)

    The resource you want the URL for.

Returns:

  • (String)

    The URL for the resource in the API.



44
45
46
# File 'lib/contextio/api.rb', line 44

def url_for(resource)
  ContextIO::API.url_for(resource)
end

#user_agent_stringObject



52
53
54
# File 'lib/contextio/api.rb', line 52

def user_agent_string
  self.class.user_agent_string
end