Class: Campi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/campi/client.rb

Constant Summary collapse

API_ARGUMENTS =

Valid arguments to build the API arguments path.

[:projects, :calendars]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/campi/client.rb', line 12

def initialize
  self.class.base_uri "https://basecamp.com/#{Campi.configuration.basecamp_id}/api/v1"

  self.class.headers(
      'User-Agent' => "#{Campi.configuration.application_name} (#{Campi.configuration.application_contact})",
      'Content-Type' => 'application/json')

  self.class.basic_auth(Campi.configuration.username, Campi.configuration.password)

  @data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



32
33
34
35
36
37
# File 'lib/campi/client.rb', line 32

def method_missing(method_name, *arguments, &block)
  if API_ARGUMENTS.include?(method_name)
    @data.merge!({method_name => arguments[0]})
    self
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/campi/client.rb', line 5

def data
  @data
end

Instance Method Details

#api_argumentsObject

Use merged data to compose the API arguments path.



42
43
44
45
46
47
48
# File 'lib/campi/client.rb', line 42

def api_arguments
  arguments = ''
  @data.each do |sym, value|
    arguments += "/#{sym.to_s}/#{value}"
  end
  arguments
end

#get_accessesObject

client = Campi::Client.new client.project(‘999999999-my-project’).get_accesses



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

def get_accesses
  path = "#{api_arguments}/accesses.json"
  get(path)
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/campi/client.rb', line 24

def respond_to?(method_name)
  API_ARGUMENTS.include?(method_name) || super
end

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/campi/client.rb', line 28

def respond_to_missing?(method_name, include_private = false)
  API_ARGUMENTS.include?(method_name) || super
end