fulcrum 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3@fulcrum_api
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fulcrum.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Chris Hall
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # Fulcrum
2
+
3
+ Fulcrum API Gem
4
+
5
+ ## Requirements
6
+
7
+ ruby 1.9.x
8
+ a [fulcrum user account](http://web.fulcrumapp.com)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'fulcrum'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install fulcrum
23
+
24
+ ## Configuration
25
+
26
+ Fulcrum::Api.configure do |config|
27
+ config.uri = 'http://web.fulcrumapp.com/api/v2'
28
+ config.key = 'your_api_key'
29
+ end
30
+
31
+ ## Forms
32
+
33
+ Fulcrum::Form.all(opts)
34
+ # opts = { 'page' => page_number, 'schema' => true_or_false }
35
+
36
+ Fulcrum::Form.find(id, opts)
37
+ # opts = { 'include_foreign_elements' => true_or_false }
38
+
39
+ Fulcrum::Form.create(form)
40
+ # form = { 'form' => { ... } }
41
+
42
+ Fulcrum::Form.update(id, form)
43
+ # form = { 'form' => { ... } }
44
+
45
+ Fulcrum::Form.delete(id)
46
+
47
+ ## Records
48
+
49
+ Fulcrum::Record.all(opts)
50
+ # opts = { 'page' => page_number,
51
+ # 'form_id' => form_id,
52
+ # 'bounding_box' => 'lat_bottom,lng_left,lat_top,lng_right',
53
+ # 'updated_since' => date_since_epoch_in_seconds }
54
+
55
+ Fulcrum::Record.find(id)
56
+ Fulcrum::Record.create(record)
57
+ # record = { 'record' => { ... } }
58
+
59
+ Fulcrum::Record.update(id, record)
60
+ # record = { 'record' => { ... } }
61
+
62
+ Fulcrum::Record.delete(id)
63
+
64
+ ## Photos
65
+
66
+ Fulcrum::Photo.find(access_key, opts)
67
+ # opts = { 'format' => 'json|image' }
68
+
69
+ Fulcrum::Photo.thumbnail(access_key, opts)
70
+ # opts = { 'format' => 'json|image' }
71
+
72
+ Fulcrum::Photo.create(photo, content_type, unique_id, label)
73
+
74
+ Fulcrum::Photo.delete(access_key)
75
+
76
+ ## Choice Lists
77
+
78
+ Fulcrum::ChoiceList.all(opts)
79
+ # opts = { 'page' => page_number }
80
+
81
+ Fulcrum::ChoiceList.find(id)
82
+
83
+ Fulcrum::ChoiceList.create(choice_list)
84
+ # choice_list = { 'choice_list' => { ... } }
85
+
86
+ Fulcrum::ChoiceList.update(id, choice_list)
87
+ # choice_list = { 'choice_list' => { ... } }
88
+
89
+ Fulcrum::ChoiceList.delete(id)
90
+
91
+ ## Classification Sets
92
+
93
+ Fulcrum::ClassificationSet.all(opts)
94
+ # opts = { 'page' => page_number }
95
+
96
+ Fulcrum::ClassificationSet.find(id)
97
+
98
+ Fulcrum::ClassificationSet.create(classification_set)
99
+ # classification_set = { 'classification_set' => { ... } }
100
+
101
+ Fulcrum::ClassificationSet.update(id, classification_set)
102
+ # classification_set = { 'classification_set' => { ... } }
103
+
104
+ Fulcrum::ClassificationSet.delete(id)
105
+
106
+ ## Members
107
+
108
+ Fulcrum::Member.all(opts)
109
+ # opts = { 'page' => page_number }
110
+
111
+ Fulcrum::Member.find(id)
112
+
113
+ ## Extra Reading
114
+
115
+ [Fulcrum API docs](http://developer.fulcrumapp.com/api/fulcrum-api.html)
116
+
117
+ ## Contributing
118
+
119
+ 1. Fork it
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
data/fulcrum.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/fulcrum/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Chris Hall"]
6
+ gem.email = ["chris@spatialnetworks.com"]
7
+ gem.description = %q{Fulcrum API Gem}
8
+ gem.summary = %q{Fulcrum API Gem}
9
+ gem.homepage = "http://docs.fulcrumapp.com/"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "fulcrum"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Fulcrum::VERSION
17
+
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency 'webmock'
21
+ gem.add_development_dependency 'debugger'
22
+
23
+ gem.add_dependency 'activesupport'
24
+ gem.add_dependency 'faraday'
25
+ gem.add_dependency 'hashie'
26
+ gem.add_dependency 'faraday_middleware'
27
+ end
data/lib/fulcrum.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "fulcrum/version"
2
+ require "fulcrum/api"
3
+ require "fulcrum/form"
4
+ require "fulcrum/record"
5
+ require "fulcrum/choice_list"
6
+ require "fulcrum/classification_set"
7
+ require "fulcrum/member"
8
+ require "fulcrum/photo"
9
+ require "fulcrum/validators/base_validator"
10
+ require "fulcrum/validators/form_validator"
11
+ require "fulcrum/validators/record_validator"
12
+ require "fulcrum/validators/choice_list_validator"
13
+ require "fulcrum/validators/classification_set_validator"
@@ -0,0 +1,95 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Fulcrum
5
+
6
+ class ApiError < Faraday::Error::ClientError; end
7
+ class ConnectionError < StandardError; end
8
+
9
+ class Api
10
+
11
+ VALID_METHODS = [:get, :post, :put, :delete]
12
+
13
+ attr :connection
14
+ attr :response
15
+ attr_writer :configuration
16
+
17
+ class << self
18
+
19
+ def key
20
+ @configuration.key
21
+ end
22
+
23
+ def uri
24
+ @configuration.uri
25
+ end
26
+
27
+ def response
28
+ @response
29
+ end
30
+
31
+ def parse_opts(keys = [], opts = {})
32
+ opts = opts.with_indifferent_access
33
+ {}.tap do |p|
34
+ keys.each { |key| p[key.to_sym] = opts[key] if opts.has_key?(key) }
35
+ end
36
+ end
37
+
38
+ def call(method = :get, path = '', params = {})
39
+ raise ArgumentError, "Invalid method: #{method.to_s}" unless VALID_METHODS.include?(method.to_sym)
40
+ @response = connection.send(method.to_sym, path, params)
41
+ @response.body
42
+ rescue Faraday::Error::ClientError => e
43
+ @response = e.response
44
+ { error: { status: @response[:status], message: @response[:body] } }
45
+ end
46
+
47
+ def configure
48
+ yield(configuration)
49
+ end
50
+
51
+ def configuration
52
+ @configuration ||= Configuration.new
53
+ end
54
+
55
+ def connection
56
+ if !@connection
57
+ @connection = Faraday.new(Fulcrum::Api.configuration.uri) do |b|
58
+ b.request :multipart
59
+ b.request :json
60
+ b.response :raise_error
61
+ b.response :json , :content_type => 'application/json'
62
+ b.adapter Faraday.default_adapter
63
+ end
64
+ @connection.headers['X-ApiToken'] = Fulcrum::Api.configuration.key
65
+ @connection.headers['User-Agent'] = "Ruby Fulcrum API Client, Version #{Fulcrum::VERSION}"
66
+ end
67
+ @connection
68
+ end
69
+
70
+ def get_key(username, password)
71
+ conn = Faraday.new(uri) do |b|
72
+ b.request :url_encoded
73
+ b.response :raise_error
74
+ b.response :json, :content_type => "application/json"
75
+ b.adapter Faraday.default_adapter
76
+ end
77
+
78
+ conn.headers['User-Agent'] = "Ruby Fulcrum API Client version #{Fulcrum::VERSION}"
79
+ conn.basic_auth(username, password)
80
+ resp = conn.get('users.json')
81
+ body = JSON.parse(resp.body)
82
+ if body['user']
83
+ body['user']['api_token']
84
+ else
85
+ nil
86
+ end
87
+ end
88
+ end
89
+
90
+ class Configuration
91
+ attr_accessor :uri
92
+ attr_accessor :key
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,38 @@
1
+ module Fulcrum
2
+ class ChoiceList < Api
3
+
4
+ class << self
5
+
6
+ def all(opts = {})
7
+ params = parse_opts([:page], opts)
8
+ call(:get, 'choice_lists.json', params)
9
+ end
10
+
11
+ def find(id)
12
+ call(:get, "choice_lists/#{id}.json")
13
+ end
14
+
15
+ def create(choice_list)
16
+ validation = ChoiceListValidator.new(choice_list)
17
+ if validation.valid?
18
+ call(:post, 'choice_lists.json', choice_list)
19
+ else
20
+ { error: { validation: validation.errors } }
21
+ end
22
+ end
23
+
24
+ def update(id, choice_list)
25
+ validation = ChoiceListValidator.new(choice_list)
26
+ if validation.valid?
27
+ call(:put, "choice_lists/#{id}.json", choice_list)
28
+ else
29
+ { error: { validation: validation.errors } }
30
+ end
31
+ end
32
+
33
+ def delete(id)
34
+ call(:delete, "choice_lists/#{id}.json")
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ module Fulcrum
2
+ class ClassificationSet < Api
3
+
4
+ class << self
5
+
6
+ def all(opts = {})
7
+ params = parse_opts([:page], opts)
8
+ call(:get, 'classification_sets.json', params)
9
+ end
10
+
11
+ def find(id)
12
+ call(:get, "classification_sets/#{id}.json")
13
+ end
14
+
15
+ def create(classification_set)
16
+ validation = ClassificationSetValidator.new(classification_set)
17
+ if validation.valid?
18
+ call(:post, 'classification_sets.json', classification_set)
19
+ else
20
+ { error: { validation: validation.errors } }
21
+ end
22
+ end
23
+
24
+ def update(id, classification_set)
25
+ validation = ClassificationSetValidator.new(classification_set)
26
+ if validation.valid?
27
+ call(:put, "classification_sets/#{id}.json", classification_set)
28
+ else
29
+ { error: { validation: validation.errors } }
30
+ end
31
+ end
32
+
33
+ def delete(id)
34
+ call(:delete, "classification_sets/#{id}.json")
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module Fulcrum
2
+ class Form < Api
3
+
4
+ class << self
5
+
6
+ def all(opts = {})
7
+ params = self.parse_opts([:page, :schema], opts)
8
+ self.call(:get, 'forms.json', params)
9
+ end
10
+
11
+ def find(id, opts = {})
12
+ params = parse_opts([:include_foreign_elements], opts)
13
+ call(:get, "forms/#{id}.json", params)
14
+ end
15
+
16
+ def create(form)
17
+ validation = FormValidator.new(form)
18
+ if validation.valid?
19
+ call(:post, "forms.json", form)
20
+ else
21
+ { error: { validation: validation.errors } }
22
+ end
23
+ end
24
+
25
+ def update(id, form)
26
+ validation = FormValidator.new(form)
27
+ if validation.valid?
28
+ call(:put, "forms/#{id}.json", form)
29
+ else
30
+ { error: { validation: validation.errors } }
31
+ end
32
+ end
33
+
34
+ def delete(id)
35
+ call(:delete, "forms/#{id}.json")
36
+ end
37
+ end
38
+ end
39
+ end