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.



17
18
19
20
# File 'lib/fulcrum/client.rb', line 17

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.



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

def connection
  @connection
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

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



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

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



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

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



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

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



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

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



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

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

#audioObject



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

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

#audit_logsObject



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

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

#authorizationsObject



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

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

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



37
38
39
40
# File 'lib/fulcrum/client.rb', line 37

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

#changesetsObject



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

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

#choice_listsObject



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

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

#classification_setsObject



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

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

#formsObject



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

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

#layersObject



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

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

#membershipsObject



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

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

#photosObject



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

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

#projectsObject



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

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

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



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

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

#recordsObject



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

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

#resetObject



32
33
34
35
# File 'lib/fulcrum/client.rb', line 32

def reset
  @response = nil
  @connection = nil
end

#rolesObject



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

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

#signaturesObject



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

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

#videosObject



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

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

#webhooksObject



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

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