Class: Jsonorama::JsonClient

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

Defined Under Namespace

Classes: ClientError, ServerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ JsonClient

Returns a new instance of JsonClient.



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

def initialize host
  self.host = host
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



65
66
67
# File 'lib/jsonorama/json_client.rb', line 65

def host
  @host
end

Instance Method Details

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



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jsonorama/json_client.rb', line 82

def get path, options={}
  options = default_options.merge options
  
  begin
    uri = uri path
    http = Net::HTTP.new(uri.host, uri.port)
    http_request = Net::HTTP::Get.new(uri.request_uri)
    http_response = http.request(http_request)
    process_response http_response, options
  rescue Exception => e
    handle_exception e, options
  end        
end

#post(path, data, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jsonorama/json_client.rb', line 96

def post path, data, options={}
  options = default_options.merge options 
  
  begin
    uri = uri path
    http = Net::HTTP.new(uri.host, uri.port)
    http_request = Net::HTTP::Post.new(uri.request_uri)
    http_request.set_form_data :json => ActiveSupport::JSON.encode(data)
    http_response = http.request(http_request)
    process_response http_response, options
  rescue Exception => e
    handle_exception e, options
  end
end

#uri(path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/jsonorama/json_client.rb', line 71

def uri path
  
  extension = ''
  extension = '.json' if path.downcase.match(/\.json$/).blank?      
  
  uri = URI.join(
    self.host, 
    "#{path}#{extension}#{query_string}")
  uri
end