Class: Hoccer::LinccerClient

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LinccerClient

Returns a new instance of LinccerClient.



25
26
27
28
29
# File 'lib/linccer_client.rb', line 25

def initialize options = {}
  @uuid = UUID.generate
  @host = options[:host] || "linccer.beta.hoccer.com"
  @port = options[:port] || 80
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/linccer_client.rb', line 133

def method_missing name, *args
  if %w(get post put delete).include? name.to_s
    request name, *args
  else
    super
  end
end

Instance Attribute Details

#uuidObject

Returns the value of attribute uuid.



18
19
20
# File 'lib/linccer_client.rb', line 18

def uuid
  @uuid
end

Class Method Details

.createObject



20
21
22
23
# File 'lib/linccer_client.rb', line 20

def self.create
  client = self.new
  client
end

Instance Method Details

#action_path(mode, options = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/linccer_client.rb', line 39

def action_path mode, options = {}
  path = "/v3/clients/#{@uuid}/action/#{mode}"
  path << '?' + options.to_url_params unless options.empty?

  path
end

#client_pathObject



31
32
33
# File 'lib/linccer_client.rb', line 31

def client_path
  "/v3/clients/#{@uuid}"
end

#delete_environmentObject



120
121
122
123
124
125
# File 'lib/linccer_client.rb', line 120

def delete_environment
  req = Net::HTTP::Delete.new(environment_path)
  Net::HTTP.start(@host, @port) do |http|
    response = http.request(req)
  end
end

#environment_pathObject



35
36
37
# File 'lib/linccer_client.rb', line 35

def environment_path
  "/v3/clients/#{@uuid}/environment"
end

#follow_redirectObject



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

def follow_redirect
  if @redirect_location
    url = URI.parse @redirect_location
    Net::HTTP.start(url.host, url.port) do |http|
      http.get( url.path )
    end
  end
end

#follow_redirect_threadedObject



113
114
115
116
117
118
# File 'lib/linccer_client.rb', line 113

def follow_redirect_threaded
  t = Thread.new do
    follow_redirect
  end
  t.value
end

#handle_response(response = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/linccer_client.rb', line 71

def handle_response response = nil
  if response.nil?
    raise Hoccer::NoServerResponse
  end

  case response.header.code

  when "200"
    return parse_json( response.body )
  when "201"
    return true
  when "204"
    return nil
  when "409"
    return nil
  when "412"
    raise Hoccer::InvalidEnvironment
  else
    raise(
      Hoccer::InvalidServerResponse,
      "#{response.header.code} + #{response.header.message} +\n\n #{ response.header.inspect}"
    )
  end
end

#parse_json(response_body) ⇒ Object



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

def parse_json response_body
  begin
    payload = JSON.parse( response_body )
  rescue JSON::ParserError => e
    raise Hoccer::InvalidJsonResponse, "Could not parse JSON"
  end
end

#peek(id = nil) ⇒ Object



67
68
69
# File 'lib/linccer_client.rb', line 67

def peek id = nil
  handle_response get(peek_path(id))
end

#peek_path(id = nil) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/linccer_client.rb', line 46

def peek_path id = nil
  path = "/v3/clients/#{uuid}/peek"
  if id
    path << '?' + 'group_id=' + id
  end
  
  path
end

#receive(mode, options = {}) ⇒ Object



63
64
65
# File 'lib/linccer_client.rb', line 63

def receive mode, options = {}
  handle_response get(action_path(mode, options))
end

#request(method, path, data = nil) ⇒ Object



127
128
129
130
131
# File 'lib/linccer_client.rb', line 127

def request method, path, data = nil
  Net::HTTP.start(@host, @port) do |http|
    http.send( "request_#{method}".to_sym, path, data )
  end
end

#share(mode, data) ⇒ Object



59
60
61
# File 'lib/linccer_client.rb', line 59

def share mode, data
  handle_response put(action_path(mode), data.to_json)
end

#update_environment(data) ⇒ Object



55
56
57
# File 'lib/linccer_client.rb', line 55

def update_environment data
  handle_response put(environment_path, data.to_json)
end