we_the_people 0.0.3
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/Gemfile +5 -0
- data/Gemfile.lock +24 -0
- data/LICENSE +21 -0
- data/README.md +46 -0
- data/Rakefile +150 -0
- data/lib/we_the_people/association_proxy.rb +32 -0
- data/lib/we_the_people/collection.rb +85 -0
- data/lib/we_the_people/config.rb +72 -0
- data/lib/we_the_people/embedded_resource.rb +18 -0
- data/lib/we_the_people/resource.rb +164 -0
- data/lib/we_the_people/resources/issue.rb +6 -0
- data/lib/we_the_people/resources/location.rb +6 -0
- data/lib/we_the_people/resources/petition.rb +29 -0
- data/lib/we_the_people/resources/response.rb +6 -0
- data/lib/we_the_people/resources/signature.rb +14 -0
- data/lib/we_the_people/resources/user.rb +8 -0
- data/lib/we_the_people/simple.rb +38 -0
- data/lib/we_the_people.rb +43 -0
- data/we_the_people.gemspec +77 -0
- metadata +114 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
we_the_people (0.0.1)
|
5
|
+
active_support
|
6
|
+
json
|
7
|
+
rest-client
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
active_support (3.0.0)
|
13
|
+
activesupport (= 3.0.0)
|
14
|
+
activesupport (3.0.0)
|
15
|
+
json (1.7.7)
|
16
|
+
mime-types (1.21)
|
17
|
+
rest-client (1.6.7)
|
18
|
+
mime-types (>= 1.16)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
we_the_people!
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) Tom Preston-Werner
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# WeThePeople
|
2
|
+
|
3
|
+
`we_the_people` is a gem to access the new [We The People](https://petitions.whitehouse.gov) petitions API.
|
4
|
+
|
5
|
+
# Quickstart
|
6
|
+
|
7
|
+
First, you'll need to configure your API key:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
WeThePeople::Config.api_key = "1234abcd"
|
11
|
+
```
|
12
|
+
|
13
|
+
Now you're ready to start asking for resources. Here are a few example calls:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
>> petition = WeThePeople::Resources::Petition.find("1234")
|
17
|
+
>> petition.body
|
18
|
+
# => "Example body"
|
19
|
+
>> petition.title
|
20
|
+
# => "My Example Petition"
|
21
|
+
>> petition.issues.first.name
|
22
|
+
# => "Civil Rights"
|
23
|
+
|
24
|
+
>> petitions = WeThePeople::Resources::Petition.all
|
25
|
+
>> petition2 = petitions.first
|
26
|
+
# Not yet implemented in the API (signatures)...
|
27
|
+
>> petition2.signatures.all.first.city
|
28
|
+
# => "Orlando"
|
29
|
+
```
|
30
|
+
|
31
|
+
# Configuration
|
32
|
+
|
33
|
+
You can configure a few options on the `WeThePeople` module:
|
34
|
+
|
35
|
+
* `api_key` - Required to make any calls; your We The People API key.
|
36
|
+
* `default_page_size` - The page size to request by default for all resources.
|
37
|
+
* `client` - If you don't want to use `rest-client` you can substitute in another HTTP client object that conforms to the same API here.
|
38
|
+
* `mock` - If set to "1", all requests will return mock results.
|
39
|
+
|
40
|
+
# Contributing
|
41
|
+
|
42
|
+
Hack some code, make a pull request. I'll review it and merge it in! Need some ideas as to where to get started? Here are a few:
|
43
|
+
|
44
|
+
* Tests. Please?
|
45
|
+
* Make resources be able to be related + associated. It looks like responses may end up going this route.
|
46
|
+
* Documentationages.
|
data/Rakefile
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
#############################################################################
|
6
|
+
#
|
7
|
+
# Helper functions
|
8
|
+
#
|
9
|
+
#############################################################################
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"#{name}-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
#############################################################################
|
41
|
+
#
|
42
|
+
# Standard tasks
|
43
|
+
#
|
44
|
+
#############################################################################
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rake/testtask'
|
49
|
+
Rake::TestTask.new(:test) do |test|
|
50
|
+
test.libs << 'lib' << 'test'
|
51
|
+
test.pattern = 'test/**/test_*.rb'
|
52
|
+
test.verbose = true
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Generate RCov test coverage and open in your browser"
|
56
|
+
task :coverage do
|
57
|
+
require 'rcov'
|
58
|
+
sh "rm -fr coverage"
|
59
|
+
sh "rcov test/test_*.rb"
|
60
|
+
sh "open coverage/index.html"
|
61
|
+
end
|
62
|
+
|
63
|
+
require 'rdoc/task'
|
64
|
+
Rake::RDocTask.new do |rdoc|
|
65
|
+
rdoc.rdoc_dir = 'rdoc'
|
66
|
+
rdoc.title = "#{name} #{version}"
|
67
|
+
rdoc.rdoc_files.include('README*')
|
68
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
69
|
+
end
|
70
|
+
|
71
|
+
desc "Open an irb session preloaded with this library"
|
72
|
+
task :console do
|
73
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
74
|
+
end
|
75
|
+
|
76
|
+
#############################################################################
|
77
|
+
#
|
78
|
+
# Custom tasks (add your own tasks here)
|
79
|
+
#
|
80
|
+
#############################################################################
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
#############################################################################
|
85
|
+
#
|
86
|
+
# Packaging tasks
|
87
|
+
#
|
88
|
+
#############################################################################
|
89
|
+
|
90
|
+
desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
|
91
|
+
task :release => :build do
|
92
|
+
unless `git branch` =~ /^\* master$/
|
93
|
+
puts "You must be on the master branch to release!"
|
94
|
+
exit!
|
95
|
+
end
|
96
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
97
|
+
sh "git tag v#{version}"
|
98
|
+
sh "git push origin master"
|
99
|
+
sh "git push origin v#{version}"
|
100
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
101
|
+
end
|
102
|
+
|
103
|
+
desc "Build #{gem_file} into the pkg directory"
|
104
|
+
task :build => :gemspec do
|
105
|
+
sh "mkdir -p pkg"
|
106
|
+
sh "gem build #{gemspec_file}"
|
107
|
+
sh "mv #{gem_file} pkg"
|
108
|
+
end
|
109
|
+
|
110
|
+
desc "Generate #{gemspec_file}"
|
111
|
+
task :gemspec => :validate do
|
112
|
+
# read spec file and split out manifest section
|
113
|
+
spec = File.read(gemspec_file)
|
114
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
115
|
+
|
116
|
+
# replace name version and date
|
117
|
+
replace_header(head, :name)
|
118
|
+
replace_header(head, :version)
|
119
|
+
replace_header(head, :date)
|
120
|
+
#comment this out if your rubyforge_project has a different name
|
121
|
+
replace_header(head, :rubyforge_project)
|
122
|
+
|
123
|
+
# determine file list from git ls-files
|
124
|
+
files = `git ls-files`.
|
125
|
+
split("\n").
|
126
|
+
sort.
|
127
|
+
reject { |file| file =~ /^\./ }.
|
128
|
+
reject { |file| file =~ /^(rdoc|pkg)/ }.
|
129
|
+
map { |file| " #{file}" }.
|
130
|
+
join("\n")
|
131
|
+
|
132
|
+
# piece file back together and write
|
133
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
134
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
135
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
136
|
+
puts "Updated #{gemspec_file}"
|
137
|
+
end
|
138
|
+
|
139
|
+
desc "Validate #{gemspec_file}"
|
140
|
+
task :validate do
|
141
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
142
|
+
unless libfiles.empty?
|
143
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
144
|
+
exit!
|
145
|
+
end
|
146
|
+
unless Dir['VERSION*'].empty?
|
147
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
148
|
+
exit!
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module WeThePeople
|
2
|
+
class AssociationProxy
|
3
|
+
def initialize(parent, klass)
|
4
|
+
@klass = klass
|
5
|
+
@parent = parent
|
6
|
+
end
|
7
|
+
|
8
|
+
def find(id)
|
9
|
+
@klass.find(id, @parent)
|
10
|
+
end
|
11
|
+
|
12
|
+
def cursor(criteria = {})
|
13
|
+
@cursor ||= @klass.cursor(@parent, criteria)
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_all
|
17
|
+
cursor.get_all(false)
|
18
|
+
end
|
19
|
+
|
20
|
+
def all
|
21
|
+
cursor.all
|
22
|
+
end
|
23
|
+
|
24
|
+
def length
|
25
|
+
cursor.length
|
26
|
+
end
|
27
|
+
|
28
|
+
def first
|
29
|
+
cursor.first
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module WeThePeople
|
2
|
+
class Collection
|
3
|
+
include Enumerable
|
4
|
+
attr_reader :count, :offset, :limit, :current_page, :all
|
5
|
+
|
6
|
+
def initialize(klass, conditions, hash, parent = nil)
|
7
|
+
@criteria = conditions
|
8
|
+
@parent = parent
|
9
|
+
|
10
|
+
if hash['metadata']['resultset']
|
11
|
+
@count = hash['metadata']['resultset']['count'].to_i
|
12
|
+
@offset = hash['metadata']['resultset']['offset'].to_i
|
13
|
+
@limit = hash['metadata']['resultset']['limit'].to_i
|
14
|
+
else
|
15
|
+
@count = WeThePeople::Config.default_page_size
|
16
|
+
@offset = 0
|
17
|
+
@limit = WeThePeople::Config.default_page_size
|
18
|
+
end
|
19
|
+
|
20
|
+
@klass = klass
|
21
|
+
|
22
|
+
@all = []
|
23
|
+
process_results(hash['results'])
|
24
|
+
end
|
25
|
+
|
26
|
+
def next_page
|
27
|
+
@offset += @limit unless @all.empty?
|
28
|
+
|
29
|
+
fetch_current_page
|
30
|
+
@current_page
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch_current_page
|
34
|
+
fetch_page(@offset, @limit)
|
35
|
+
end
|
36
|
+
|
37
|
+
def previous_page
|
38
|
+
@offset -= @limit
|
39
|
+
@offset = 0 if @offset < 0
|
40
|
+
|
41
|
+
@all.slice(@offset, @limit)
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_all(refresh = false)
|
45
|
+
if refresh
|
46
|
+
@all = []
|
47
|
+
@offset = 0
|
48
|
+
end
|
49
|
+
|
50
|
+
(@offset..@count).step(@limit) do |current_offset|
|
51
|
+
@all += fetch_page(current_offset, @limit)
|
52
|
+
end
|
53
|
+
|
54
|
+
@all
|
55
|
+
end
|
56
|
+
|
57
|
+
def length
|
58
|
+
@count
|
59
|
+
end
|
60
|
+
alias size length
|
61
|
+
|
62
|
+
def page_length
|
63
|
+
@limit
|
64
|
+
end
|
65
|
+
alias page_size page_length
|
66
|
+
|
67
|
+
def each
|
68
|
+
@all.each do |record|
|
69
|
+
yield record
|
70
|
+
end
|
71
|
+
end
|
72
|
+
private
|
73
|
+
def fetch_page(page_offset, page_limit)
|
74
|
+
process_results(@klass.fetch(@parent, @criteria.merge(:offset => page_offset, :limit => page_limit))['results'])
|
75
|
+
end
|
76
|
+
|
77
|
+
def process_results(results)
|
78
|
+
@current_page = results.map do |result|
|
79
|
+
@klass.new(result)
|
80
|
+
end
|
81
|
+
|
82
|
+
@all += @current_page
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module WeThePeople
|
2
|
+
class Config
|
3
|
+
class MotionHTTPWrapper
|
4
|
+
class Response
|
5
|
+
attr_reader :body
|
6
|
+
def initialize(response_body)
|
7
|
+
@body = response_body
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.get(url, args = {})
|
12
|
+
url_string = ("#{url}?#{make_query_string(args[:params] || {})}").stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
|
13
|
+
url = NSURL.URLWithString(url_string)
|
14
|
+
request = NSURLRequest.requestWithURL(url)
|
15
|
+
response = nil
|
16
|
+
error = nil
|
17
|
+
data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: error)
|
18
|
+
|
19
|
+
NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.make_query_string(hash)
|
23
|
+
hash.collect do |key, value|
|
24
|
+
"#{key}=#{value}"
|
25
|
+
end.sort * '&'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class MotionJSONWrapper
|
30
|
+
def self.parse(string_data)
|
31
|
+
json_data = string_data.dataUsingEncoding(NSUTF8StringEncoding)
|
32
|
+
e = Pointer.new(:object)
|
33
|
+
NSJSONSerialization.JSONObjectWithData(json_data, options:0, error: e).mutableCopy
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class <<self
|
38
|
+
attr_writer :default_page_size
|
39
|
+
def default_page_size
|
40
|
+
@default_page_size ||= 1000
|
41
|
+
end
|
42
|
+
|
43
|
+
attr_writer :client
|
44
|
+
def client
|
45
|
+
@client ||= (in_motion? ? MotionHTTPWrapper : RestClient)
|
46
|
+
end
|
47
|
+
|
48
|
+
def json
|
49
|
+
in_motion? ? MotionJSONWrapper : JSON
|
50
|
+
end
|
51
|
+
|
52
|
+
def in_motion?
|
53
|
+
defined?(BW)
|
54
|
+
end
|
55
|
+
|
56
|
+
def host
|
57
|
+
"http://api.whitehouse.gov/v1"
|
58
|
+
end
|
59
|
+
|
60
|
+
attr_accessor :api_key
|
61
|
+
|
62
|
+
def default_params
|
63
|
+
params = { :key => @api_key }
|
64
|
+
params.merge!(:mock => 1) if @mock
|
65
|
+
params.merge!(:limit => default_page_size)
|
66
|
+
params
|
67
|
+
end
|
68
|
+
|
69
|
+
attr_accessor :mock
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Struct
|
2
|
+
def self.hash_initialized(*keys)
|
3
|
+
klass = Class.new(self.new(*keys))
|
4
|
+
keys = keys.map(&:to_s)
|
5
|
+
|
6
|
+
klass.class_eval do
|
7
|
+
define_method(:initialize) do |h|
|
8
|
+
super(*h.values_at(*keys))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
klass
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def EmbeddedResource(*keys)
|
17
|
+
Struct.hash_initialized(*keys)
|
18
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
module WeThePeople
|
2
|
+
class Resource
|
3
|
+
class <<self
|
4
|
+
attr_reader :embedded_attributes, :embedded_array_attributes, :attributes
|
5
|
+
|
6
|
+
def bare_name
|
7
|
+
name.split("::").last
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(id, parent = nil)
|
11
|
+
raise "Must be called by parent." if @belongs_to && parent.nil?
|
12
|
+
|
13
|
+
json = WeThePeople::Config.client.get(build_resource_url(id, parent), :params => WeThePeople::Config.default_params).to_s
|
14
|
+
new(WeThePeople::Config.json.parse(json)['results'].first)
|
15
|
+
end
|
16
|
+
|
17
|
+
def path(parent = nil)
|
18
|
+
raise "Must be called by parent." if @belongs_to && parent.nil?
|
19
|
+
|
20
|
+
if parent
|
21
|
+
"#{parent.path}/#{bare_name.underscore.pluralize}"
|
22
|
+
else
|
23
|
+
"#{bare_name.underscore.pluralize}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_resource_url(id, parent = nil)
|
28
|
+
"#{WeThePeople::Config.host}/#{path(parent)}/#{id}.json"
|
29
|
+
end
|
30
|
+
|
31
|
+
def fetch(parent = nil, criteria = {})
|
32
|
+
raise "Must be called by parent." if @belongs_to && parent.nil?
|
33
|
+
|
34
|
+
body = WeThePeople::Config.client.get(build_index_url(parent, criteria), :params => criteria.merge(WeThePeople::Config.default_params)).to_s
|
35
|
+
WeThePeople::Config.json.parse(body)
|
36
|
+
end
|
37
|
+
|
38
|
+
def cursor(parent = nil, criteria = {})
|
39
|
+
Collection.new(self, criteria, fetch(parent, criteria), parent)
|
40
|
+
end
|
41
|
+
|
42
|
+
def all(parent = nil, criteria = {})
|
43
|
+
cursor(parent, criteria).all
|
44
|
+
end
|
45
|
+
|
46
|
+
def build_index_url(parent = nil, criteria = {})
|
47
|
+
"#{WeThePeople::Config.host}/#{path(parent)}.json"
|
48
|
+
end
|
49
|
+
|
50
|
+
def belongs_to(klass_name)
|
51
|
+
@belongs_to = klass_name
|
52
|
+
|
53
|
+
define_method klass_name.to_s.downcase do
|
54
|
+
@parent
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_attribute_key(key)
|
59
|
+
@attributes ||= []
|
60
|
+
@attributes << key.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
COERCERS = {
|
64
|
+
Integer => lambda {|v| Integer(v) },
|
65
|
+
Time => lambda {|v| Time.at(v) }
|
66
|
+
}
|
67
|
+
|
68
|
+
def coerce_value(val, klass)
|
69
|
+
COERCERS[klass].call(val)
|
70
|
+
end
|
71
|
+
|
72
|
+
def attribute(name, coerce_to = nil)
|
73
|
+
name = name.to_s
|
74
|
+
add_attribute_key(name)
|
75
|
+
|
76
|
+
define_method "#{name}=" do |val|
|
77
|
+
val = self.class.coerce_value(val, coerce_to) if coerce_to
|
78
|
+
@attributes[name] = val
|
79
|
+
end
|
80
|
+
|
81
|
+
define_method name do
|
82
|
+
coerce_to ? self.class.coerce_value(@attributes[name], coerce_to) : @attributes[name]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def has_embedded(klass_name)
|
87
|
+
name = klass_name.to_s.singularize
|
88
|
+
|
89
|
+
add_attribute_key(name)
|
90
|
+
@embedded_attributes ||= []
|
91
|
+
@embedded_attributes << name
|
92
|
+
|
93
|
+
define_method "#{name}=" do |val|
|
94
|
+
val = klass.new(val)
|
95
|
+
@attributes[name] = val
|
96
|
+
end
|
97
|
+
|
98
|
+
define_method name do
|
99
|
+
@attributes[name]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def has_many_embedded(klass_name)
|
104
|
+
name = klass_name.to_s.pluralize
|
105
|
+
|
106
|
+
add_attribute_key(name)
|
107
|
+
@embedded_array_attributes ||= []
|
108
|
+
@embedded_array_attributes << name
|
109
|
+
|
110
|
+
define_method "#{name}=" do |val|
|
111
|
+
raise TypeError unless val.is_a?(Array)
|
112
|
+
|
113
|
+
val = val.map {|o| klass.new(o)}
|
114
|
+
@attributes[name] = val
|
115
|
+
end
|
116
|
+
|
117
|
+
define_method name do
|
118
|
+
@attributes[name]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def has_many(klass_name)
|
123
|
+
name = klass_name.to_s.pluralize
|
124
|
+
|
125
|
+
add_attribute_key(name)
|
126
|
+
|
127
|
+
define_method name do
|
128
|
+
AssociationProxy.new(self, "WeThePeople::Resources::#{name.classify}".constantize)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def initialize(attrs, parent = nil)
|
134
|
+
@parent = parent
|
135
|
+
|
136
|
+
result = {}
|
137
|
+
attrs.each_pair do |key, value|
|
138
|
+
result[key.to_s.gsub(/\s/, '_')] = value
|
139
|
+
end
|
140
|
+
|
141
|
+
attrs = result.select {|a| self.class.attributes.include?(a)}
|
142
|
+
|
143
|
+
self.class.embedded_attributes.each do |embedded_key|
|
144
|
+
attrs[embedded_key] = "WeThePeople::Resources::#{embedded_key.classify}".constantize.new(attrs[embedded_key]) if !attrs[embedded_key].nil? && !attrs[embedded_key].empty?
|
145
|
+
end if self.class.embedded_attributes
|
146
|
+
|
147
|
+
self.class.embedded_array_attributes.each do |embedded_array_key|
|
148
|
+
if attrs[embedded_array_key].is_a?(Array)
|
149
|
+
# NOTE: This is to make RubyMotion happy
|
150
|
+
attrs[embedded_array_key] = attrs[embedded_array_key].dup
|
151
|
+
attrs[embedded_array_key].map! do |embedded_array_element|
|
152
|
+
"WeThePeople::Resources::#{embedded_array_key.classify}".constantize.new(embedded_array_element)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end if self.class.embedded_array_attributes
|
156
|
+
|
157
|
+
@attributes = attrs
|
158
|
+
end
|
159
|
+
|
160
|
+
def path
|
161
|
+
"#{self.class.path}/#{id}"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module WeThePeople
|
2
|
+
module Resources
|
3
|
+
class Petition < WeThePeople::Resource
|
4
|
+
attribute :id
|
5
|
+
attribute :type
|
6
|
+
attribute :title
|
7
|
+
attribute :body
|
8
|
+
attribute :status
|
9
|
+
attribute :signature_threshold, Integer
|
10
|
+
attribute :signature_count, Integer
|
11
|
+
attribute :signatures_needed, Integer
|
12
|
+
attribute :deadline, Time
|
13
|
+
attribute :created, Time
|
14
|
+
|
15
|
+
has_embedded :response
|
16
|
+
has_many_embedded :issues
|
17
|
+
|
18
|
+
has_many :signatures
|
19
|
+
|
20
|
+
def successful?
|
21
|
+
signatures_needed == 0
|
22
|
+
end
|
23
|
+
|
24
|
+
def failed?
|
25
|
+
deadline < Time.now && !successful?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module WeThePeople
|
2
|
+
class Simple
|
3
|
+
class <<self
|
4
|
+
def petitions(criteria = {})
|
5
|
+
get("petitions.json", criteria)
|
6
|
+
end
|
7
|
+
|
8
|
+
def petition(id)
|
9
|
+
get("petitions/#{id}.json").first
|
10
|
+
end
|
11
|
+
|
12
|
+
def signatures(petition_id, criteria = {})
|
13
|
+
get("petitions/#{petition_id}/signatures.json", criteria)
|
14
|
+
end
|
15
|
+
|
16
|
+
def signature(petition_id, signature_id)
|
17
|
+
get("petitions/#{petition_id}/signatures/#{signature_id}.json").first
|
18
|
+
end
|
19
|
+
|
20
|
+
def users(criteria = {})
|
21
|
+
get("users.json", criteria)
|
22
|
+
end
|
23
|
+
|
24
|
+
def user(id)
|
25
|
+
get("users/#{id}.json").first
|
26
|
+
end
|
27
|
+
|
28
|
+
def url(path)
|
29
|
+
"#{WeThePeople::Config.host}/#{path}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def get(path, params = {})
|
33
|
+
response = WeThePeople::Config.client.get(url(path), :params => params.merge(WeThePeople::Config.default_params))
|
34
|
+
WeThePeople::Config.json.parse(response.body)['results']
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'rest_client'
|
5
|
+
require 'active_support/inflector'
|
6
|
+
require 'active_support/core_ext/hash'
|
7
|
+
require 'active_support/core_ext/module/delegation'
|
8
|
+
|
9
|
+
require 'we_the_people/config'
|
10
|
+
require 'we_the_people/resource'
|
11
|
+
require 'we_the_people/embedded_resource'
|
12
|
+
require 'we_the_people/association_proxy'
|
13
|
+
require 'we_the_people/collection'
|
14
|
+
|
15
|
+
require 'we_the_people/resources/petition'
|
16
|
+
require 'we_the_people/resources/issue'
|
17
|
+
require 'we_the_people/resources/response'
|
18
|
+
require 'we_the_people/resources/signature'
|
19
|
+
require 'we_the_people/resources/location'
|
20
|
+
require 'we_the_people/resources/user'
|
21
|
+
|
22
|
+
if defined?(Motion::Project::Config)
|
23
|
+
Motion::Project::App.setup do |app|
|
24
|
+
# Borrowed from the teacup gem
|
25
|
+
insert_point = 0
|
26
|
+
app.files.each_index do |index|
|
27
|
+
file = app.files[index]
|
28
|
+
if file =~ /^(?:\.\/)?app\//
|
29
|
+
# found app/, so stop looking
|
30
|
+
break
|
31
|
+
end
|
32
|
+
insert_point = index + 1
|
33
|
+
end
|
34
|
+
|
35
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'we_the_people/**/*.rb')).reverse.each do |file|
|
36
|
+
app.files.insert(insert_point, file)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module WeThePeople
|
42
|
+
VERSION = '0.0.3'
|
43
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
## This is the rakegem gemspec template. Make sure you read and understand
|
2
|
+
## all of the comments. Some sections require modification, and others can
|
3
|
+
## be deleted if you don't need them. Once you understand the contents of
|
4
|
+
## this file, feel free to delete any comments that begin with two hash marks.
|
5
|
+
## You can find comprehensive Gem::Specification documentation, at
|
6
|
+
## http://docs.rubygems.org/read/chapter/20
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.rubygems_version = '1.3.5'
|
11
|
+
|
12
|
+
## Leave these as is they will be modified for you by the rake gemspec task.
|
13
|
+
## If your rubyforge_project name is different, then edit it and comment out
|
14
|
+
## the sub! line in the Rakefile
|
15
|
+
s.name = 'we_the_people'
|
16
|
+
s.version = '0.0.3'
|
17
|
+
s.date = '2013-04-25'
|
18
|
+
s.rubyforge_project = 'we_the_people'
|
19
|
+
|
20
|
+
## Make sure your summary is short. The description may be as long
|
21
|
+
## as you like.
|
22
|
+
s.summary = "API client for We The People."
|
23
|
+
s.description = "API client for the We The People petition application."
|
24
|
+
|
25
|
+
## List the primary authors. If there are a bunch of authors, it's probably
|
26
|
+
## better to set the email to an email list or something. If you don't have
|
27
|
+
## a custom homepage, consider using your GitHub URL or the like.
|
28
|
+
s.authors = ["Jeremy McAnally"]
|
29
|
+
s.email = 'jeremy@github.com'
|
30
|
+
s.homepage = 'http://github.com/jm/we_the_people'
|
31
|
+
|
32
|
+
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
33
|
+
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
34
|
+
s.require_paths = %w[lib]
|
35
|
+
|
36
|
+
## Specify any RDoc options here. You'll want to add your README and
|
37
|
+
## LICENSE files to the extra_rdoc_files list.
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.extra_rdoc_files = %w[README.md LICENSE]
|
40
|
+
|
41
|
+
## List your runtime dependencies here. Runtime dependencies are those
|
42
|
+
## that are needed for an end user to actually USE your code.
|
43
|
+
s.add_dependency('rest-client')
|
44
|
+
s.add_dependency('json')
|
45
|
+
s.add_dependency('active_support')
|
46
|
+
|
47
|
+
## Leave this section as-is. It will be automatically generated from the
|
48
|
+
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
49
|
+
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
50
|
+
# = MANIFEST =
|
51
|
+
s.files = %w[
|
52
|
+
Gemfile
|
53
|
+
Gemfile.lock
|
54
|
+
LICENSE
|
55
|
+
README.md
|
56
|
+
Rakefile
|
57
|
+
lib/we_the_people.rb
|
58
|
+
lib/we_the_people/association_proxy.rb
|
59
|
+
lib/we_the_people/collection.rb
|
60
|
+
lib/we_the_people/config.rb
|
61
|
+
lib/we_the_people/embedded_resource.rb
|
62
|
+
lib/we_the_people/resource.rb
|
63
|
+
lib/we_the_people/resources/issue.rb
|
64
|
+
lib/we_the_people/resources/location.rb
|
65
|
+
lib/we_the_people/resources/petition.rb
|
66
|
+
lib/we_the_people/resources/response.rb
|
67
|
+
lib/we_the_people/resources/signature.rb
|
68
|
+
lib/we_the_people/resources/user.rb
|
69
|
+
lib/we_the_people/simple.rb
|
70
|
+
we_the_people.gemspec
|
71
|
+
]
|
72
|
+
# = MANIFEST =
|
73
|
+
|
74
|
+
## Test files will be grabbed from the file list. Make sure the path glob
|
75
|
+
## matches what you actually use.
|
76
|
+
s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: we_the_people
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy McAnally
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: active_support
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: API client for the We The People petition application.
|
63
|
+
email: jeremy@github.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files:
|
67
|
+
- README.md
|
68
|
+
- LICENSE
|
69
|
+
files:
|
70
|
+
- Gemfile
|
71
|
+
- Gemfile.lock
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/we_the_people.rb
|
76
|
+
- lib/we_the_people/association_proxy.rb
|
77
|
+
- lib/we_the_people/collection.rb
|
78
|
+
- lib/we_the_people/config.rb
|
79
|
+
- lib/we_the_people/embedded_resource.rb
|
80
|
+
- lib/we_the_people/resource.rb
|
81
|
+
- lib/we_the_people/resources/issue.rb
|
82
|
+
- lib/we_the_people/resources/location.rb
|
83
|
+
- lib/we_the_people/resources/petition.rb
|
84
|
+
- lib/we_the_people/resources/response.rb
|
85
|
+
- lib/we_the_people/resources/signature.rb
|
86
|
+
- lib/we_the_people/resources/user.rb
|
87
|
+
- lib/we_the_people/simple.rb
|
88
|
+
- we_the_people.gemspec
|
89
|
+
homepage: http://github.com/jm/we_the_people
|
90
|
+
licenses: []
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options:
|
93
|
+
- --charset=UTF-8
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project: we_the_people
|
110
|
+
rubygems_version: 1.8.23
|
111
|
+
signing_key:
|
112
|
+
specification_version: 2
|
113
|
+
summary: API client for We The People.
|
114
|
+
test_files: []
|