guidestar 0.0.1 → 0.1.0

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/README.md CHANGED
@@ -33,7 +33,7 @@ Once you initialize it, you can use it in your code via:
33
33
  results.total_count
34
34
  # => 1292
35
35
 
36
- results.organizations.each do |org|
36
+ results.each do |org|
37
37
  org.name
38
38
  # => 'Monkey Paradise'
39
39
  org.ein
@@ -46,9 +46,14 @@ Once you initialize it, you can use it in your code via:
46
46
 
47
47
  For getting the next page of results, it behaves similarly to kaminari or will_paginate:
48
48
 
49
- results.per(50).page(2).organizations
49
+ results.per(50).page(2).each { |more_orgs| puts more_orgs.name }
50
50
 
51
+ If you need more direct access to the array of results, just hit:
51
52
 
53
+ results.organizations
54
+
55
+ The way you use it is all super-flexible. You can pass in your options beforehand or
56
+ chain them along the way.
52
57
 
53
58
  ## Contributing
54
59
 
data/lib/guidestar.rb CHANGED
@@ -16,6 +16,10 @@ module Guidestar
16
16
  yield self
17
17
  true
18
18
  end
19
+
20
+ def method_missing(method_name, *args)
21
+ Guidestar::Client.new.send(method_name.to_sym, *args)
22
+ end
19
23
  end
20
24
 
21
25
  Faraday.register_middleware :response,
@@ -2,7 +2,8 @@ module Guidestar
2
2
  module Request
3
3
 
4
4
  def get(path, data={})
5
- Guidestar::Result.new(get_raw(path, data),path,self)
5
+ set_params(data)
6
+ Guidestar::Result.new(path,self)
6
7
  end
7
8
 
8
9
  def get_raw(path, data={})
@@ -20,7 +21,8 @@ module Guidestar
20
21
  @options[:page_size] = @options.delete(:per) if @options[:per]
21
22
  @options[:zip_radius] = @options.delete(:zipradius) if @options[:zipradius]
22
23
  @options[:org_name] = @options.delete(:name) if @options[:name]
23
- @options[:offset] = (@options.delete(:page)-1) if @options[:page]
24
+ @options[:offset] = (@options.delete(:page)-1) * @options[:page_size].to_i if @options[:page]
25
+ @options[:ein] = @options[:ein].to_s.insert(2,'-') if @options[:ein] && @options[:ein].to_s[2] != '-'
24
26
  builder = Nokogiri::XML::Builder.new do |xml|
25
27
  xml.query do
26
28
  xml.version @options[:version]
@@ -1,38 +1,59 @@
1
1
  module Guidestar
2
2
  class Result
3
- #
4
- # Public: Returns the String xml of the response from Guidestar
5
- attr_reader :xml
6
3
 
7
- # Public: Returns the Integer number of results from the query
8
- attr_reader :total_count
4
+ include Enumerable
5
+ extend Forwardable
6
+
7
+ def_delegators :organizations, :size, :length, :last
8
+ def_delegators :data, :xml, :total_count, :search_time
9
9
 
10
- # Public: Returns the Float time in seconds to search
11
- attr_reader :search_time
10
+ def initialize(path, client)
11
+ @path = path
12
+ @client = client
13
+ @options = {}
14
+ @data = Hashie::Mash.new
15
+ end
12
16
 
13
- # Public: Returns an Array of organizations from the search
17
+ # Public: Contains the data we retrieve, or gets new data if there
18
+ # are new parameters on the request.
19
+ #
20
+ # Returns an Array of organizations from the search
14
21
  def organizations
15
22
  return @organizations if @organizations
16
- parse_xml(@client.get_raw(@path, @options))
23
+ load_response
17
24
  @organizations
18
25
  end
19
26
 
20
- def initialize(raw_response, path, client)
21
- @path = path
22
- @client = client
23
- parse_xml(raw_response)
27
+ # Internal: Contains a few extra details that might be desired, like
28
+ # the total_count and search_time, which are also available via
29
+ # delegation
30
+ #
31
+ # Returns a Hash of extra data
32
+ def data
33
+ return @data if @organizations
34
+ load_response
35
+ @data
36
+ end
37
+
38
+ # Public: Iterates through the inner array to make data more accessible
39
+ def each(&block)
40
+ organizations.each(&block)
24
41
  end
25
42
 
26
43
  private
27
44
 
28
- def parse_xml(raw_response)
29
- @xml = ::MultiXml.parse(raw_response.body.string)['root']
30
- @total_count = @xml['totalResults'].to_i
31
- @search_time = @xml['searchTime'].to_f
32
- @options = {}
45
+ def load_response
46
+ raw_response = @client.get_raw(@path, @options)
47
+ @data[:xml] = ::MultiXml.parse(raw_response.body.string)['root']
48
+ @data[:total_count] = @data[:xml]['totalResults'].to_i
49
+ @data[:search_time] = @data[:xml]['searchTime'].to_f
33
50
 
51
+ @options = {}
34
52
  @organizations = []
35
- orgs = @xml['organizations']['organization']
53
+
54
+ orgs = @data[:xml]['organizations']['organization'] if @data[:xml]['organizations']
55
+ return if orgs.nil?
56
+
36
57
  orgs = [orgs] unless orgs.is_a?(Array)
37
58
  orgs.each do |org|
38
59
  org = Hashie::Mash.new clean_keys(org)
@@ -1,3 +1,3 @@
1
1
  module Guidestar
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guidestar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: