Class: Cocoafish::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoafish/client.rb

Constant Summary collapse

@@connection =
nil
@@debug =
false
@@realm =
"http://api.cocoafish.com"

Class Method Summary collapse

Class Method Details

.debugObject



26
27
28
# File 'lib/cocoafish/client.rb', line 26

def debug
  @@debug
end

.debug=(debug_flag) ⇒ Object



21
22
23
24
# File 'lib/cocoafish/client.rb', line 21

def debug=(debug_flag)
  @@debug = debug_flag
  @@connection.debug = @@debug  if @@connection
end

.delete(endpoint, data = nil) ⇒ Object

Raises:

  • (NoConnectionEstablished)


37
38
39
40
41
# File 'lib/cocoafish/client.rb', line 37

def delete(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  json_hash = @@connection.delete Cocoafish::Endpoint.url(@@realm, endpoint), data
  parse_response(json_hash)
end

.get(endpoint, data = nil) ⇒ Object

Raises:

  • (NoConnectionEstablished)


30
31
32
33
34
35
# File 'lib/cocoafish/client.rb', line 30

def get(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  json_hash = @@connection.get Cocoafish::Endpoint.url(@@realm, endpoint), data
  
  parse_response(json_hash)
end

.get_paginated_array(response) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cocoafish/client.rb', line 64

def get_paginated_array(response)
  
  # return nil for empty json
  if response.json == nil
    return nil
  end
  
  # make sure we have only 1 top-level object in the response (users, places, etc.)
  if response.response.instance_variables.count != 1
    return nil
  end
  
  # get the response array
  orig_array = response.response.instance_variable_get(response.response.instance_variables.first)

  # create the paginated object
  if response.meta.page && orig_array     
    WillPaginate::Collection.create(response.meta.page, response.meta.per_page, response.meta.total_results) do |pager|
      pager.replace(orig_array)
    end
  end
end

.parse_response(json_hash) ⇒ Object



60
61
62
# File 'lib/cocoafish/client.rb', line 60

def parse_response(json_hash)
  CocoafishObject.new(json_hash)
end

.post(endpoint, data = nil) ⇒ Object

Raises:

  • (NoConnectionEstablished)


43
44
45
46
47
# File 'lib/cocoafish/client.rb', line 43

def post(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  json_hash = @@connection.post Cocoafish::Endpoint.url(@@realm, endpoint), data
  parse_response(json_hash)
end

.put(endpoint, data = nil) ⇒ Object

Raises:

  • (NoConnectionEstablished)


49
50
51
52
53
54
55
56
57
58
# File 'lib/cocoafish/client.rb', line 49

def put(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  json_hash = @@connection.put Cocoafish::Endpoint.url(@@realm, endpoint), data
  begin
    parse_response(json_hash)
  rescue
    # binary data
    json_hash
  end
end

.set_credentials(token, secret, options = {}) ⇒ Object



11
12
13
14
15
# File 'lib/cocoafish/client.rb', line 11

def set_credentials(token, secret, options = {})
  @@realm = options[:hostname] || @@realm
  @@connection = Connection.new(token, secret, options)
  @@connection.debug = @@debug
end

.set_session_id(session_id = nil) ⇒ Object



17
18
19
# File 'lib/cocoafish/client.rb', line 17

def set_session_id(session_id = nil)
  @@connection.session_id = session_id
end