Class: ConvertLoop::Client
- Inherits:
-
Object
- Object
- ConvertLoop::Client
- Defined in:
- lib/convertloop/client.rb
Instance Method Summary collapse
- #event_logs ⇒ Object
- #get_http(uri) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #parse_json_response(response) ⇒ Object
- #people ⇒ Object
- #post(resource, body) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 |
# File 'lib/convertloop/client.rb', line 9 def initialize(={}) @host = [:host] || "https://api.convertloop.co" @version = [:version] || "v1" @app_id = [:app_id] @api_key = [:api_key] raise ArgumentError, "Missing key ':app_id'" if @app_id.nil? || @app_id.empty? raise ArgumentError, "Missing key ':api_key'" if @api_key.nil? || @api_key.empty? end |
Instance Method Details
#event_logs ⇒ Object
24 25 26 |
# File 'lib/convertloop/client.rb', line 24 def event_logs ConvertLoop::EventLog.new(self) end |
#get_http(uri) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/convertloop/client.rb', line 47 def get_http(uri) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true end return http end |
#parse_json_response(response) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/convertloop/client.rb', line 56 def parse_json_response(response) case response when Net::HTTPSuccess JSON.parse(response.body) else response.error! end end |
#people ⇒ Object
19 20 21 22 |
# File 'lib/convertloop/client.rb', line 19 def people puts "Self: #{self}" ConvertLoop::Person.new(self) end |
#post(resource, body) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/convertloop/client.rb', line 28 def post(resource, body) uri = URI.parse("#{url}#{resource}") http = get_http(uri) request = Net::HTTP::Post.new(uri.path) request.basic_auth @app_id, @api_key request['Content-Type'] = 'application/json' request['Accept'] = 'application/json' request['X-API-Source'] = "ruby-#$convertloop_version" request.body = body.to_json response = http.request(request) parse_json_response(response) end |
#url ⇒ Object
43 44 45 |
# File 'lib/convertloop/client.rb', line 43 def url "#{@host}/#{@version}" end |