Class: Quaderno::Base

Inherits:
OpenStruct
  • Object
show all
Includes:
HTTParty, Quaderno::Behavior::Crud, Exceptions, Helpers::Authentication, Helpers::RateLimit
Defined in:
lib/quaderno-ruby/base.rb

Constant Summary collapse

PRODUCTION_URL =
'https://quadernoapp.com/api/'
SANDBOX_URL =
'http://sandbox-quadernoapp.com/api/'
@@auth_token =
nil
@@rate_limit_info =
nil
@@api_version =
nil
@@url =
PRODUCTION_URL
@@user_agent_suffix =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::RateLimit

#rate_limit_info, #rate_limit_info=

Methods included from Helpers::Authentication

#get_authentication

Methods included from Quaderno::Behavior::Crud

included

Methods included from Exceptions

included

Class Method Details

.api_model(klass) ⇒ Object

Class methods



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/quaderno-ruby/base.rb', line 21

def self.api_model(klass)
  instance_eval <<-END
    def api_model
      #{klass}
    end
  END
  class_eval <<-END
    def api_model
      #{klass}
    end
  END
end

.api_version=(api_version) ⇒ Object



38
39
40
# File 'lib/quaderno-ruby/base.rb', line 38

def self.api_version=(api_version)
  @@api_version = api_version
end

.auth_token=(auth_token) ⇒ Object



42
43
44
# File 'lib/quaderno-ruby/base.rb', line 42

def self.auth_token=(auth_token)
  @@auth_token = auth_token
end

.authorization(auth_token, mode = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/quaderno-ruby/base.rb', line 54

def self.authorization(auth_token, mode = nil)
  mode ||= :production
  url = mode == :sandbox ? SANDBOX_URL : PRODUCTION_URL
  response = get("#{url}authorization.json", basic_auth: { username: auth_token }, headers: default_headers)

  if response.code == 200
    data = self.new(response.parsed_response)
    data.rate_limit_info = response

    data
  elsif response.response.is_a?(Net::HTTPServerError)
    raise_exception(Quaderno::Exceptions::ServerError, 'Server error', response)
  else
    raise_exception(Quaderno::Exceptions::InvalidSubdomainOrToken, 'Invalid subdomain or token', response)
  end
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



34
35
36
# File 'lib/quaderno-ruby/base.rb', line 34

def self.configure
  yield self
end

.me(options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/quaderno-ruby/base.rb', line 98

def self.me(options = {})
  options[:auth_token] ||= auth_token
  options[:api_url] ||= url

  authentication = get_authentication(options)

  party_response = get("#{authentication[:url]}me.json",
    basic_auth: authentication[:basic_auth],
    headers: default_headers.merge(authentication[:headers])
  )

  check_exception_for(party_response, { subdomain_or_token: true })

  data = self.new(party_response.parsed_response)
  data.rate_limit_info = party_response

  data
end

.ping(options = {}) ⇒ Object Also known as: rate_limit_info

Check the connection



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

def self.ping(options = {})
  begin
    options[:auth_token] ||= auth_token
    options[:api_url] ||= url

    authentication = get_authentication(options)

    party_response = get("#{authentication[:url]}ping.json",
      basic_auth: authentication[:basic_auth],
      headers: default_headers.merge(authentication[:headers])
    )

    check_exception_for(party_response, { subdomain_or_token: true })
  rescue Errno::ECONNREFUSED
    return Quaderno::Base.new({ status: false })
  end

  data = self.new({ status: true })
  data.rate_limit_info = party_response

  data
end

.url=(url) ⇒ Object



46
47
48
# File 'lib/quaderno-ruby/base.rb', line 46

def self.url=(url)
  @@url = url
end

.user_agent_header=(custom_user_agent) ⇒ Object



50
51
52
# File 'lib/quaderno-ruby/base.rb', line 50

def self.user_agent_header=(custom_user_agent)
  @@user_agent_suffix = custom_user_agent
end

Instance Method Details

#to_hashObject

Instance methods



118
119
120
# File 'lib/quaderno-ruby/base.rb', line 118

def to_hash
  self.marshal_dump
end