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
58
# 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'],
      token: context['api_token'] }
  end
end

.create_connection(url, key = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fulcrum/client.rb', line 60

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

Instance Method Details

#audioObject



96
97
98
# File 'lib/fulcrum/client.rb', line 96

def audio
  @audio ||= Fulcrum::Audio.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



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

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

#choice_listsObject



76
77
78
# File 'lib/fulcrum/client.rb', line 76

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

#classification_setsObject



80
81
82
# File 'lib/fulcrum/client.rb', line 80

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

#formsObject



84
85
86
# File 'lib/fulcrum/client.rb', line 84

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

#layersObject



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

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

#membershipsObject



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

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

#photosObject



88
89
90
# File 'lib/fulcrum/client.rb', line 88

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

#projectsObject



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

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

#recordsObject



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

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

#signaturesObject



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

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

#videosObject



92
93
94
# File 'lib/fulcrum/client.rb', line 92

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

#webhooksObject



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

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