Class: Fulcrum::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fulcrum/client.rb

Constant Summary collapse

DEFAULT_URL =
'https://api.fulcrumapp.com/api/v2'
DEFAULT_USER_AGENT =
"Fulcrum Ruby API Client, Version #{Fulcrum::VERSION}"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, url = nil) ⇒ Client

Returns a new instance of Client.



15
16
17
18
# File 'lib/fulcrum/client.rb', line 15

def initialize(key = nil, url = nil)
  self.key = key
  self.url = url || DEFAULT_URL
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



10
11
12
# File 'lib/fulcrum/client.rb', line 10

def connection
  @connection
end

#keyObject

Returns the value of attribute key.



12
13
14
# File 'lib/fulcrum/client.rb', line 12

def key
  @key
end

#responseObject

Returns the value of attribute response.



11
12
13
# File 'lib/fulcrum/client.rb', line 11

def response
  @response
end

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/fulcrum/client.rb', line 13

def url
  @url
end

Class Method Details

.authenticate(username, password, url = DEFAULT_URL) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fulcrum/client.rb', line 44

def self.authenticate(username, password, url=DEFAULT_URL)
  connection = create_connection(url)

  connection.basic_auth(username, password)

  resp = connection.get('users.json')

  resp.body['user']['contexts'].map do |context|
    { id: context['id'],
      name: context['name'] }
  end
end

.create_authorization(username, password, organization_id, note, timeout = nil, user_id = nil, url = DEFAULT_URL) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fulcrum/client.rb', line 70

def self.create_authorization(username, password, organization_id, note, timeout = nil, user_id = nil, url=DEFAULT_URL)
  connection = create_connection(url)

  connection.basic_auth(username, password)

  body = { authorization: { organization_id: organization_id,
                            note: note, timeout: timeout, user_id: user_id } }
  resp = connection.post('authorizations.json', body)

  resp.body['authorization']
end

.create_connection(url, key = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fulcrum/client.rb', line 82

def self.create_connection(url, key = nil)
  Faraday.new(url) do |connection|
    connection.request :multipart
    connection.request :json

    connection.response :raise_error
    connection.response :json, content_type: 'application/json'

    connection.adapter  Faraday.default_adapter

    connection.headers['X-ApiToken'] = key if key
    connection.headers['X-Require-Media'] = 'false'
    connection.headers['User-Agent'] = DEFAULT_USER_AGENT
  end
end

.get_user(username, password, url = DEFAULT_URL) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fulcrum/client.rb', line 57

def self.get_user(username, password, url=DEFAULT_URL)
  connection = create_connection(url)

  connection.basic_auth(username, password)

  resp = connection.get('users.json')

  user = resp.body['user']
  user['contexts'] = user['contexts'].map{|c| c.except!('api_token')}

  user
end

Instance Method Details

#attachmentsObject



122
123
124
# File 'lib/fulcrum/client.rb', line 122

def attachments
  @attachment ||= Fulcrum::Attachment.new(self)
end

#audioObject



118
119
120
# File 'lib/fulcrum/client.rb', line 118

def audio
  @audio ||= Fulcrum::Audio.new(self)
end

#audit_logsObject



158
159
160
# File 'lib/fulcrum/client.rb', line 158

def audit_logs
  @audit_logs ||= Fulcrum::AuditLog.new(self)
end

#authorizationsObject



162
163
164
# File 'lib/fulcrum/client.rb', line 162

def authorizations
  @authorizations ||= Fulcrum::Authorization.new(self)
end

#call(method = :get, path = '', params = {}) ⇒ Object



35
36
37
38
# File 'lib/fulcrum/client.rb', line 35

def call(method = :get, path = '', params = {})
  @response = connection.send(method.to_sym, path, params.with_indifferent_access)
  @response.body
end

#changesetsObject



146
147
148
# File 'lib/fulcrum/client.rb', line 146

def changesets
  @changesets ||= Fulcrum::Changeset.new(self)
end

#choice_listsObject



98
99
100
# File 'lib/fulcrum/client.rb', line 98

def choice_lists
  @choice_lists ||= Fulcrum::ChoiceList.new(self)
end

#classification_setsObject



102
103
104
# File 'lib/fulcrum/client.rb', line 102

def classification_sets
  @classification_sets ||= Fulcrum::ClassificationSet.new(self)
end

#formsObject



106
107
108
# File 'lib/fulcrum/client.rb', line 106

def forms
  @forms ||= Fulcrum::Form.new(self)
end

#layersObject



142
143
144
# File 'lib/fulcrum/client.rb', line 142

def layers
  @layers ||= Fulcrum::Layer.new(self)
end

#membershipsObject



138
139
140
# File 'lib/fulcrum/client.rb', line 138

def memberships
  @memberships ||= Fulcrum::Membership.new(self)
end

#photosObject



110
111
112
# File 'lib/fulcrum/client.rb', line 110

def photos
  @photos ||= Fulcrum::Photo.new(self)
end

#projectsObject



130
131
132
# File 'lib/fulcrum/client.rb', line 130

def projects
  @projects ||= Fulcrum::Project.new(self)
end

#query(sql, format = 'json') ⇒ Object



166
167
168
169
170
# File 'lib/fulcrum/client.rb', line 166

def query(sql, format = 'json')
  body = { q: sql,
           format: format }
  call(:post, 'query', body)
end

#recordsObject



134
135
136
# File 'lib/fulcrum/client.rb', line 134

def records
  @records ||= Fulcrum::Record.new(self)
end

#resetObject



30
31
32
33
# File 'lib/fulcrum/client.rb', line 30

def reset
  @response = nil
  @connection = nil
end

#rolesObject



154
155
156
# File 'lib/fulcrum/client.rb', line 154

def roles
  @roles ||= Fulcrum::Role.new(self)
end

#signaturesObject



126
127
128
# File 'lib/fulcrum/client.rb', line 126

def signatures
  @signatures ||= Fulcrum::Signature.new(self)
end

#videosObject



114
115
116
# File 'lib/fulcrum/client.rb', line 114

def videos
  @videos ||= Fulcrum::Video.new(self)
end

#webhooksObject



150
151
152
# File 'lib/fulcrum/client.rb', line 150

def webhooks
  @webhooks ||= Fulcrum::Webhook.new(self)
end