Class: DisqusApi::Api

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

Constant Summary collapse

DEFAULT_VERSION =
'3.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version = DEFAULT_VERSION, specifications = {}) ⇒ Api

Returns a new instance of Api.

Parameters:

  • version (String) (defaults to: DEFAULT_VERSION)
  • specifications (Hash) (defaults to: {})

    API specifications



8
9
10
11
12
13
14
15
16
17
# File 'lib/disqus_api/api.rb', line 8

def initialize(version = DEFAULT_VERSION, specifications = {})
  @version = version
  @endpoint = "https://disqus.com/api/#@version/".freeze
  @specifications = ActiveSupport::HashWithIndifferentAccess.new(specifications)

  @namespaces = ActiveSupport::HashWithIndifferentAccess.new
  @specifications.keys.each do |namespace|
    @namespaces[namespace] = Namespace.new(self, namespace)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

DisqusApi.v3.—->><<—–.details

Forwards calls to API declared in YAML



58
59
60
# File 'lib/disqus_api/api.rb', line 58

def method_missing(method_name, *args)
  namespaces[method_name] or raise(ArgumentError, "No such namespace #{method_name}")
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



4
5
6
# File 'lib/disqus_api/api.rb', line 4

def endpoint
  @endpoint
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



4
5
6
# File 'lib/disqus_api/api.rb', line 4

def namespaces
  @namespaces
end

#specificationsObject (readonly)

Returns the value of attribute specifications.



4
5
6
# File 'lib/disqus_api/api.rb', line 4

def specifications
  @specifications
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/disqus_api/api.rb', line 4

def version
  @version
end

Instance Method Details

#connectionFaraday::Connection

Returns:

  • (Faraday::Connection)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/disqus_api/api.rb', line 29

def connection
  Faraday.new(connection_options) do |builder|
    builder.use Faraday::Request::Multipart
    builder.use Faraday::Request::UrlEncoded
    builder.use Faraday::Response::ParseJson

    builder.params.merge!(DisqusApi.config.slice(:api_secret, :api_key, :access_token))

    builder.adapter(*DisqusApi.adapter)
  end
end

#connection_optionsHash

Returns:

  • (Hash)


20
21
22
23
24
25
26
# File 'lib/disqus_api/api.rb', line 20

def connection_options
  {
    headers: { 'Accept' => "application/json", 'User-Agent' => "DisqusAgent"},
    ssl: { verify: false },
    url: @endpoint
  }
end

#get(path, arguments = {}) ⇒ Object

Performs custom GET request

Parameters:

  • path (String)
  • arguments (Hash) (defaults to: {})


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

def get(path, arguments = {})
  perform_request { connection.get(path, arguments).body }
end

#post(path, arguments = {}) ⇒ Object

Performs custom POST request

Parameters:

  • path (String)
  • arguments (Hash) (defaults to: {})


51
52
53
# File 'lib/disqus_api/api.rb', line 51

def post(path, arguments = {})
  perform_request { connection.post(path, arguments).body }
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/disqus_api/api.rb', line 62

def respond_to?(method_name, include_private = false)
  namespaces[method_name] || super
end