Module: Mycrm::Connectable::ExtendedMethods

Defined in:
lib/mycrm/connectable.rb

Overview

private module to be extended

Instance Method Summary collapse

Instance Method Details

#connectionObject



72
73
74
75
76
77
# File 'lib/mycrm/connectable.rb', line 72

def connection
  Mycrm::Connectable.connection do |faraday|
    faraday.headers['Authorization'] = "Bearer #{skim(Mycrm::Connectable.token)}"
    faraday.headers['X-MyCrm-ApiKey'] = Mycrm.configuration.api_key.to_s
  end
end

#parse(body) ⇒ Object



85
86
87
88
89
# File 'lib/mycrm/connectable.rb', line 85

def parse(body)
  JSON.parse(body)
rescue JSON::ParserError
  raise ResponseError, "#{skim(body)} is not in a valid format"
end

#respond(response) ⇒ Object

Raises:



79
80
81
82
83
# File 'lib/mycrm/connectable.rb', line 79

def respond(response)
  raise ResponseError, skim(response.body) unless response.success?
  raise ResponseError, 'Not found' if 'null'.eql?(skim(response.body))
  parse(response.body)
end

#skim(string) ⇒ Object



91
92
93
# File 'lib/mycrm/connectable.rb', line 91

def skim(string)
  string.to_s.gsub(/\A"|"\Z/, '')
end