wordpress-api 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wordpress-api.rb +130 -25
  3. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ee4ce6d2b1c78eaba10a6fe4274cdb32d0dacfd
4
- data.tar.gz: 735723ab9c6522c41d812ed3f88d476f95344aac
3
+ metadata.gz: 6ac01ab9d05af338cd83dad0bb3caf0483a9bb74
4
+ data.tar.gz: 4362588688c0d7938d14de50ff0333cd9c45d5c9
5
5
  SHA512:
6
- metadata.gz: 0db0b47608b17f7a100733db8333d0d9a15b940168d44c9252cb6899d662247b4f09a6acfb6ec36cb6324042b4cd1b4a63acdadb4b6fc2d59e412cc633bc15cb
7
- data.tar.gz: 2e474fbaeb60b08bc492a1b11ac2c14e03ed2c275712e225ef0cede94b52fdb237b84a2dcc58ed13771766a2d7f79ee6da13b1d4b59160d6c5a64de45949fdf5
6
+ metadata.gz: 5cf1a57d1555e19d8e0c0bd4b75d33d3ddc875db8055ddf2311d3b779e0fd0e665c0ce5c41530b53120bac8578ee126a8079ae59a0e04aa5724caed071ca14d4
7
+ data.tar.gz: ab05fc53aef240b9816a26b76f9c5ffb46626e5c4daf91489e7b659f33d325ca2cf7be5dead642c8a55e3b1e12f9c4f3a054226ee18ab4845c5c1dac7ff2ee11
@@ -1,51 +1,156 @@
1
- require 'open-uri'
1
+ require 'net/http'
2
2
  require 'json'
3
3
 
4
4
  class WordPress
5
- def self.hi
6
- puts "Hello world!"
7
- end
8
5
 
9
6
  def self.set_source(source)
10
7
  @source = source
11
8
  end
12
9
 
13
- def self.get_posts(source, count = nil)
14
- unless count.nil?
15
- data = open("#{source}posts/")
10
+ def self.query_url(uri)
11
+ begin
12
+ data = Net::HTTP.get(uri)
13
+ rescue
14
+ puts "There was an error with the provided URL: #{uri}"
15
+ end
16
+ end
17
+
18
+ def self.prepare_query(params)
19
+ args = Hash.new
20
+ if params.is_a?(Hash)
21
+ params.each{|x|
22
+ filter = "filter[#{x[0]}]";
23
+ arg = {filter => x[1]};
24
+ args = args.merge(arg);
25
+ return args
26
+ }
27
+ else
28
+ raise RuntimeError, "Something is wrong, perhaps you did not pass a hash."
29
+ end
30
+ end
31
+
32
+ def self.query_load_json(source, endpoint, params)
33
+ url = URI("#{source}#{endpoint}/")
34
+ if params
35
+ args = prepare_query params
36
+ url.query = URI.encode_www_form(args)
37
+ end
38
+ data = self.query_url(url)
39
+ content = JSON.load(data)
40
+ return content
41
+ end
42
+
43
+ # Get Posts from a WordPress source
44
+ #
45
+ # Requires:
46
+ # source (str): the url to the root API endpoint
47
+ # params (hash): a hash of parameters to filter by
48
+ #
49
+ # Note, the hash for the params argument should be formed like this:
50
+ #
51
+ # params = {'posts_per_page'=>'1','order'=>'ASC'}
52
+ #
53
+ # Even though WordPress requires the "filter" argument, we will prepend that
54
+ # for you in this method.
55
+ #
56
+ def self.get_posts(source, params = nil, post = nil)
57
+ if post.nil?
58
+ wpposts = self.query_load_json(source, "posts", params)
16
59
  else
17
- data = open("#{source}posts/?filter[posts_per_page]=#{count}")
60
+ wpposts = self.get_post(source, post)
18
61
  end
19
- wpposts = JSON.load(data)
20
62
  end
21
63
 
22
64
  def self.get_post(source, post)
23
- data = open("#{source}posts/#{post}")
24
- post = JSON.load(data)
65
+ posts = self.query_load_json(source, "posts/#{post}", params = nil)
66
+ end
67
+
68
+ # Get Media from a WordPress source
69
+ #
70
+ # By default gets the 9 most recent media objects from the WordPress site. If
71
+ # passed optional parameters, will filter those posts accordingly. Like
72
+ # get_posts, this should be passed as a hash. If a specific attachment is
73
+ # given as the third parameter, it will return only that attachment's object
74
+ # and the `params` hash will be ignored (if passed)
75
+ #
76
+ # Requires:
77
+ # source (str): the url to the root API endpoint
78
+ # attachment (int, optional): a specific attachment to get
79
+ # params (hash, optional): a hash of parameters to filter by
80
+ #
81
+ # Note, the hash for the params argument should be formed like this:
82
+ #
83
+ # params = {'posts_per_page'=>'1','order'=>'ASC'}
84
+ #
85
+ # Even though WordPress requires the "filter" argument, we will prepend that
86
+ # for you in this method.
87
+
88
+ def self.get_media(source, params = nil, attachment = nil)
89
+ if attachment
90
+ posts = get_attachment(source, attachment)
91
+ else
92
+ posts = self.query_load_json(source, "media", params)
93
+ end
25
94
  end
26
95
 
27
- def self.get_media(source)
28
- data = open("#{source}media/")
29
- media = JSON.load(data)
96
+ def self.get_attachment(source, attachment)
97
+ posts = self.query_load_json(source, "media/#{attachment}")
30
98
  end
31
99
 
32
- def self.get_taxonomies(source)
33
- data = open("#{source}/taxonomies")
34
- taxonomies = JSON.load(data)
100
+ # Get Taxonomies from a WordPress source
101
+ #
102
+ # By default, returns all taxonomies registered in the WordPress database.
103
+ #
104
+ # If passed a hash of parameters, will filter the taxonomies accordingly.
105
+ # (See wp-json documentation at http://wp-api.org/#taxonomies_retrieve-all-taxonomies)
106
+ #
107
+ # Requires:
108
+ # source (str): the url to the root API endpoint
109
+ #
110
+ # Optional
111
+ # attachment (int): a specific attachment to get
112
+ # params (hash): a hash of parameters to filter by (currently no filters are
113
+ # documented at wp-api.org)
114
+ #
115
+ # Note, the hash for the params argument should be formed like this:
116
+ #
117
+ # params = {'posts_per_page'=>'1','order'=>'ASC'}
118
+ #
119
+ # Even though WordPress requires the "filter" argument, we will prepend that
120
+ # for you in this method.
121
+
122
+ def self.get_taxonomies(source, params = nil, taxonomy = nil)
123
+ if taxonomy
124
+ get_taxonomy(source, taxonomy)
125
+ else
126
+ taxonomies = self.query_load_json(source, "taxonomies")
127
+ end
35
128
  end
36
129
 
37
130
  def self.get_taxonomy(source, taxonomy)
38
- data = open("#{source}/taxonomies/#{taxonomy}")
39
- taxonomy = JSON.load(data)
131
+ taxonomy = self.query_load_json(source, "taxonomies/#{taxonomy}")
40
132
  end
41
133
 
42
- def self.get_terms(source, taxonomy)
43
- data = open("#{source}/taxonomies/#{taxonomy}/terms")
44
- terms = JSON.load(data)
134
+ # Get terms
135
+ #
136
+ # Retrieves all terms associated with a given taxonomy.
137
+ #
138
+ # Requires:
139
+ # source (str): the url to the root API endpoint
140
+ # taxonomy (str): the taxonomy from which to retrieve terms
141
+ #
142
+ # Optional:
143
+ # term (str): the slug of the term to retrieve
144
+
145
+ def self.get_terms(source, taxonomy, term = nil)
146
+ if term
147
+ terms = get_term(source, taxonomy, term)
148
+ else
149
+ terms = self.query_load_json(source, "/taxonomies/#{taxonomy}/terms")
150
+ end
45
151
  end
46
152
 
47
- def self.get_term(source, taxonomy, term)
48
- data = open("#{source}/taxonomies/#{taxonomy}/terms/#{term}")
49
- term = JSON.load(data)
153
+ def get_term(source, taxonomy, term)
154
+ term = self.query_load_json(source, "/taxonomies/#{taxonomy}/terms/#{term}")
50
155
  end
51
156
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordpress-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Boone
@@ -10,8 +10,10 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-01-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Methods for interfacing with the WordPress API. Currently supports all
14
- get requests documented at http://wp-api.org.
13
+ description: |-
14
+ Methods for interfacing with the WordPress API. Currently
15
+ supports all unauthenticated get requests documented at the
16
+ WordPress API homepage > http://wp-api.org.
15
17
  email: greg@harmsboone.org
16
18
  executables: []
17
19
  extensions: []