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
78
# File 'lib/mycrm/connectable.rb', line 72

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

#parse(body) ⇒ Object



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

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

#respond(response) ⇒ Object

Raises:



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

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



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

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